【问题标题】:playframework manual jpa transactions inside the Jobs作业中的 playframework 手动 jpa 事务
【发布时间】:2013-01-01 07:31:56
【问题描述】:

我已阅读this

我的问题是:

如果我想在 Job 中手动管理我的交易,我应该怎么做?

现在我有一个代码并在那里使用“@NoTransaction”,就像上面帖子中解释的那样:

@NoTransaction
@Override
public JobResult doJobWithResult() throws Exception {

    ...  

    JobResult result = new JobResult();
     try {

        //MyEntity.em().getTransaction().commit();
        MyEntity.em().getTransaction().begin();
....

但是,但是在交易开始的那一刻(begin())我还是报错了:

java.lang.IllegalStateException: 事务已经激活

因此,即使我使用@NoTransaction,Job 仍然会启动它自己的事务。

我可以这样解决:

   MyEntity.em().getTransaction().commit(); // commit previous started transaction
   MyEntity.em().getTransaction().begin();

但这似乎不是一个好的解决方案。

你会推荐什么?

【问题讨论】:

    标签: jpa transactions playframework


    【解决方案1】:

    我发现了什么:如果使用:

      JPAPlugin.startTx(false); // to start transaction
      JPAPlugin.closeTx(false); // to close and commit transaction
      JPAPlugin.closeTx(true); //  to roll-back transaction 
    

    而不是使用所有这些 JPA 东西,然后一切正常。

    所以,这对我来说是一个解决方案。但我并没有深入了解那是怎么玩的!认为/识别我是手动使用 jpa 还是通过插件 api 工作 .. 然后决定。如果看 startTx 方法,它看起来像这样:

      if (autoTxs) {
            manager.getTransaction().begin();
        }
        JPA.createContext(manager, readonly);  
    

    这个想法是当你想在 Play 中手动管理事务时“更好地使用”JPA-Plugin-方法(因为 JPA 插件涉及一些后端魔法,所以最好在你的操作中接近它)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-25
      相关资源
      最近更新 更多