Open jnye opened 9 years ago
As a work around you can split the @Schedule class and the @Transactional class and it will work.
package services;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import ninja.scheduler.Schedule;
import java.util.concurrent.TimeUnit;
@Singleton
public class TestScheduledAction {
@Inject
private TestAction testAction;
@Schedule(initialDelay = 0, delay = 60, timeUnit = TimeUnit.SECONDS)
public void run() {
testAction.doSomething();
}
}
package services;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.persist.Transactional;
import javax.persistence.EntityManager;
@Singleton
public class TestAction {
@Inject
private Provider<EntityManager> entityManagerProvider;
@Transactional
public void doSomething() {
if (!entityManagerProvider.get().getTransaction().isActive()) {
throw new RuntimeException("No transaction active!");
}
}
}
I meet the same question, someone would help us?
@Transactional
does not work on private
methods afaik.
Using version 5.2.1 the following throws an exception. I've not tested earlier versions.
Also, if I place
@Transactional
on the run method the scheduler never calls it.