2016-04-24 111 views
1

我正在使用play framework和JPA。几乎没有消息传递给Akka Actor以进行异步处理。在异步过程中,我需要通过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) 

任何人有一个想法,JPA通过连接阿卡?

+0

JPA和akka已经注意到了彼此。我不会注释onReceive方法,在那里寻找麻烦。 – Snickers3192

+0

我通过使用JPA.withTransaction解决了这个问题,就像错误消息所述 – kopelitsa

回答

0

@Transactional是一个动作组合,它会只有工作在控制器。

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