hibernate应用程序总是抛出没有事务正在进行中的错误

hibernate应用程序总是抛出没有事务正在进行中的错误

本文介绍了将2个数据源注入spring + hibernate应用程序总是抛出没有事务正在进行中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have Spring + JPA (Hibernate) project, at which i connect to MsSQL database, now i need to open a new connection but this time it will be for MySQL. i am using XML configuration

<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

then i try to inject the entityManager using the fragment

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

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

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

it works only for the first Database, but when try to persist on the second it returns error

javax.persistence.TransactionRequiredException: no transaction is in progress

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

i tried to annotated the method (which contains persist() call) with @Transactional("transactionManager") one time, and @Transactional(value = "enduserTransactionManager") the other time, the second entityManager always throw the same exception

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

i tried to separate the persist() call into different methods, and annotated each of the two method @Transactional("transactionManager") and @Transactional(value = "enduserTransactionManager") but still get the same error

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

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


推荐答案

通过使用<$直接注入一个 em 来修复c $ c> entityManagerFactory

Fixed, by injecting one em directly an the other using entityManagerFactory

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

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

EntityManager endUserEntityManager;

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

这篇关于将2个数据源注入spring + hibernate应用程序总是抛出没有事务正在进行中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:47