I have an EJB with a @Schedule marked method, that persists an entity to a database. Within this method, when I'm calling EntityManager.flush() after persist(), I'm getting javax.persistence.TransactionRequiredException: No transaction is currently active
AFAIK all EJB methods are transactional, but I'm getting this error even if I additionally mark the method with @TransactionAttribute(REQUIRED).
When I manage a transaction manually by EntityTransaction.begin() and commit(), everything works OK.
I'm using Wildfly 10.0 Final on JDK 8u74, EclipseLink 2.6.0.
Here is my 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();
}
}