简体   繁体   中英

spring how to write config to support 2 database

i try to config a data source ----> mysql the other data source ----> h2 in memory (embedded ) with the following config:

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost/item"
    p:username="" p:password="" />

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
    p:dataSource-ref="dataSource">
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
            <prop key="net.sf.ehcache.configurationResourceName">/ehcache.xml</prop>
        </props>
    </property>
    <property name="packagesToScan" value="*************" />
</bean>

<!-- Spring transaction management -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"
    p:sessionFactory-ref="sessionFactory" />

<tx:annotation-driven transaction-manager="transactionManager" />


<bean id="org.h2.tools.Server" class="org.h2.tools.Server" scope="singleton" factory-method="createTcpServer"
      init-method="start" depends-on="org.h2.tools.Server-WebServer">
    <constructor-arg value="-tcp,-tcpAllowOthers,true,-tcpPort,9092"/>
</bean>

<bean id="org.h2.tools.Server-WebServer" class="org.h2.tools.Server" scope="singleton" factory-method="createWebServer" init-method="start">
    <constructor-arg value="-web,-webAllowOthers,true,-webPort,8082"/>
</bean>

<!-- notice that loading the Driver as a bean is unnecessary is most cases! u could safely remove this and the depends-on in the next bean -->

<bean id="H2DatabaseJDBCDriver" class="org.h2.Driver" scope="singleton" init-method="load" depends-on="org.h2.tools.Server"/>


<bean id="H2InMemoryDB"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    depends-on="org.h2.tools.Server">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:mem:appdb1;DB_CLOSE_DELAY=-1" />
    <!-- ;TRACE_LEVEL_FILE=3;TRACE_LEVEL_SYSTEM_OUT=3 -->
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

<bean id="H2InMemoryDBPool" class="org.apache.commons.pool.impl.GenericObjectPool">
    <!-- Two connections: InMemoryEntityManagerFactory and transactionManager -->
    <property name="minIdle" value="1"/>
    <property name="maxWait" value="10"/>
    <property name="maxActive" value="10"/>
    <property name="maxIdle" value="10"/>
    <property name="minEvictableIdleTimeMillis" value="300000"/>
    <property name="timeBetweenEvictionRunsMillis" value="60000"/>
</bean>

<bean id="H2InMemoryDBDSConnFactory" class="org.apache.commons.dbcp.DataSourceConnectionFactory">
    <constructor-arg><ref bean="H2InMemoryDB"/></constructor-arg>
</bean>

<bean id="H2InMemoryDBPoolableConnFactory" class="org.apache.commons.dbcp.PoolableConnectionFactory">
    <constructor-arg index="0"><ref bean="H2InMemoryDBDSConnFactory"/></constructor-arg>
    <constructor-arg index="1"><ref bean="H2InMemoryDBPool"/></constructor-arg>
    <constructor-arg index="2"><null/></constructor-arg>
    <constructor-arg index="3"><null/></constructor-arg>
    <constructor-arg index="4"><value>false</value></constructor-arg>
    <constructor-arg index="5"><value>true</value></constructor-arg>
</bean>

<bean id="pooledInMemoryDB" class="org.apache.commons.dbcp.PoolingDataSource" depends-on="H2InMemoryDBPoolableConnFactory">
    <constructor-arg><ref bean="H2InMemoryDBPool"/></constructor-arg>      
</bean>   



 <bean id="sessionFactory2"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
    p:dataSource-ref="pooledInMemoryDB">
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
    <property name="packagesToScan" value="********" />

</bean>

    <bean id="transactionManager2"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"
    p:sessionFactory-ref="sessionFactory" />

but its not work .

so my question is :

do i need to use 2 session factory or using dynamic data source switch ?

Thanks

I did find a typo that may cause you code to break.

Your second transactionmanager refers to the first sessionFactory. I think you want it to refer to the second transactionmanager. Try:

<bean id="transactionManager2"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"
    p:sessionFactory-ref="sessionFactory2" />

Regarding your question whether one has to use some sort of "dynamic data source switch" as described here , multiple data sources should just be fine.

i use

 <jdbc:embedded-database id="embeddedDatasource" type="DERBY">
    <jdbc:script location="classpath:test.sql"/>
 </jdbc:embedded-database>

instead

it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM