如何在SOA中配置JPA多个持久化单元

发布于 2024-12-01 19:35:19 字数 2195 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路还长,别太狂 2024-12-08 19:35:19

如果您有多个数据源,则可以使用 @Qualifier 注释将 DAO 配置为针对特定会话,如下所示:

@Autowired
public MyDAOImpl(@Qualifier("someSessionFactory") SessionFactory sessionFactory) {
    setSessionFactory(sessionFactory);
}   

If you have multiple data sources, you can configure your DAOs to target a specific Session using the @Qualifier annotation, as follows:

@Autowired
public MyDAOImpl(@Qualifier("someSessionFactory") SessionFactory sessionFactory) {
    setSessionFactory(sessionFactory);
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文