mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#203: ENH: Add Transaction hook ... for preCommit(), postCommit(), postRollback()
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package com.avaje.tests.transaction;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.TransactionCallbackAdapter;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TestTransactionCallback extends BaseTestCase {
|
||||
|
||||
int countPreCommit;
|
||||
int countPostCommit;
|
||||
int countPreRollback;
|
||||
int countPostRollback;
|
||||
|
||||
@Test(expected = PersistenceException.class)
|
||||
public void test_noActiveTransaction() {
|
||||
|
||||
Ebean.register(new MyCallback());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_commitAndRollback() {
|
||||
|
||||
|
||||
Ebean.beginTransaction();
|
||||
Ebean.register(new MyCallback());
|
||||
Ebean.commitTransaction();
|
||||
|
||||
assertEquals(1, countPreCommit);
|
||||
assertEquals(1, countPostCommit);
|
||||
assertEquals(0, countPreRollback);
|
||||
assertEquals(0, countPostRollback);
|
||||
|
||||
Ebean.beginTransaction();
|
||||
Ebean.register(new MyCallback());
|
||||
Ebean.rollbackTransaction();
|
||||
|
||||
assertEquals(1, countPreCommit);
|
||||
assertEquals(1, countPostCommit);
|
||||
assertEquals(1, countPreRollback);
|
||||
assertEquals(1, countPostRollback);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = PersistenceException.class)
|
||||
public void test_withEbeanserver() {
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
server.register(new MyCallback());
|
||||
}
|
||||
|
||||
|
||||
class MyCallback extends TransactionCallbackAdapter {
|
||||
|
||||
@Override
|
||||
public void preCommit() {
|
||||
countPreCommit++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postCommit() {
|
||||
countPostCommit++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preRollback() {
|
||||
countPreRollback++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postRollback() {
|
||||
countPostRollback++;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user