开发者

Hibernate JPA and Spring javax.persistence.TransactionRequiredException: no transaction is in progress

When I call:

entityManager.flush()

I get the excep开发者_JAVA技巧tion mentioned in the title.

I am using Hibernate JPA.


After encountering this problem myself and spending a few hours trying to get it resolved I finally found a reason for it: Spring has a bug and can't maintain transactions with @Transactional annotation if the same class has @Service annotation for the means of autowiring.

Once the @Service annotation was removed from the service class in question, and an appropriate bean was declared in the XML config:

<bean id="myService" class="com.example.myapp.service.MyServiceImpl" />

the problem is gone.

Check this JIRA bug for more details.


Ensure that you have an active transaction when this statement executes. If you are using JPA use EntityManager.getTransaction().begin(). This is assuming that you are using JPA outside a JTA transaction scope.

If you are running the application inside a container with JTA support you can also use JTA UserTransaction to manage transactions.


My Problem was to do with the way that I setup the <tx:annotation-driven/> Element in my context definition -

Originally I had load time weaving enabled (not knownley) that read <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/> and by simply removing the 2nd attribute - everything worked (took 2 hours of head banging though). I believe the 2nd element relates to the @Configurable sterotype but can let other (smarter) people explain the difference & why one would work & the other does does not.. Hope this helps...

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


I had this problem, just add @Transacctional annotation not only on the method, also in the class together with your @Service annotation.

for example:

@Service
@Transactional
public class MyService {

}


Spring 4.3.1 / Hibernate 4.2.21

My configuration was 100% Java code with no hibernate or spring xml documents (eg context.xml, persistence.xml etc). The issue was the EntityManagerFactory I was passing to the TransactionManager, see the below configuration in the transactionManager method.

@Configuration
@EnableTransactionManagement
public class HibernateConfiguration2 {

@Bean
public DataSource dataSource() {
    return ...; // Build a basic datasource
}

@Bean
@Autowired
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactory.setDataSource(dataSource);
    entityManagerFactory.setPackagesToScan("nz.co.mark");
    entityManagerFactory.setPersistenceProviderClass(org.hibernate.ejb.HibernatePersistence.class);

    return entityManagerFactory;
}

@Bean
@Autowired
public EntityManager entityManager(LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean) {
    EntityManager em = localContainerEntityManagerFactoryBean.getNativeEntityManagerFactory().createEntityManager();
    em.setFlushMode(FlushModeType.AUTO);
    return em;
}

@Bean
@Autowired
public JpaTransactionManager transactionManager(LocalContainerEntityManagerFactoryBean emf) throws Exception {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf.getObject());
    // The below line would generate javax.persistence.TransactionRequiredException: no transaction is in progress
    // transactionManager.setEntityManagerFactory(emf.getNativeEntityManagerFactory());
    return transactionManager;
}


Ensure that you have an active transaction when this statement executes. If you are using JPA use EntityManager.getTransaction().begin(). This is assuming that you are using JPA outside a JTA transaction scope.


I had the same problem... spent some hours until I found the reason finally. It was just one line of code that caused the exception in my case...

In my mvc-core-config.xml the following line was the reason:

<context:component-scan base-package="com.my.package.application" />

After I changed it as follows, everything worked again:

<context:component-scan base-package="com.my.package.application.controller" />

So I guess the scanning of all my application packages instead of just my @Controller classes lead to the problem like @harshal-waghmare mentioned in his post to another answer.


Please make sure that your handler method is declared as public

@Transactional 
@RequestMapping('/test')
public String doTest() {
    // do your stuff here 
    return 'testview';
}


Make sure that your spring configuration includes the following line:

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

mode can be either proxy or aspectj and transaction-manager has to point to your transaction manager been.


Same was happening to me using spring 3.0.0 / 3.0.3. Data was persisted in MySQL from JUnit but not from the tomcat server. After so much work I gave up on RESOURCE_LOCAL for JTA.

This worked for me http://erich.soomsam.net/2007/04/24/spring-jpa-and-jta-with-hibernate-and-jotm/ It uses JTA and depends on JOTM.


I did all the thing as a following. My problems was with "import" tag, there are several context root like servlet-context and root-context which are not dependent on each other. It becomes clear with Spring Explorer view in STS. No JTA for Tomcat.

My advice would be universal: run Pet Clinic on your environment , How to run Spring 3.0 PetClinic in tomcat with Hibernate backed JPA or generate with Roo stub of application and try to compare your configs with referenced.


For JBoss 4.0 and Hibernate, I fixed this problem by adding some transaction manager properties to my EntityManagerFactoryBean definition:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="xaDs" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory
            </prop>
            <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup
            </prop>
        </props>
    </property>

I found the soluton on this message board thread.


I have finally fixed this error by adding

<tx:annotation-driven mode="aspectj" transaction-manager="yourTransactionManager" />

into my application-context.xml

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜