【发布时间】:2016-04-24 17:39:36
【问题描述】:
我正在使用播放框架和 JPA。很少有消息传递给 Akka Actor 进行异步处理。在异步进程中,我需要通过 JPA 连接我的数据库。
public class OrderCreation extends UntypedActor {
private EntityManagerFactory emFact = null;
private ActorSelection provisioner;
@Transactional(readOnly = false)
@Override
public void onReceive(Object order) throws Exception {
//HERE I need to do JPA related transactions
}
@Override
public void postStop() throws Exception {
}
@Override
public void preStart() throws Exception {
provisioner =getContext().actorSelection("/user/OrderProvisioner");
emFact = Persistence.createEntityManagerFactory("test-data-play");
}
}
我收到了这个错误
[akka://application/user/OrderCreation] No EntityManager bound to this thread. Try wrapping this call in JPA.withTransaction, or ensure that the HTTP context is setup on this thread.
java.lang.RuntimeException: No EntityManager bound to this thread. Try wrapping this call in JPA.withTransaction, or ensure that the HTTP context is setup on this thread.
at play.db.jpa.JPA.em(JPA.java:58)
有人想通过 Akka 连接 JPA 吗?
【问题讨论】:
-
JPA 和 akka 之间没有任何联系。我不会注释 onReceive 方法,在那里自找麻烦。
-
我通过使用 JPA.withTransaction 解决了这个问题,正如错误消息所说
标签: java jpa playframework-2.0 akka transactional