【发布时间】:2013-11-18 07:27:26
【问题描述】:
我们使用 spring 和 jpa 2.0 我管理的 bean 调用了与客户端相同的方法。 我希望托管 bean 在从数据库上运行,客户端在主数据库上调用。
关于我们如何做到这一点的任何想法?
谢谢
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>
【问题讨论】:
-
嗯...很容易降低问题分数,有点难以说出原因或回答问题。
-
你试过什么?您的 persistence.xml 定义在哪里?显然,JPA 需要一个持久性单元,您可以拥有一个用于主数据库和一个用于从数据库。很好,很好地批评人们投反对票,但您在问题上花费的时间最少。
-
我可以在这里写我的 persistence.xml 你会看到定义了两个数据源,但我的问题是如何在不修改我的所有方法的情况下告诉使用哪个数据源,将数据源作为参数或添加一个 bean在我所有的课程中精确使用的数据源。
-
但那些是数据源而不是 persistence.xml。在持久化单元中,您可以指定相应单元将使用的 jtaDataSource / nonJtaDataSource(取决于是否使用 JTA)(因此将它们设置为“dataSource”或“dataSourceSlaveDB”)。您没有为您的客户端或服务器提供关于他们如何获得 EMF 的代码