Play 2.1.1: Unable to roll back transaction with ebean orm

I have a problem understanding how to work with ebean transactions in game 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 get the error: PersistenceException: is the existing transaction still active?

Also I'm trying to do 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 get updated values. Also in the last example, I will try rollback like this:. Ebean.currentTransaction () end ();

But get a NullPointerException error.

Can someone point me to a working example with transactions? Or write some example in the comments.

Thank.

UPDATE

As a result, we found a 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();

}
0
source share
2

. .

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

, , - .

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

0

Ebean.beginTransaction(); Ebean.rollbackTransaction(); Ebean.commitTransaction();.... @Transactional Ebean.execute(txScope, TxRunnable().

, @Transactional / , Ebean.execute(txScope, TxRunnable() / .

, , Ebean.execute(txScope,... .

+1

Source: https://habr.com/ru/post/1534524/


All Articles