乔布斯内部的playframework手动jpa交易 [英] playframework manual jpa transactions inside the Jobs

查看:96
本文介绍了乔布斯内部的playframework手动jpa交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读:

我的问题是:

如果我想在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();

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

您会推荐什么?

解决方案

发现的内容:如果使用:

  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插件涉及一些后端魔术,因此最好在您的操作中使其接近于此)

I have read this:

And my question is:

If I want to manage my transactions manually in the Job, what should I do?

Now I have a code and use there "@NoTransaction" like was explained in the post above:

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

    ...  

    JobResult result = new JobResult();
     try {

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

But, but in the moment of beginning (begin()) the transaction I have still an error:

java.lang.IllegalStateException: Transaction already active

Thus, even if I use @NoTransaction the Job still starts its own transaction.

I can fix it like this:

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

But it does not seems a nice solution.

What would you recommend?

解决方案

What I found: If use:

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

instead of using all those JPA stuff, then everything works fine.

So, this is a solution for me. But I did not dive deep in that how the play! thinks/recognizes whether I use jpa manually or work though the plugin api .. and then decide. It looks like this, if look at startTx method:

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

The idea is that 'better to use' JPA-Plugin-approach when you want to manage transactions manually in Play (since JPA plugin is involved in some back-end magic, so better to be close to it in your actions)

这篇关于乔布斯内部的playframework手动jpa交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆