play 2.1.1: Unable to rollback transaction with ebean orm
Asked Answered
S

2

1

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();
        }

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();
}

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

But receive NullPointerException error.

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

Thanks.

UPDATE

Eventually have found solution:

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();

}
Skep answered 12/6, 2013 at 21:56 Comment(0)
S
0

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 documentation example: http://www.avaje.org/ebean/introtrans_begin.html

Skep answered 10/7, 2013 at 10:50 Comment(4)
As said Rob, you shouldn't use the begin, rollback, commit directly. The problem you're pointing is a bug of Ebean : github.com/ebean-orm/avaje-ebeanorm/pull/44. Try to upgrade your version of PlayTinderbox
You mean upgrade with Play 2.2.2, the last one from 1-st of March ?Subcontract
A) If you use beginTransaction() ... then you SHOULD have a try finally block with endTransaction() in the finally.Epideictic
B) You can use beginTransaction(), rollbackTransaction() etc OR @Transactional but not both together (as was the case in the original code example).Epideictic
E
1

In short you shouldn't use Ebean.beginTransaction(); Ebean.rollbackTransaction(); or Ebean.commitTransaction(); .... with either @Transactional or Ebean.execute(txScope, new TxRunnable().

So the enhancement of the @Transactional method handles the commit/rollback for you and similarly the Ebean.execute(txScope, new TxRunnable() handles the commit/rollback for you.

If you want to fail a transaction in say Ebean.execute(txScope, ... then throw an exception.

Epideictic answered 21/6, 2013 at 1:6 Comment(3)
Please correct me if I'm wrong. I've try to throw exception inside @Transactional controller action. Unfortunately all sql updates call was committed. Could you please point me to working example for play 2.1.1. Thank you for you comment.Skep
This is not working. I am using EbeanServer.execute(TxScope, TxRunnable); and it does not rollback on trown exceptions.Valine
David, put an example (e.g. failing test) of your issue somewhere. You could base it off github.com/ebean-orm-examples/example-minimal ...Epideictic
S
0

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 documentation example: http://www.avaje.org/ebean/introtrans_begin.html

Skep answered 10/7, 2013 at 10:50 Comment(4)
As said Rob, you shouldn't use the begin, rollback, commit directly. The problem you're pointing is a bug of Ebean : github.com/ebean-orm/avaje-ebeanorm/pull/44. Try to upgrade your version of PlayTinderbox
You mean upgrade with Play 2.2.2, the last one from 1-st of March ?Subcontract
A) If you use beginTransaction() ... then you SHOULD have a try finally block with endTransaction() in the finally.Epideictic
B) You can use beginTransaction(), rollbackTransaction() etc OR @Transactional but not both together (as was the case in the original code example).Epideictic

© 2022 - 2024 — McMap. All rights reserved.