【问题标题】:How do I connect with play persistence (JPA) in Akka actors?如何在 Akka actor 中连接播放持久性 (JPA)?
【发布时间】: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


【解决方案1】:

@Transactional 是一个动作组合,它将在控制器中工作。

你需要在你的actor中注入JPAApi并使用jpaApi.withTransaction方法来创建/附加EntityManager到线程并将你的代码包装在一个事务中。

【讨论】:

    猜你喜欢
    • 2016-03-07
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多