我有一个带有@Schedule标记方法的EJB,它将实体持久化到数据库。在这种方法中,当我在persist()之后调用EntityManager.flush()时,我得到javax.persistence.TransactionRequiredException: No transaction is currently active
AFAIK所有EJB方法都是事务性的,但即使我用@TransactionAttribute(REQUIRED)标记方法,我也会收到此错误。
当我通过EntityTransaction.begin()和commit()手动管理事务时,一切正常。
我在JDK 8u74,EclipseLink 2.6.0上使用Wildfly 10.0 Final。
这是我的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
<persistence-unit name="mainPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/datasources/ExampleDS</jta-data-source>
<class>com.example.MyEntity</class>
</persistence-unit>
</persistence>
MyEJB.java:
package com.example;
import javax.ejb.*;
import javax.ejb.Timer;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Startup
@DependsOn("AppUtils")
@Singleton
public class MyEJB {
@PersistenceContext(unitName = "mainPU")
private EntityManager em;
@Schedule(hour = "*", minute = "*", second = "*/20", info = "", persistent = false)
private void doStuff(Timer timer) {
MyEntity entity = new MyEntity("Test" + Math.random(), "Test value");
em.merge(entity);
em.flush();
}
}
答案 0 :(得分:0)
使用JTA时,它会将更改提交并刷新到数据库中并关闭当前事务。因此,当您执行em.flush()
时,交易已经关闭。