如何仅将特定数据库用于托管操作

时间:2013-11-18 07:27:27

标签: java spring jpa mbeans

我们使用spring和jpa 2.0 我有托管bean,调用与客户端相同的方法。 我希望托管bean在从属DB上运行,客户端调用主数据库。

关于我们如何做到这一点的任何想法?

由于

Here is my xml file defining datasources :
     

                               

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <!-- connection pool datasource which supports also XA 2 phase commit -->
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>

    <property name="url" value="jdbc:mysql://localhost:3306/netoplay"/>
    <property name="username" value="root" />
    <property name="password" value="" />

    <property name="initialSize" value="10"/>
    <property name="maxActive" value="100"/> <!-- The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit. -->
    <property name="maxIdle" value="15"/> <!-- The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit. -->
    <property name="minIdle" value="10"/> <!-- The minimum number of connections that can remain idle in the pool, without extra ones being created, or zero to create none. -->
    <property name="timeBetweenEvictionRunsMillis" value="10000"/> <!-- the idle evicter thread evicts idle connections -->
    <property name="minEvictableIdleTimeMillis" value="60000"/> <!-- time a connection may be idle until it can be evicted -->

    <property name="validationQuery" value="/* ping */ SELECT 1"/>
    <property name="testOnBorrow" value="true"/> <!-- test the connection using the ping validationQuery before it is given to the application -->
    <property name="testWhileIdle" value="true"/> <!-- in addition to testOnBorrow, test connections while they are sitting idle -->

    <!-- If you enable "removeAbandoned" then it is possible that a connection is reclaimed by the pool because it is considered to be abandoned. This mechanism is triggered when (getNumIdle() < 2) and (getNumActive() > getMaxActive() - 3) -->
    <property name="removeAbandoned" value="true"/> <!-- Flag to remove abandoned connections if they exceed the removeAbandonedTimout. If set to true a connection is considered abandoned and eligible for removal if it has been idle longer than the removeAbandonedTimeout. Setting this to true can recover db connections from poorly written applications which fail to close a connection. -->
    <property name="removeAbandonedTimeout" value="300"/> <!-- Timeout in seconds before an abandoned connection can be removed. 300 seconds (5 minutes) is the default -->

</bean>

<bean id="dataSourceSlaveDB" class="org.apache.commons.dbcp.BasicDataSource"> <!-- connection pool datasource which supports also XA 2 phase commit -->
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>

    <property name="url" value="jdbc:mysql://localhost:3306/netoplay"/>
    <property name="username" value="root" />
    <property name="password" value="" />

    <property name="initialSize" value="10"/>
    <property name="maxActive" value="100"/> <!-- The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit. -->
    <property name="maxIdle" value="15"/> <!-- The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit. -->
    <property name="minIdle" value="10"/> <!-- The minimum number of connections that can remain idle in the pool, without extra ones being created, or zero to create none. -->
    <property name="timeBetweenEvictionRunsMillis" value="10000"/> <!-- the idle evicter thread evicts idle connections -->
    <property name="minEvictableIdleTimeMillis" value="60000"/> <!-- time a connection may be idle until it can be evicted -->

    <property name="validationQuery" value="/* ping */ SELECT 1"/>
    <property name="testOnBorrow" value="true"/> <!-- test the connection using the ping validationQuery before it is given to the application -->
    <property name="testWhileIdle" value="true"/> <!-- in addition to testOnBorrow, test connections while they are sitting idle -->

    <!-- If you enable "removeAbandoned" then it is possible that a connection is reclaimed by the pool because it is considered to be abandoned. This mechanism is triggered when (getNumIdle() < 2) and (getNumActive() > getMaxActive() - 3) -->
    <property name="removeAbandoned" value="true"/> <!-- Flag to remove abandoned connections if they exceed the removeAbandonedTimout. If set to true a connection is considered abandoned and eligible for removal if it has been idle longer than the removeAbandonedTimeout. Setting this to true can recover db connections from poorly written applications which fail to close a connection. -->
    <property name="removeAbandonedTimeout" value="300"/> <!-- Timeout in seconds before an abandoned connection can be removed. 300 seconds (5 minutes) is the default -->

</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="punit"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL"/>
            <!-- <property name="databasePlatform" value="${hibernate.dialect}"/> -->
            <property name="showSql" value="false"/>
            <property name="generateDdl" value="false"/>
            <!--  <property name="hibernate.connection.autocommit" value="false"/> -->
        </bean>
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.connection.autocommit" value="false" />
        </map>
    </property>
</bean>

这是我的persistence.xml文件:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">    
<persistence-unit name="punit"> <!-- transaction-type="JTA" JTA is the defailt --> 
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>           
    </properties>
</persistence-unit>

1 个答案:

答案 0 :(得分:1)

背景:当您使用自动事务管理时,Spring最终会(通常在您进行第一次数据库访问时)创建会话。此时,您需要在两个数据库之间切换。

因此,您需要做的是禁用自动事务管理并将其替换为手动管理。找到调用客户端中代码的位置以及调用mbeans的位置。用手动事务管理包装并创建适当的会话。

根据代码的工作方式,这可能需要做很多工作。然后诀窍是确定需要更多工作的一方。想象一下,你有500个调用mbeans的地方,但你可以为所有客户端调用安装一个HTTP过滤器,因为它使用HTTP。

因此,您创建了一个过滤器,用于为客户端数据库创建会话并将其配置到HTTP堆栈中。对于mbeans,您使用Spring配置的自动事务管理来连接到从DB。

由于正常的事务管理在会话已经存在时没有执行任何操作,因此所有代码都将转到正确的数据库。

注意:在此方案中,您无法使用自动缓存。如果这样做,两个数据库中的对象将最终进入缓存,真正可怕的事情将开始发生。

相关问题