TomEE 7.0.0 上 ManagedScheduledExecutorService 任务内的事务?

Transactions inside tasks of ManagedScheduledExecutorService on TomEE 7.0.0?

上下文

我想使用 ManagedScheduledExecutorService 安排重复的后台任务。我通过 Instance 获得 Runnables/Callables,所以我的任务中有可用的注入功能。

我的应用程序在 TomEE 7.0.0-SNAPSHOT webprofile.

上运行

问题

由于这些任务正在访问数据库,因此它们需要一个事务。但是,在 Runnable/Callable 中没有交易处于活动状态。

用户交易

documentation for ManagedScheduledExecutorService 表示

If a transaction is required, use a javax.transaction.UserTransaction instance. A UserTransaction instance is available (...) by requesting an injection of a UserTransaction object using the Resource annotation.

但是,注入的

@Resource
private UserTransaction userTransaction;

调用任务时是null

无状态 bean

我采用的另一种方法是将无状态 EJB 注入到我的任务中,希望这会为我创建一个事务。

这导致启动时出现以下异常:

SEVERE: CDI Beans module deployment failed
java.lang.IllegalStateException: no interface to proxy for ejb StatelessEjb, is this is a MDB maybe you shouldn't use a scope?
    at org.apache.openejb.cdi.CdiEjbBean.createEjb(CdiEjbBean.java:252)
    at org.apache.openejb.cdi.CdiPlugin.getSessionBeanProxy(CdiPlugin.java:224)
    at org.apache.webbeans.container.BeanManagerImpl.getEjbOrJmsProxyReference(BeanManagerImpl.java:951)
    at org.apache.webbeans.container.BeanManagerImpl.getReference(BeanManagerImpl.java:777)
    at org.apache.webbeans.container.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:651)
    at org.apache.webbeans.inject.AbstractInjectable.inject(AbstractInjectable.java:111)
    at org.apache.webbeans.inject.InjectableConstructor.createParameters(InjectableConstructor.java:109)
    at org.apache.webbeans.inject.InjectableConstructor.doInjection(InjectableConstructor.java:72)
    at org.apache.webbeans.portable.InjectionTargetImpl.newInstance(InjectionTargetImpl.java:190)
    at org.apache.webbeans.portable.InjectionTargetImpl.produce(InjectionTargetImpl.java:173)
    at org.apache.webbeans.portable.AbstractProducer.produce(AbstractProducer.java:172)
    at org.apache.webbeans.component.AbstractOwbBean.create(AbstractOwbBean.java:127)
    at org.apache.webbeans.component.ManagedBean.create(ManagedBean.java:67)
    at org.apache.webbeans.context.DependentContext.getInstance(DependentContext.java:68)
    at org.apache.webbeans.context.AbstractContext.get(AbstractContext.java:124)
    at org.apache.webbeans.container.BeanManagerImpl.getReference(BeanManagerImpl.java:785)
    at org.apache.webbeans.inject.instance.InstanceImpl.create(InstanceImpl.java:306)
    at org.apache.webbeans.inject.instance.InstanceImpl.get(InstanceImpl.java:123)
(...)

测试用例

我创建了一个小 test case project on Github。它包含两个分支,说明了上述问题。

问题

使用 CDI 获取它应该可以修复它:

@Inject
private UserTransaction userTransaction;

编辑:@Resource 案例的问题已修复:https://issues.apache.org/jira/browse/TOMEE-1672