春季有两名实体经理 [英] Two entity managers in Spring

查看:70
本文介绍了春季有两名实体经理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经以编程方式做到这一点-正在创建两个具有不同数据源的实体管理器,并为它们提供不同的服务。

I had done this programmatically at some point - that is creating two entity managers with different data sources and providing them for different services.

但是现在我正在构建一个使用Spring的webApp。我要让两个实体管理器负责单独的数据库。因此,就我而言,DAO的一半将使用 emNumber1 ,而后一半将使用 emNumber2 。哦,还有完全不同的持久性单元,因为我只需要从 DB1 中读取数据,然后对其进行处理并将该数据存储在完全不同的实体中的 DB2 中。

However now I am building a webApp using Spring. Thing is that I want to have two entity managers responsible for separate databases. So in my case, half of DAO's would be using emNumber1, and second half would be using emNumber2. Oh and totally different persistence units as I only need to read data from DB1 and then process it and store this data in DB2 in totally different entities.

我很清楚这个问题已经存在并且已经存在了一段时间,但是形状和形式各不相同,所以我之所以提出这个问题,只是因为我不理解伴随着提出的解决方案这些谷歌论坛的线程,或者它们不适用于我的情况。在相同的情况下,我无法理解该解决方案或如何在我的情况下应用该解决方案,因此对于是否需要通过链接来自此站点的解决方案来解决该问题感到非常抱歉。

I am well aware that this problem is and has been going around for sometime now but in different shapes and forms and so I am composing this question only because I could not understand the solutions that were presented withing those googled-up forum threads or they were not applicable to my case. In same cases I could not understand the solution or how to apply it in my case so really sorry for redundant question if it were to be solved by linking a solution from this very own site.

这是我认为适合配置单个事务管理器的方式

Here is how I find fit to configure a single transaction manager

<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="com.mysql.jdbc.Driver"
      p:url="jdbc:mysql://localhost:3306/db"
      p:username="dbuser"
      p:password="dbuser"/>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="pu1"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true"/>
            <property name="generateDdl" value="true"/>
            <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
        </bean>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="dataSource"/>
</bean>

然后在我的dao内:

public class SimpleDaoImpl implements SimpleDao {
@PersistenceContext
private EntityManager entityManager;

    /* methods that use entity manager for transactions and data detcing */
}

因此,如果两个事务管理器具有不同的数据源,那么将如何指定要向哪个DAO注入哪个实体管理器,或者在春季这是不可能的?

So in case of two transaction managers with different data sources how would one specify what entity manager to inject into which DAO or is that impossible in Spring?

推荐答案

您可以执行@PersistenceContext(unitName = pu1)

You can do @PersistenceContext(unitName="pu1")

这篇关于春季有两名实体经理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆