0

我尝试使用以下配置配置数据源----> mysql另一个数据源---->内存中的h2(嵌入式):

    <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" />

但它不起作用。

所以我的问题是:

我需要使用 2 个会话工厂还是使用动态数据源开关?

谢谢

4

2 回答 2

0

我用

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

反而

有用

于 2013-02-05T00:53:15.270 回答
0

我确实发现了一个可能导致您代码中断的错字。

您的第二个事务管理器指的是第一个 sessionFactory。我认为您希望它引用第二个事务管理器。尝试:

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

关于您是否必须使用此处所述的某种“动态数据源开关”的问题,多个数据源应该没问题。

于 2013-02-01T07:56:47.100 回答