播放2.1.1:无法回退与ebean orm的事务 [英] play 2.1.1: Unable to rollback transaction with ebean orm

查看:199
本文介绍了播放2.1.1:无法回退与ebean orm的事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解如何在游戏2.1.1下处理ebean交易时遇到问题.

I have problem with understanding how to work with ebean transactions under play 2.1.1.

    Ebean.execute(txScope, new TxRunnable() {

        public void run() {

            Ebean.beginTransaction();
            System.out.println("[**] : " + Ebean.currentTransaction());
            User user = Ebean.find(User.class, 22);
            user.setPassword("qweqwe125");
            Ebean.save(user);

            user = Ebean.find(User.class, 22);
            user.setPassword("qweqwe126");
            Ebean.rollbackTransaction();
            // or other case
            //Ebean.currentTransaction().rollback();
        }

但是在这种情况下,我会收到错误消息:PersistenceException:现有事务仍处于活动状态?

But in this case I receive error: PersistenceException: The existing transaction still active?

我也尝试做类似的事情:

Also I've try to make something like:

@Transactional(type=TxType.REQUIRES_NEW, isolation = TxIsolation.SERIALIZABLE)
public static void transactional2() {
    User user = User.query.getById(22l);
    user.setPassword("qweqwe123");
    user.save();

    Ebean.endTransaction();
}

在这种情况下,我收到更新的值.同样在上一个示例中,我尝试以这种方式回滚: Ebean.currentTransaction().end();

In this case I receive updated values. Also in last example I've try rollback in this way: Ebean.currentTransaction().end();

但是会收到NullPointerException错误.

But receive NullPointerException error.

有人可以指出我在交易中可行的例子吗?或在评论中写一些例子.

Could some one point me to workable example with transactions? Or write some example in comments.

谢谢.

更新

最终找到了解决方法:

public static void transactional2() {
    com.avaje.ebean.Ebean.beginTransaction();

    User user = User.query.getById(22l);
    user.setPassword("qweqwe123");
    user.save();

    com.avaje.ebean.Ebean.rollbackTransaction();
    // OR: com.avaje.ebean.Ebean.commitTransaction();

}

推荐答案

最终找到了解决方案. 为我工作.

Eventually have found solution. Works for me.

public static void transactional2() {
    com.avaje.ebean.Ebean.beginTransaction();

    User user = User.query.getById(22l);
    user.setPassword("qweqwe123");
    user.save();

    com.avaje.ebean.Ebean.rollbackTransaction();
    // OR: com.avaje.ebean.Ebean.commitTransaction();
}

如果此解决方案有问题,请添加您的评论.

Please add you comments if something wrong with this solution.

Ebean文档示例: http://www.avaje.org/ebean/introtrans_begin.html

Ebean documentation example: http://www.avaje.org/ebean/introtrans_begin.html

这篇关于播放2.1.1:无法回退与ebean orm的事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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