2016-04-24 56 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到線程並在事務中包裝您的代碼。