Two entity managers in Spring

∥☆過路亽.° 提交于 2019-12-23 12:49:12

问题


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

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>

Then inside my dao:

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

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

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?


回答1:


Here i have hibernate with two different data sources and i use @Qualifier to specify the session factory like this:

@Autowired
@Qualifier(value="sessionFactoryOne")
private SessionFactory sessionFactoryOne;

@Autowired
@Qualifier(value="sessionFactoryTwo")
private SessionFactory sessionFactoryTwo;

then i get the session from this guys.

In xml config i have two dataSource, sessionFactory and transactionManager, one for each DB. Works fine ;)



来源:https://stackoverflow.com/questions/14181444/two-entity-managers-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!