Hibernate 为什么两个事务管理器实例抛出异常?

Hibernate 为什么两个事务管理器实例抛出异常?,hibernate,jpa,spring-batch,Hibernate,Jpa,Spring Batch,我正在使用SpringBatch和JPA,任何时候我想保存一个bean,我的SpringBatch和JPA事务管理器都有以下问题。 每当我试图保存一个bean时,就会出现以下异常(整个堆栈跟踪): 其实信息太少了,就告诉我吧。 如果有人能帮我解决这个问题,我将非常感激 非常感谢 解决方案是在DAO类中采用正确的事务注释。 我曾经 ,这将导致异常。我改成这个: org.springframework.transaction.annotation.Transactional 现在一切正常 我想这

我正在使用SpringBatch和JPA,任何时候我想保存一个bean,我的SpringBatch和JPA事务管理器都有以下问题。 每当我试图保存一个bean时,就会出现以下异常(整个堆栈跟踪):

其实信息太少了,就告诉我吧。 如果有人能帮我解决这个问题,我将非常感激

非常感谢


解决方案是在DAO类中采用正确的事务注释。 我曾经

,这将导致异常。我改成这个:

org.springframework.transaction.annotation.Transactional 

现在一切正常

我想这里可以帮你:你能粘贴完整的stacktrace吗?问题在于bean注入,有两个相同类型的bean,所以spring不知道应该注入哪一个。您有一个
batchTransactionManager
和一个
applicationTransactionManager
尝试指定。但通常应该避免使用@Transactional for SB方法,因为事务管理是由SBOk完成的,我明白了。有两个事务管理器。我不得不选择这个:org.springframework.transaction.annotation.Transactional,而不是JPA事务管理器(javax.transaction.Transactional)。谢谢你@LucaBassoRici!你给了我一个很好的暗示
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:ws="http://jax-ws.dev.java.net/spring/core"
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://jax-ws.dev.java.net/spring/core
http://jax-ws.dev.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet
http://jax-ws.dev.java.net/spring/servlet.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd"
default-autowire="byName">

<context:annotation-config />

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

<jee:jndi-lookup id="prodDataSource" jndi-name="jdbc/mysqldb"
    expected-type="javax.sql.DataSource" />

<!-- JPA -->
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="appPersistenceUnit" />
    <property name="dataSource" ref="prodDataSource" />
    <property name="jpaDialect">
        <bean class="com.tk.servicetool.exports.CustomHibernateJpaDialect" />
    </property>
</bean>

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


<!-- Spring Batch -->
<bean id="jobRepository"
    class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" >
    <property name="transactionManager" ref="applicationTransactionManager" /> 
</bean>

<bean id="jobLauncher"
    class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"  
xmlns:task="http://www.springframework.org/schema/task"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd">

<bean id="batchTransactionManager"
    class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

<bean id="billrecordExporter" class="com.blang.services.exports.RrecordExporter" />

<batch:job id="RecordReadJob" job-repository="jobRepository">
    <batch:step id="RecordReadStep">
        <batch:tasklet ref="recordExporter"
            transaction-manager="batchTransactionManager" />
    </batch:step>
</batch:job>

<bean id="batchLauncher" class="com.blang.services.exports.BatchLauncher">
    <property name="jobLauncher" ref="jobLauncher" />
    <property name="job" ref="recordReadJob" />
</bean>
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.blang.bean.ExportedReport;

@Repository
public class ExportedReportDAO
{
@PersistenceContext
private EntityManager entityManager;
private static final Logger LOGGER = LoggerFactory.getLogger(DocumentDAO.class);

@Transactional
public void addReport(ExportedReport report) {
  entityManager.persist(report);
}

}
javax.transaction.Transactional
org.springframework.transaction.annotation.Transactional