如何在SOA中配置JPA多个持久化单元
我正在使用 SOA 并且有多个 persistence.xml (每个组件都依赖于数据库)。我正在使用 Spring + JPA。代码示例如下:
在核心中:
Code:
public abstract class GenericJpaDAOImpl<T extends BaseEntity> implements GenericJpaDAO<T> {
protected abstract EntityManager getEntityManager();
}
在组件 SSO 中:
Code:
public class UserDAOImpl extends GenericJpaDAOImpl<User> implements UserDAO {
/* Any method specific to UserLogin */
@PersistenceContext(unitName = "sso", type = PersistenceContextType.TRANSACTION)
protected EntityManager entityManager;
@Override
protected EntityManager getEntityManager() {
return this.entityManager;
}
}
Persistence.xml
代码:
<persistence-unit name="sso" transaction-type="RESOURCE_LOCAL">
应用程序 XML
Code:
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name="dataSource" ref="ssoDataSource" />
<property name="persistenceUnitName" value="sso"></property>
</bean>
<bean id="ssoDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="<URL>"/>
<property name="user" value="<USER>"/>
<property name="password" value="<PASSWORD>"/>
</bean>
类似的组件:计费(如上)
我在 http://forum.springsource.org/showth...sistence-units 但这确实适用于预期的行为。就我而言,如果加载了用于计费的应用程序上下文 xml,则该组件的第一个 DAO 操作工作文件,但 SSO 组件不起作用。如果需要更多详细信息,请提出建议并告诉我。
I am using SOA and have multiple persistence.xml (each component dependent on DB). I am using Spring + JPA. Code sample as below:
In Core:
Code:
public abstract class GenericJpaDAOImpl<T extends BaseEntity> implements GenericJpaDAO<T> {
protected abstract EntityManager getEntityManager();
}
In Component SSO:
Code:
public class UserDAOImpl extends GenericJpaDAOImpl<User> implements UserDAO {
/* Any method specific to UserLogin */
@PersistenceContext(unitName = "sso", type = PersistenceContextType.TRANSACTION)
protected EntityManager entityManager;
@Override
protected EntityManager getEntityManager() {
return this.entityManager;
}
}
Persistence.xml
Code:
<persistence-unit name="sso" transaction-type="RESOURCE_LOCAL">
Application XML
Code:
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name="dataSource" ref="ssoDataSource" />
<property name="persistenceUnitName" value="sso"></property>
</bean>
<bean id="ssoDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="<URL>"/>
<property name="user" value="<USER>"/>
<property name="password" value="<PASSWORD>"/>
</bean>
Similarlly Component: Billing (as Above)
I got similar thread on http://forum.springsource.org/showth...sistence-units but this does work for expected behaviour. In my case If application context xml for Billing gets loaded first DAO operations works file for this component but SSO component does not work. Please suggest and let me know if more detail is required.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有多个数据源,则可以使用
@Qualifier
注释将 DAO 配置为针对特定会话,如下所示:If you have multiple data sources, you can configure your DAOs to target a specific Session using the
@Qualifier
annotation, as follows: