将2个数据源注入spring + hibernate应用程序总是抛出没有事务正在进行中的错误

时间:2017-11-22 08:51:38

标签: java mysql spring hibernate jpa

我有Spring + JPA(Hibernate)项目,我连接到 MsSQL 数据库,现在我需要打开一个新连接,但这次它将是 MySQL 。我正在使用XML配置

<bean id="hibernateJpaVendorAdapter"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${spring.datasource.driverClassName}" />
   ....
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:dataSource-ref="dataSource" p:packagesToScan="com.wsg.admin.api.model"
    p:jpaVendorAdapter-ref="hibernateJpaVendorAdapter">

    <property name="jpaProperties">
        <props>
            <prop key="hibernate.cache.use_second_level_cache">false</prop>
            ....
        </props>
    </property>
    <property name="persistenceUnitName" value="dataSourcePU" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

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

<!-- Configure the MySQL connection -->
<bean id="enduserDataSource" class="org.apache.commons.dbcp2.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${enduser.db.driver}" />
    ....
</bean>

<bean id="enduserEntityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:dataSource-ref="enduserDataSource" p:packagesToScan="com.wsg.admin.api.model"
    p:jpaVendorAdapter-ref="hibernateJpaVendorAdapter">

    <property name="jpaProperties">
        <props>
            <prop key="hibernate.cache.use_second_level_cache">false</prop>
            ....
        </props>
    </property>
    <property name="persistenceUnitName" value="enduserDataSourcePU" />
</bean>

<tx:annotation-driven transaction-manager="enduserTransactionManager" />

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

然后我尝试使用片段

注入entityManager
@PersistenceContext(unitName="dataSourcePU")
EntityManager entityManager;

@PersistenceContext(unitName="enduserDataSourcePU")
EntityManager endUserEntityManager;

它仅适用于第一个数据库,但是当尝试在第二个数据库上保持时,它会返回错误

javax.persistence.TransactionRequiredException: no transaction is in progress

我尝试用@Transactional("transactionManager")一次注释方法(包含persist()调用),另一次用@Transactional(value = "enduserTransactionManager")注释,第二个entityManager总是抛出相同的异常

我试图将persist()调用分成不同的方法,并注释了两个方法@Transactional("transactionManager")@Transactional(value = "enduserTransactionManager")中的每一个但仍然得到相同的错误

@Transactional(value = "enduserTransactionManager")
private void createNewBrandMySQL(Brand newBrand) {

@Transactional("transactionManager")
public Integer createNewBrand(Brand newBrand) throws EntityDoesntExistException { 
//this method calls createNewBrandMySQL

1 个答案:

答案 0 :(得分:1)

修复了使用em

直接注入一个entityManagerFactory的问题
@PersistenceContext(unitName = "dataSourcePU")
EntityManager entityManager;

@Autowired
@Qualifier("enduserEntityManagerFactory")
EntityManagerFactory endUserEntityManagerFactory;

EntityManager endUserEntityManager;

@PostConstruct
public void init() {
    endUserEntityManager = endUserEntityManagerFactory.createEntityManager();
}