mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Refactor move tests from ebean-core to ebean-test
This commit is contained in:
1 parent
0b00a14fd3
commit
eab56d323b
1650 files changed
+185
-147
No files matched your search
@@ -0,0 +1,103 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import io.ebean.config.dbplatform.IdType;
|
||||
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.m2m.MnyB;
|
||||
import org.tests.model.m2m.MnyTopic;
|
||||
import org.tests.model.m2m.Role;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestBatchModelFlush extends BaseTestCase {
|
||||
|
||||
@SuppressWarnings("static-access")
|
||||
@Transactional(batchSize = 50)
|
||||
@Test
|
||||
public void insert() {
|
||||
|
||||
new MnyB("TestBatchModelFlush_0").save();
|
||||
new MnyB("TestBatchModelFlush_1").save();
|
||||
|
||||
MnyB bean = new MnyB("TestBatchModelFlush_2");
|
||||
bean.save();
|
||||
bean.db().currentTransaction().flush();
|
||||
|
||||
MnyB bean2 = new MnyB("TestBatchModelFlush_3");
|
||||
bean2.save();
|
||||
bean2.db().flush();
|
||||
|
||||
new MnyB("TestBatchModelFlush_4").save();
|
||||
DB.flush();
|
||||
|
||||
// the rest is flushed on commit
|
||||
new MnyB("TestBatchModelFlush_5").save();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleTopLevel_expect_singleFlush() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// 2 unrelated "top level" beans being persisted
|
||||
MnyB m0 = new MnyB("BatchMultipleTop_0");
|
||||
MnyB m1 = new MnyB("BatchMultipleTop_1");
|
||||
Role r0 = new Role("Role_0");
|
||||
Role r1 = new Role("Role_1");
|
||||
|
||||
MnyTopic t0 = new MnyTopic("MnyTopic_0");
|
||||
MnyTopic t1 = new MnyTopic("MnyTopic_1");
|
||||
|
||||
try (Transaction transaction = DB.beginTransaction()) {
|
||||
transaction.setBatchMode(true);
|
||||
|
||||
m0.save();
|
||||
DB.save(r0);
|
||||
DB.save(t0);
|
||||
DB.save(t1);
|
||||
|
||||
m1.save();
|
||||
DB.save(r1);
|
||||
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
// DEBUG io.ebean.SUM - txn[1001] BatchControl flush [MnyB:100 i:2, Role:101 i:2, MnyTopic:102 i:2]
|
||||
|
||||
assertThat(sql).hasSize(9);
|
||||
|
||||
// first saved to batch - (depth 100)
|
||||
assertSql(sql.get(0)).contains("insert into mny_b");
|
||||
if (idType() == IdType.IDENTITY) {
|
||||
assertSql(sql.get(1)).contains(" -- bind(BatchMultipleTop_0");
|
||||
assertSql(sql.get(2)).contains(" -- bind(BatchMultipleTop_1");
|
||||
}
|
||||
// second saved to batch - (depth 101)
|
||||
assertThat(sql.get(3)).contains("insert into mt_role");
|
||||
assertThat(sql.get(4)).contains(" -- bind(");
|
||||
assertThat(sql.get(5)).contains(" -- bind(");
|
||||
// third saved to batch - (depth 102)
|
||||
assertThat(sql.get(6)).contains("insert into mny_topic");
|
||||
if (idType() == IdType.IDENTITY) {
|
||||
assertThat(sql.get(7)).contains(" -- bind(MnyTopic_0");
|
||||
assertThat(sql.get(8)).contains(" -- bind(MnyTopic_1");
|
||||
}
|
||||
|
||||
DB.delete(t0);
|
||||
DB.delete(t1);
|
||||
DB.delete(r0);
|
||||
DB.delete(r1);
|
||||
DB.delete(m0);
|
||||
DB.delete(m1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tests.model.basic.UTDetail;
|
||||
import org.tests.model.basic.UTMaster;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestBatchPersistCascade extends BaseTestCase {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(TestBatchPersistCascade.class);
|
||||
|
||||
@Test
|
||||
@IgnorePlatform(Platform.HANA)
|
||||
public void test() {
|
||||
|
||||
Database ebeanServer = DB.getDefault();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Transaction txn = ebeanServer.beginTransaction();
|
||||
try {
|
||||
txn.setBatchMode(true);
|
||||
logger.info("start ------------");
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
UTMaster master = createMaster(i);
|
||||
logger.info("save ------------ {}", i);
|
||||
ebeanServer.save(master);
|
||||
//txn.flushBatch();
|
||||
}
|
||||
|
||||
logger.info("commit ------------");
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertTrue(loggedSql.size() > 2);
|
||||
|
||||
|
||||
testUpdates();
|
||||
|
||||
}
|
||||
|
||||
private void testUpdates() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
List<UTMaster> list = server.find(UTMaster.class).fetch("details").findList();
|
||||
|
||||
Transaction txn = server.beginTransaction();
|
||||
try {
|
||||
txn.setBatchMode(true);
|
||||
txn.setBatchOnCascade(true);
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
UTMaster master = createMaster(i + 500);
|
||||
logger.info("save ------------ {}", i);
|
||||
server.save(master);
|
||||
}
|
||||
|
||||
logger.info("starting updates ------------ ");
|
||||
|
||||
UTMaster lastMaster = null;
|
||||
for (UTMaster utMaster : list) {
|
||||
utMaster.setName(utMaster.getName() + " + mod");
|
||||
List<UTDetail> details = utMaster.getDetails();
|
||||
for (UTDetail detail : details) {
|
||||
detail.setQty(detail.getQty() + 7);
|
||||
detail.setName(detail.getName() + " + foo");
|
||||
}
|
||||
|
||||
server.save(utMaster);
|
||||
lastMaster = utMaster;
|
||||
}
|
||||
|
||||
logger.info("starting some inserts ------------ ");
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
UTMaster master = createMaster(i + 1000);
|
||||
logger.info("save ------------ {}", i);
|
||||
server.save(master);
|
||||
if (i == 1) {
|
||||
logger.info("save lastMaster ------------ ");
|
||||
lastMaster.setName("mod");
|
||||
server.save(lastMaster);
|
||||
}
|
||||
}
|
||||
|
||||
logger.info("commit ------------ ");
|
||||
|
||||
server.commitTransaction();
|
||||
} finally {
|
||||
server.endTransaction();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private UTDetail createUTDetail(String master, int count) {
|
||||
UTDetail detail = new UTDetail();
|
||||
detail.setName(master + "-" + count);
|
||||
detail.setAmount(50d);
|
||||
detail.setQty(count);
|
||||
return detail;
|
||||
}
|
||||
|
||||
private UTMaster createMaster(int count) {
|
||||
|
||||
String name = "master" + count;
|
||||
|
||||
UTMaster m0 = new UTMaster();
|
||||
m0.setName(name);
|
||||
for (int i = 0; i < 5; i++) {
|
||||
m0.addDetail(createUTDetail(name, i));
|
||||
}
|
||||
return m0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import org.tests.model.m2m.MnyB;
|
||||
import org.tests.model.m2m.MnyC;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestBeanStateReset extends BaseTestCase {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(TestBeanStateReset.class);
|
||||
|
||||
@Test
|
||||
public void resetForInsert() {
|
||||
|
||||
// setup to fail foreign key constraint
|
||||
MnyC c = new MnyC();
|
||||
c.setId(Long.MAX_VALUE);
|
||||
|
||||
MnyB b = new MnyB();
|
||||
b.getCs().add(c);
|
||||
|
||||
try {
|
||||
// inserts of b succeeds but intersection insert fails FK check on c
|
||||
b.save();
|
||||
|
||||
} catch (PersistenceException e) {
|
||||
logger.info("expected error " + e.getMessage());
|
||||
|
||||
DB.beanState(b).resetForInsert();
|
||||
b.getCs().clear();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
b.setName("mod");
|
||||
b.save();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertSql(sql.get(0)).contains("insert into mny_b (id, name, version, when_created, when_modified, a_id) values (");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void alternativeVia_persistCascadeOff_with_commitAndContinue() {
|
||||
|
||||
// setup to fail foreign key constraint when using cascade save
|
||||
MnyC c = new MnyC();
|
||||
c.setId(Long.MAX_VALUE);
|
||||
|
||||
MnyB b = new MnyB();
|
||||
b.getCs().add(c);
|
||||
|
||||
Transaction transaction = DB.beginTransaction();
|
||||
try {
|
||||
// turn off cascade ...
|
||||
transaction.setPersistCascade(false);
|
||||
b.save();
|
||||
|
||||
// commit at this point
|
||||
transaction.commitAndContinue();
|
||||
try {
|
||||
|
||||
// turn on cascade ... such that the ManyToMany is persisted
|
||||
transaction.setPersistCascade(true);
|
||||
|
||||
// save b again which this time cascades to the ManyToMany
|
||||
// but this fails due to FK on ManyToMany
|
||||
b.save();
|
||||
|
||||
// we actually don't get here due to the FK error
|
||||
transaction.commit();
|
||||
|
||||
} catch (PersistenceException e) {
|
||||
// so we failed the second save but that is ok'ish
|
||||
// we handle this exception knowing b got inserted and committed
|
||||
// and that the inserts into the intersection table failed
|
||||
logger.info("The ManyToMany intersection error: " + e.getMessage());
|
||||
}
|
||||
} finally {
|
||||
// performs a rollback as the commit at line:80 does not happen
|
||||
transaction.end();
|
||||
}
|
||||
|
||||
// assert our insert prior to the commitAndContinue succeeded
|
||||
MnyB madeIt = DB.find(MnyB.class, b.getId());
|
||||
assertNotNull(madeIt);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.TxIsolation;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.sql.Connection;
|
||||
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class TestBeginTransactionWithExisting extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testTransactionIsoLevels() {
|
||||
assertEquals(Transaction.READ_COMMITTED, Connection.TRANSACTION_READ_COMMITTED);
|
||||
assertEquals(Transaction.READ_UNCOMMITTED, Connection.TRANSACTION_READ_UNCOMMITTED);
|
||||
assertEquals(Transaction.REPEATABLE_READ, Connection.TRANSACTION_REPEATABLE_READ);
|
||||
assertEquals(Transaction.SERIALIZABLE, Connection.TRANSACTION_SERIALIZABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
assertEquals(Transaction.READ_COMMITTED, Connection.TRANSACTION_READ_COMMITTED);
|
||||
assertThrows(PersistenceException.class, () -> {
|
||||
Transaction txn = DB.beginTransaction();
|
||||
try {
|
||||
try (Transaction txn2 = DB.beginTransaction(TxIsolation.READ_COMMITED)) {
|
||||
fail("Expected persitenceException here");
|
||||
}
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tests.model.m2m.MnyB;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class TestCommitAndContinue extends BaseTestCase {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger("org.avaje.ebean.TXN");
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
@IgnorePlatform({Platform.SQLSERVER, Platform.HSQLDB, Platform.COCKROACH}) // they will dead lock
|
||||
public void transactional_partialSuccess() {
|
||||
|
||||
MnyB a = new MnyB("a100");
|
||||
MnyB b = new MnyB("b200");
|
||||
|
||||
a.save();
|
||||
|
||||
// commit at this point
|
||||
DB.currentTransaction().commitAndContinue();
|
||||
|
||||
try {
|
||||
b.save();
|
||||
|
||||
// some error occurs
|
||||
throw new IllegalStateException();
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
// mark the transaction as rollback
|
||||
DB.currentTransaction().setRollbackOnly();
|
||||
|
||||
// use a different transaction to assert
|
||||
Database server = DB.getDefault();
|
||||
try (Transaction anotherTxn = server.createTransaction()) {
|
||||
// success prior to commitAndContinue
|
||||
assertNotNull(server.find(MnyB.class, a.getId(), anotherTxn));
|
||||
// insert failed after commitAndContinue - sqlserver dead locks here
|
||||
assertNull(server.find(MnyB.class, b.getId(), anotherTxn));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The @Transactional is nicer to me.
|
||||
*/
|
||||
@Test
|
||||
@IgnorePlatform({Platform.SQLSERVER, Platform.HSQLDB, Platform.COCKROACH}) // they will dead lock
|
||||
public void tryFinally_partialSuccess() {
|
||||
|
||||
MnyB a = new MnyB("a100");
|
||||
MnyB b = new MnyB("b200");
|
||||
|
||||
Database server = DB.getDefault();
|
||||
Transaction txn = server.beginTransaction();
|
||||
try {
|
||||
a.save();
|
||||
// commit at this point
|
||||
txn.commitAndContinue();
|
||||
|
||||
try {
|
||||
b.save();
|
||||
|
||||
// some error occurs
|
||||
throw new IllegalStateException();
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
// mark the transaction as rollback
|
||||
txn.setRollbackOnly();
|
||||
|
||||
// use a different transaction to assert
|
||||
try (Transaction anotherTxn = server.createTransaction()) {
|
||||
// success prior to commitAndContinue
|
||||
assertNotNull(server.find(MnyB.class, a.getId(), anotherTxn));
|
||||
// insert failed after commitAndContinue
|
||||
assertNull(server.find(MnyB.class, b.getId(), anotherTxn));
|
||||
}
|
||||
//anotherTxn.end();
|
||||
}
|
||||
|
||||
// does not commit due to the txn.setRollbackOnly();
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
//server.endTransaction();
|
||||
txn.end();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
@IgnorePlatform({Platform.SQLSERVER, Platform.HSQLDB, Platform.COCKROACH}) // they will dead lock
|
||||
public void transactional_partialSuccess_secondTransactionInsert() {
|
||||
|
||||
MnyB a = new MnyB("a100");
|
||||
MnyB b = new MnyB("b200");
|
||||
MnyB c = new MnyB("c300");
|
||||
|
||||
a.save();
|
||||
|
||||
// commit at this point
|
||||
DB.currentTransaction().commitAndContinue();
|
||||
|
||||
try {
|
||||
b.save();
|
||||
|
||||
// some error occurs
|
||||
throw new IllegalStateException();
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
// mark the transaction as rollback
|
||||
DB.currentTransaction().setRollbackOnly();
|
||||
|
||||
// use a different transaction to do something useful
|
||||
Database server = DB.getDefault();
|
||||
Transaction txn2 = server.createTransaction();
|
||||
try {
|
||||
server.save(c, txn2);
|
||||
txn2.commit();
|
||||
} finally {
|
||||
txn2.end();
|
||||
}
|
||||
}
|
||||
|
||||
// asserts
|
||||
|
||||
Database server = DB.getDefault();
|
||||
try (Transaction txnForAssert = server.createTransaction()) {
|
||||
// success prior to commitAndContinue
|
||||
assertNotNull(server.find(MnyB.class, a.getId(), txnForAssert));
|
||||
|
||||
// insert failed after commitAndContinue
|
||||
assertNull(server.find(MnyB.class, b.getId(), txnForAssert));
|
||||
|
||||
// successful insert using txn2
|
||||
assertNotNull(server.find(MnyB.class, c.getId(), txnForAssert));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basic() {
|
||||
|
||||
MnyB a = new MnyB("a");
|
||||
MnyB b = new MnyB("b");
|
||||
MnyB c = new MnyB("c");
|
||||
|
||||
Transaction txn = DB.beginTransaction();
|
||||
try {
|
||||
a.save();
|
||||
txn.commitAndContinue();
|
||||
|
||||
txn.setBatchMode(true);
|
||||
b.save();
|
||||
logger.info("... pre commitAndContinue");
|
||||
txn.commitAndContinue();
|
||||
|
||||
c.save();
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void runTransactional() {
|
||||
|
||||
new MnyB("a100").save();
|
||||
new MnyB("a101").save();
|
||||
|
||||
DB.currentTransaction().commitAndContinue();
|
||||
|
||||
new MnyB("a200").save();
|
||||
new MnyB("a201").save();
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestCreateTransactionWithIsolation extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
// Database server = DB.getDefault();
|
||||
// Transaction txn = server.createTransaction(TxIsolation.SERIALIZABLE);
|
||||
// txn.end();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebeaninternal.api.SpiTransaction;
|
||||
import org.tests.model.basic.EBasicVer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
public class TestDeleteFromPersistenceContext extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testDeleteBean() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
EBasicVer bean = new EBasicVer("Please Delete Me");
|
||||
DB.save(bean);
|
||||
|
||||
SpiTransaction transaction = (SpiTransaction) DB.beginTransaction();
|
||||
try {
|
||||
|
||||
EBasicVer bean2 = DB.find(EBasicVer.class, bean.getId());
|
||||
assertNotSame(bean, bean2);
|
||||
|
||||
EBasicVer bean3 = DB.find(EBasicVer.class, bean.getId());
|
||||
// same instance from PersistenceContext
|
||||
assertSame(bean2, bean3);
|
||||
|
||||
Object bean4 = transaction.getPersistenceContext().get(EBasicVer.class, bean.getId());
|
||||
assertSame(bean2, bean4);
|
||||
|
||||
DB.delete(bean2);
|
||||
|
||||
Object bean5 = transaction.getPersistenceContext().get(EBasicVer.class, bean.getId());
|
||||
assertNull(bean5);
|
||||
|
||||
DB.commitTransaction();
|
||||
|
||||
} finally {
|
||||
DB.endTransaction();
|
||||
}
|
||||
|
||||
EBasicVer bean6 = DB.find(EBasicVer.class).where().eq("id", bean.getId()).findOne();
|
||||
assertNull(bean6);
|
||||
|
||||
awaitL2Cache();
|
||||
EBasicVer bean7 = DB.find(EBasicVer.class, bean.getId());
|
||||
assertNull(bean7);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.DataIntegrityException;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.TxScope;
|
||||
import io.ebean.annotation.ForPlatform;
|
||||
import io.ebean.annotation.PersistBatch;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import io.ebeaninternal.api.SpiTransaction;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
public class TestExecuteComplete extends BaseTestCase {
|
||||
|
||||
|
||||
@ForPlatform(Platform.H2)
|
||||
@Test
|
||||
public void execute_when_errorOnCommit_threadLocalIsCleared() {
|
||||
|
||||
try {
|
||||
DB.execute(TxScope.required().setBatch(PersistBatch.ALL), () -> {
|
||||
|
||||
Customer customer = DB.reference(Customer.class, 42424242L);
|
||||
Order order = new Order();
|
||||
order.setCustomer(customer);
|
||||
|
||||
DB.save(order);
|
||||
});
|
||||
fail();
|
||||
} catch (DataIntegrityException e) {
|
||||
// assert the thread local has been cleaned up
|
||||
assertThat(getInScopeTransaction()).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
@ForPlatform(Platform.H2)
|
||||
@Test
|
||||
public void nestedExecute_when_errorOnCommit_threadLocalIsCleared() {
|
||||
|
||||
try {
|
||||
DB.execute(TxScope.required().setBatch(PersistBatch.ALL), () ->
|
||||
DB.execute(() -> {
|
||||
|
||||
Customer customer = DB.reference(Customer.class, 42424242L);
|
||||
Order order = new Order();
|
||||
order.setCustomer(customer);
|
||||
|
||||
DB.save(order);
|
||||
}));
|
||||
fail();
|
||||
} catch (DataIntegrityException e) {
|
||||
// assert the thread local has been cleaned up
|
||||
assertThat(getInScopeTransaction()).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
@ForPlatform(Platform.H2)
|
||||
@Test
|
||||
public void transactional_errorOnCommit_expect_threadScopeCleanup() {
|
||||
|
||||
try {
|
||||
errorOnCommit();
|
||||
fail();
|
||||
} catch (DataIntegrityException e) {
|
||||
assertThat(getInScopeTransaction()).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(batchSize = 10)
|
||||
private void errorOnCommit() {
|
||||
Customer customer = DB.reference(Customer.class, 42424242L);
|
||||
Order order = new Order();
|
||||
order.setCustomer(customer);
|
||||
|
||||
DB.save(order);
|
||||
}
|
||||
|
||||
@ForPlatform(Platform.H2)
|
||||
@Test
|
||||
public void normal_expect_threadScopeCleanup() {
|
||||
|
||||
Transaction txn1 = server().beginTransaction();
|
||||
try {
|
||||
txn1.commit();
|
||||
} finally {
|
||||
txn1.end();
|
||||
}
|
||||
|
||||
assertThat(getInScopeTransaction()).isNull();
|
||||
}
|
||||
|
||||
@ForPlatform(Platform.H2)
|
||||
@Test
|
||||
public void missingEnd_expect_threadScopeCleanup() {
|
||||
|
||||
Transaction txn1 = server().beginTransaction();
|
||||
try {
|
||||
txn1.commit();
|
||||
} finally {
|
||||
// accidentally omit end()
|
||||
//txn1.end();
|
||||
}
|
||||
|
||||
assertThat(getInScopeTransaction()).isNull();
|
||||
}
|
||||
|
||||
@ForPlatform(Platform.H2)
|
||||
@Test
|
||||
public void missingEnd_withRollbackOnly_expect_threadScopeCleanup() {
|
||||
|
||||
Transaction txn1 = server().beginTransaction();
|
||||
try {
|
||||
txn1.rollback();
|
||||
} finally {
|
||||
// accidentally omit end()
|
||||
//txn1.end();
|
||||
}
|
||||
|
||||
assertThat(getInScopeTransaction()).isNull();
|
||||
}
|
||||
|
||||
@ForPlatform(Platform.H2)
|
||||
@Test
|
||||
public void implicit_query_expect_threadScopeCleanup() {
|
||||
|
||||
DB.find(Customer.class).findList();
|
||||
|
||||
assertThat(getInScopeTransaction()).isNull();
|
||||
}
|
||||
|
||||
@ForPlatform(Platform.H2)
|
||||
@Test
|
||||
public void implicit_save_expect_threadScopeCleanup() {
|
||||
|
||||
Customer cust = new Customer();
|
||||
cust.setName("Roland");
|
||||
DB.save(cust);
|
||||
|
||||
assertThat(getInScopeTransaction()).isNull();
|
||||
}
|
||||
|
||||
@ForPlatform(Platform.H2)
|
||||
@Test
|
||||
public void no_transaction_expect_threadScopeCleanup() {
|
||||
|
||||
try (Transaction txn = DB.beginTransaction(TxScope.notSupported())) {
|
||||
SpiTransaction txn2 = getInScopeTransaction();
|
||||
// The NoTransaction placeholder can normally only occur inside
|
||||
// a scopedTrans. (Class is package private, so check
|
||||
assertThat(txn2.toString()).contains("NoTransaction");
|
||||
assertThat(txn2.toString()).contains("NoTransaction");
|
||||
}
|
||||
|
||||
assertThat(getInScopeTransaction()).isNull();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.BBookmark;
|
||||
import org.tests.model.basic.BBookmarkUser;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TestInsertManyAndRef extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testMe() {
|
||||
|
||||
final BBookmarkUser u = new BBookmarkUser("Mr Test");
|
||||
u.setEmailAddress("test@test.com");
|
||||
u.setPassword("password");
|
||||
|
||||
final List<BBookmark> bookmarks = new ArrayList<>();
|
||||
final BBookmark b1 = new BBookmark();
|
||||
b1.setBookmarkReference("Acts 2:7-20");
|
||||
b1.setUser(u);
|
||||
|
||||
final BBookmark b2 = new BBookmark();
|
||||
b2.setBookmarkReference("Acts 7:1-20");
|
||||
b2.setUser(u);
|
||||
|
||||
bookmarks.add(b1);
|
||||
bookmarks.add(b2);
|
||||
|
||||
DB.saveAll(bookmarks);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestNested extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testRunnableFail() {
|
||||
|
||||
try {
|
||||
DB.execute(this::willFail);
|
||||
} catch (PersistenceException e) {
|
||||
assertThat(e.getMessage()).contains("test runnable rollback");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCallableFail() {
|
||||
|
||||
try {
|
||||
DB.execute(this::willFailCallable);
|
||||
} catch (PersistenceException e) {
|
||||
assertThat(e.getMessage()).contains("test callable rollback");
|
||||
}
|
||||
}
|
||||
|
||||
private void willFail() {
|
||||
DB.executeCall(() -> {
|
||||
throw new RuntimeException("test runnable rollback");
|
||||
});
|
||||
}
|
||||
|
||||
private void willFailCallable() {
|
||||
DB.executeCall((Callable<String>) () -> {
|
||||
throw new Exception("test callable rollback");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.TxScope;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Product;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TestNestedBeginRequired extends BaseTestCase {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(TestNestedBeginRequired.class);
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
someOuterMethod();
|
||||
}
|
||||
|
||||
private void someOuterMethod() {
|
||||
|
||||
Transaction txn = server.beginTransaction(TxScope.required());
|
||||
try {
|
||||
|
||||
server.find(Country.class).findCount();
|
||||
|
||||
someInnerMethod();
|
||||
|
||||
server.find(Product.class).findCount();
|
||||
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void someInnerMethod() {
|
||||
|
||||
logger.debug("someInnerMethod() ...");
|
||||
Transaction txn = server.beginTransaction(TxScope.required());
|
||||
try {
|
||||
|
||||
server.find(Customer.class).findCount();
|
||||
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
logger.debug("someInnerMethod() ... done");
|
||||
|
||||
}
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.TxScope;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.EBasic;
|
||||
import org.tests.model.basic.Product;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TestNestedBeginRequiredWithFailure extends BaseTestCase {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(TestNestedBeginRequiredWithFailure.class);
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
someOuterMethod();
|
||||
}
|
||||
|
||||
private void someOuterMethod() {
|
||||
|
||||
Transaction txn = server.beginTransaction(TxScope.required());
|
||||
try {
|
||||
|
||||
server.find(Country.class).findCount();
|
||||
|
||||
try {
|
||||
someInnerMethodWithFailure();
|
||||
|
||||
server.find(Product.class).findCount();
|
||||
txn.commit();
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
logger.info("Inner method failed with " + e);
|
||||
}
|
||||
|
||||
} finally {
|
||||
// For REQUIRED ... the transaction can already been
|
||||
// rolled back (by inner method) so that case is expected
|
||||
// (and that is the case here - txn is already rolled back)
|
||||
txn.end();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void someInnerMethodWithFailure() {
|
||||
|
||||
logger.debug("someInnerMethod() ...");
|
||||
Transaction txn = server.beginTransaction(TxScope.required());
|
||||
try {
|
||||
|
||||
server.find(Customer.class).findCount();
|
||||
|
||||
EBasic basic = new EBasic();
|
||||
basic.setName("ignore");
|
||||
server.save(basic);
|
||||
|
||||
if (server != null) {
|
||||
throw new RuntimeException("barf");
|
||||
}
|
||||
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
logger.debug("someInnerMethod() ... done");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.TxScope;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Product;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TestNestedBeginRequiresNew extends BaseTestCase {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(TestNestedBeginRequiresNew.class);
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
someOuterMethod();
|
||||
}
|
||||
|
||||
private void someOuterMethod() {
|
||||
|
||||
Transaction txn = server.beginTransaction(TxScope.requiresNew());
|
||||
try {
|
||||
|
||||
server.find(Country.class).findCount();
|
||||
|
||||
someInnerMethod();
|
||||
|
||||
server.find(Product.class).findCount();
|
||||
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void someInnerMethod() {
|
||||
|
||||
logger.debug("someInnerMethod() ...");
|
||||
Transaction txn = server.beginTransaction(TxScope.requiresNew());
|
||||
try {
|
||||
|
||||
server.find(Customer.class).findCount();
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
logger.debug("someInnerMethod() ... done");
|
||||
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.TxScope;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Product;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TestNestedBeginRequiresNewWithFailure extends BaseTestCase {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(TestNestedBeginRequiresNewWithFailure.class);
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
someOuterMethod();
|
||||
}
|
||||
|
||||
private void someOuterMethod() {
|
||||
|
||||
Transaction txn = server.beginTransaction(TxScope.requiresNew());
|
||||
try {
|
||||
|
||||
server.find(Country.class).findCount();
|
||||
|
||||
try {
|
||||
someInnerMethodWithFailure();
|
||||
} catch (RuntimeException e) {
|
||||
logger.info("Inner method failed with " + e.getMessage());
|
||||
}
|
||||
server.find(Product.class).findCount();
|
||||
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void someInnerMethodWithFailure() {
|
||||
|
||||
logger.debug("someInnerMethod() ...");
|
||||
Transaction txn = server.beginTransaction(TxScope.requiresNew());
|
||||
try {
|
||||
|
||||
server.find(Customer.class).findCount();
|
||||
|
||||
if (server != null) {
|
||||
throw new RuntimeException("barf");
|
||||
}
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
logger.debug("someInnerMethod() ... done");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import io.ebean.annotation.TxType;
|
||||
import io.ebean.meta.MetaTimedMetric;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestNestedMandatory extends BaseTestCase {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TestNestedMandatory.class);
|
||||
|
||||
private static Transaction outerTxn;
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
resetAllMetrics();
|
||||
|
||||
new Outer().doOuter();
|
||||
|
||||
List<MetaTimedMetric> txnMetrics = visitTimedMetrics();
|
||||
for (MetaTimedMetric txnTimed : txnMetrics) {
|
||||
System.out.println(txnTimed);
|
||||
}
|
||||
|
||||
assertThat(txnMetrics).hasSize(2);
|
||||
assertThat(txnMetrics.get(1).name()).isEqualTo("txn.named.outer");
|
||||
}
|
||||
|
||||
class Outer {
|
||||
|
||||
@Transactional(label = "outer")
|
||||
void doOuter() {
|
||||
outerTxn = DB.currentTransaction();
|
||||
|
||||
log.info("outer start ...{}", outerTxn);
|
||||
new Inner().doInner();
|
||||
log.info("outer end ...{}", outerTxn);
|
||||
}
|
||||
}
|
||||
|
||||
class Inner {
|
||||
|
||||
@Transactional(type = TxType.MANDATORY)
|
||||
void doInner() {
|
||||
Transaction innerTxn = DB.currentTransaction();
|
||||
log.info("inner ...{}", innerTxn);
|
||||
|
||||
assertThat(innerTxn).isSameAs(outerTxn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.PersistenceContextScope;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.TxScope;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.EBasic;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class TestNestedSubTransaction extends BaseTestCase {
|
||||
|
||||
/**
|
||||
* MySql only supports named savepoints - review.
|
||||
*/
|
||||
@IgnorePlatform({Platform.MYSQL, Platform.SQLSERVER, Platform.HANA})
|
||||
@Test
|
||||
public void ebeanServer_commitTransaction_expect_sameAsTransactionCommit() {
|
||||
|
||||
Database server = server();
|
||||
|
||||
EBasic bean = new EBasic("x1");
|
||||
|
||||
try (Transaction txn0 = server.beginTransaction()) {
|
||||
txn0.setRollbackOnly();
|
||||
|
||||
server.save(bean);
|
||||
|
||||
try (Transaction txn1 = server.beginTransaction()) {
|
||||
bean.setName("x2");
|
||||
server.save(bean);
|
||||
//txn1.commit();
|
||||
server.commitTransaction();
|
||||
}
|
||||
|
||||
EBasic fresh = server.find(EBasic.class, bean.getId());
|
||||
assertNotNull(fresh); // FAILS
|
||||
assertThat(fresh.getName()).isEqualTo("x2");
|
||||
|
||||
try (Transaction txn2 = server.beginTransaction()) {
|
||||
bean.setName("barney");
|
||||
DB.save(bean);
|
||||
//txn2.commit();
|
||||
server.commitTransaction();
|
||||
}
|
||||
|
||||
fresh = server.find(EBasic.class)
|
||||
.setId(bean.getId())
|
||||
.setPersistenceContextScope(PersistenceContextScope.QUERY)
|
||||
.findOne();
|
||||
|
||||
assertNotNull(fresh);
|
||||
assertThat(fresh.getName()).isEqualTo("barney");
|
||||
|
||||
} finally {
|
||||
cleanup(bean);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@IgnorePlatform({Platform.SQLSERVER, Platform.MYSQL, Platform.HANA, Platform.ORACLE})
|
||||
@Test
|
||||
public void nestedUseSavepoint_doubleNested_rollbackCommit() {
|
||||
|
||||
Database server = server();
|
||||
|
||||
EBasic bean = new EBasic("start2");
|
||||
|
||||
try (Transaction txn0 = server.beginTransaction()) {
|
||||
txn0.setRollbackOnly();
|
||||
txn0.setNestedUseSavepoint();
|
||||
|
||||
server.save(bean);
|
||||
|
||||
try (Transaction txn1 = server.beginTransaction()) {
|
||||
bean.setName("updateNested");
|
||||
server.save(bean);
|
||||
|
||||
try (Transaction txn2 = server.beginTransaction()) {
|
||||
bean.setName("barney");
|
||||
DB.save(bean);
|
||||
txn2.commit();
|
||||
}
|
||||
|
||||
txn1.rollback();
|
||||
}
|
||||
|
||||
EBasic fresh = server.find(EBasic.class)
|
||||
.setId(bean.getId())
|
||||
.setPersistenceContextScope(PersistenceContextScope.QUERY)
|
||||
.findOne();
|
||||
|
||||
assertNotNull(fresh);
|
||||
assertThat(fresh.getName()).isEqualTo("start2");
|
||||
}
|
||||
}
|
||||
|
||||
@IgnorePlatform({Platform.SQLSERVER, Platform.MYSQL, Platform.HANA, Platform.ORACLE})
|
||||
@Test
|
||||
public void nestedUseSavepoint_doubleNested_commitRollback() {
|
||||
|
||||
Database server = server();
|
||||
|
||||
EBasic bean = new EBasic("start3");
|
||||
|
||||
try (Transaction txn0 = server.beginTransaction()) {
|
||||
txn0.setRollbackOnly();
|
||||
txn0.setNestedUseSavepoint();
|
||||
|
||||
server.save(bean);
|
||||
|
||||
try (Transaction txn1 = server.beginTransaction()) {
|
||||
bean.setName("updateNested3");
|
||||
server.save(bean);
|
||||
|
||||
try (Transaction txn2 = server.beginTransaction()) {
|
||||
bean.setName("barney3");
|
||||
DB.save(bean);
|
||||
txn2.rollback();
|
||||
}
|
||||
|
||||
txn1.commit();
|
||||
}
|
||||
|
||||
EBasic fresh = server.find(EBasic.class)
|
||||
.setId(bean.getId())
|
||||
.setPersistenceContextScope(PersistenceContextScope.QUERY)
|
||||
.findOne();
|
||||
|
||||
assertNotNull(fresh);
|
||||
assertThat(fresh.getName()).isEqualTo("updateNested3");
|
||||
}
|
||||
}
|
||||
|
||||
@IgnorePlatform({Platform.SQLSERVER, Platform.MYSQL, Platform.HANA, Platform.ORACLE})
|
||||
@Test
|
||||
public void nestedUseSavepoint_nested_RequiresNew() {
|
||||
|
||||
Database server = server();
|
||||
|
||||
EBasic bean = new EBasic("start4");
|
||||
|
||||
try (Transaction txn0 = server.beginTransaction()) {
|
||||
txn0.setRollbackOnly();
|
||||
txn0.setNestedUseSavepoint();
|
||||
|
||||
server.save(bean);
|
||||
|
||||
try (Transaction txn1 = server.beginTransaction(TxScope.requiresNew())) {
|
||||
bean.setName("updateNested4");
|
||||
server.save(bean);
|
||||
txn1.commit();
|
||||
}
|
||||
|
||||
EBasic fresh = server.find(EBasic.class)
|
||||
.setId(bean.getId())
|
||||
.setPersistenceContextScope(PersistenceContextScope.QUERY)
|
||||
.findOne();
|
||||
|
||||
assertNotNull(fresh);
|
||||
assertThat(fresh.getName()).isEqualTo("updateNested4");
|
||||
}
|
||||
|
||||
EBasic after = server.find(EBasic.class)
|
||||
.setId(bean.getId())
|
||||
.findOne();
|
||||
|
||||
assertNull(after);
|
||||
}
|
||||
|
||||
@IgnorePlatform({Platform.SQLSERVER, Platform.MYSQL, Platform.HANA, Platform.ORACLE})
|
||||
@Test
|
||||
public void nestedUseSavepoint() {
|
||||
|
||||
Database server = server();
|
||||
|
||||
EBasic bean = new EBasic("start1");
|
||||
|
||||
try (Transaction txn0 = server.beginTransaction()) {
|
||||
txn0.setRollbackOnly();
|
||||
txn0.setNestedUseSavepoint();
|
||||
|
||||
server.save(bean);
|
||||
|
||||
try (Transaction txn1 = server.beginTransaction()) {
|
||||
bean.setName("updateNested");
|
||||
server.save(bean);
|
||||
txn1.commit();
|
||||
}
|
||||
|
||||
EBasic fresh = server.find(EBasic.class, bean.getId());
|
||||
assertNotNull(fresh);
|
||||
assertThat(fresh.getName()).isEqualTo("updateNested");
|
||||
|
||||
try (Transaction txn2 = server.beginTransaction()) {
|
||||
bean.setName("barney");
|
||||
DB.save(bean);
|
||||
txn2.rollback();
|
||||
}
|
||||
|
||||
fresh = server.find(EBasic.class)
|
||||
.setId(bean.getId())
|
||||
.setPersistenceContextScope(PersistenceContextScope.QUERY)
|
||||
.findOne();
|
||||
|
||||
assertNotNull(fresh);
|
||||
assertThat(fresh.getName()).isEqualTo("updateNested");
|
||||
|
||||
} finally {
|
||||
cleanup(bean);
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanup(EBasic bean) {
|
||||
DB.delete(EBasic.class, bean.getId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tests.model.basic.EBasic;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestNestedTransaction extends BaseTestCase {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TestNestedTransaction.class);
|
||||
|
||||
private EBasic bean;
|
||||
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
bean = new EBasic("new");
|
||||
DB.save(bean);
|
||||
}
|
||||
|
||||
private void assertClean() {
|
||||
EBasic myBean = DB.find(EBasic.class, bean.getId());
|
||||
assertThat(myBean.getName()).isEqualTo("new");
|
||||
}
|
||||
|
||||
private void assertModified() {
|
||||
EBasic myBean = DB.find(EBasic.class, bean.getId());
|
||||
assertThat(myBean.getName()).isEqualTo("modified");
|
||||
}
|
||||
|
||||
private void modify() {
|
||||
bean.setName("modified");
|
||||
DB.save(bean);
|
||||
}
|
||||
|
||||
// ===== level 0 =======
|
||||
@Test
|
||||
public void testNested_0() {
|
||||
try (Transaction txn0 = DB.beginTransaction()) {
|
||||
modify();
|
||||
// no commit
|
||||
}
|
||||
assertClean();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNested_1() {
|
||||
try (Transaction txn0 = DB.beginTransaction()) {
|
||||
modify();
|
||||
txn0.commit();
|
||||
}
|
||||
assertModified();
|
||||
}
|
||||
|
||||
// ===== level 1 =======
|
||||
@Test
|
||||
public void testNested_00() {
|
||||
try (Transaction txn0 = DB.beginTransaction()) {
|
||||
try (Transaction txn1 = DB.beginTransaction()) {
|
||||
modify();
|
||||
// no commit
|
||||
}
|
||||
// no commit
|
||||
}
|
||||
assertClean();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNested_01() {
|
||||
try (Transaction txn0 = DB.beginTransaction()) {
|
||||
try (Transaction txn1 = DB.beginTransaction()) {
|
||||
modify();
|
||||
// no commit
|
||||
}
|
||||
attemptCommit(txn0);
|
||||
}
|
||||
assertClean();
|
||||
}
|
||||
|
||||
private void attemptCommit(Transaction txn) {
|
||||
try {
|
||||
txn.commit();
|
||||
} catch (IllegalStateException e) {
|
||||
// expected
|
||||
log.info("Expected IllegalStateException as transaction already rolled back " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNested_10() {
|
||||
try (Transaction txn0 = DB.beginTransaction()) {
|
||||
try (Transaction txn1 = DB.beginTransaction()) {
|
||||
modify();
|
||||
txn1.commit();
|
||||
}
|
||||
// no commit
|
||||
}
|
||||
assertClean();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNested_11() {
|
||||
try (Transaction txn0 = DB.beginTransaction()) {
|
||||
try (Transaction txn1 = DB.beginTransaction()) {
|
||||
modify();
|
||||
txn1.commit();
|
||||
}
|
||||
txn0.commit();
|
||||
}
|
||||
assertModified();
|
||||
}
|
||||
|
||||
// ===== level 2 =======
|
||||
@Test
|
||||
public void testNested_000() {
|
||||
try (Transaction txn0 = DB.beginTransaction()) {
|
||||
try (Transaction txn1 = DB.beginTransaction()) {
|
||||
try (Transaction txn2 = DB.beginTransaction()) {
|
||||
modify();
|
||||
// no commit
|
||||
}
|
||||
// no commit
|
||||
}
|
||||
// no commit
|
||||
}
|
||||
assertClean();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNested_001() {
|
||||
try (Transaction txn0 = DB.beginTransaction()) {
|
||||
try (Transaction txn1 = DB.beginTransaction()) {
|
||||
try (Transaction txn2 = DB.beginTransaction()) {
|
||||
modify();
|
||||
// no commit
|
||||
}
|
||||
// no commit
|
||||
}
|
||||
attemptCommit(txn0);
|
||||
}
|
||||
assertClean();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNested_010() {
|
||||
try (Transaction txn0 = DB.beginTransaction()) {
|
||||
try (Transaction txn1 = DB.beginTransaction()) {
|
||||
try (Transaction txn2 = DB.beginTransaction()) {
|
||||
modify();
|
||||
// no commit
|
||||
}
|
||||
txn1.commit();
|
||||
}
|
||||
// no commit
|
||||
}
|
||||
assertClean();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNested_011() {
|
||||
try (Transaction txn0 = DB.beginTransaction()) {
|
||||
try (Transaction txn1 = DB.beginTransaction()) {
|
||||
try (Transaction txn2 = DB.beginTransaction()) {
|
||||
modify();
|
||||
// no commit
|
||||
}
|
||||
txn1.commit();
|
||||
}
|
||||
attemptCommit(txn0);
|
||||
}
|
||||
assertClean();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNested_100() {
|
||||
try (Transaction txn0 = DB.beginTransaction()) {
|
||||
try (Transaction txn1 = DB.beginTransaction()) {
|
||||
try (Transaction txn2 = DB.beginTransaction()) {
|
||||
modify();
|
||||
txn2.commit();
|
||||
}
|
||||
// no commit
|
||||
}
|
||||
// no commit
|
||||
}
|
||||
assertClean();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNested_101() {
|
||||
try (Transaction txn0 = DB.beginTransaction()) {
|
||||
try (Transaction txn1 = DB.beginTransaction()) {
|
||||
try (Transaction txn2 = DB.beginTransaction()) {
|
||||
modify();
|
||||
txn2.commit();
|
||||
}
|
||||
// no commit
|
||||
}
|
||||
attemptCommit(txn0);
|
||||
}
|
||||
assertClean();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNested_110() {
|
||||
try (Transaction txn0 = DB.beginTransaction()) {
|
||||
try (Transaction txn1 = DB.beginTransaction()) {
|
||||
try (Transaction txn2 = DB.beginTransaction()) {
|
||||
modify();
|
||||
txn2.commit();
|
||||
}
|
||||
txn1.commit();
|
||||
}
|
||||
// no commit
|
||||
}
|
||||
assertClean();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNested_111() {
|
||||
try (Transaction txn0 = DB.beginTransaction()) {
|
||||
try (Transaction txn1 = DB.beginTransaction()) {
|
||||
try (Transaction txn2 = DB.beginTransaction()) {
|
||||
modify();
|
||||
txn2.commit();
|
||||
}
|
||||
txn1.commit();
|
||||
}
|
||||
txn0.commit();
|
||||
}
|
||||
assertModified();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.bean.PersistenceContext;
|
||||
import io.ebeaninternal.api.SpiTransaction;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestPersistContextClear extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Order order0 = null;
|
||||
|
||||
Transaction t = DB.beginTransaction();
|
||||
try {
|
||||
ResetBasicData.createOrderCustAndOrder("testPc");
|
||||
SpiTransaction spiTxn = (SpiTransaction) t;
|
||||
PersistenceContext pc = spiTxn.getPersistenceContext();
|
||||
|
||||
// no orders or customers in the PC
|
||||
assertEquals(0, pc.size(Order.class));
|
||||
assertEquals(0, pc.size(Customer.class));
|
||||
|
||||
Database server = DB.getDefault();
|
||||
List<Order> list = server.find(Order.class).fetch("customer").fetch("details").findList();
|
||||
|
||||
int orderSize = list.size();
|
||||
assertTrue(orderSize > 1);
|
||||
|
||||
// keep a hold of one of them
|
||||
order0 = list.get(0);
|
||||
|
||||
assertEquals(orderSize, pc.size(Order.class));
|
||||
|
||||
System.gc();
|
||||
assertEquals(orderSize, pc.size(Order.class));
|
||||
assertTrue(pc.size(Customer.class) > 0);
|
||||
|
||||
list = null;
|
||||
// System.gc();
|
||||
|
||||
// transaction still holds PC ...
|
||||
// System.out.println("pc2:"+pc);
|
||||
// These asserts may not succeed depending on JVM
|
||||
// assertEquals(pc.size(Order.class), 1);
|
||||
// assertEquals(pc.size(Customer.class), 1);
|
||||
|
||||
} finally {
|
||||
t.end();
|
||||
}
|
||||
|
||||
System.gc();
|
||||
|
||||
// we still have the order
|
||||
assertNotNull(order0);
|
||||
// its likely the only one in the PC now
|
||||
// due to the System.gc(); but can't garuntee it
|
||||
// so removing these asserts... can put them back
|
||||
// to manually test.
|
||||
// assertEquals(pc.size(Order.class), 1);
|
||||
// assertEquals(pc.size(Customer.class), 1);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.ForPlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.EBasicVer;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Savepoint;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestSavepoint extends BaseTestCase {
|
||||
|
||||
@ForPlatform({Platform.POSTGRES, Platform.H2})
|
||||
@Test
|
||||
public void test() throws SQLException {
|
||||
|
||||
EBasicVer basicVer = new EBasicVer("save1");
|
||||
basicVer.setOther("other1");
|
||||
DB.save(basicVer);
|
||||
|
||||
try (Transaction transaction = DB.beginTransaction()) {
|
||||
|
||||
Connection connection = transaction.connection();
|
||||
Savepoint savepoint = connection.setSavepoint();
|
||||
|
||||
basicVer.setOther("changeOther");
|
||||
DB.save(basicVer);
|
||||
|
||||
connection.rollback(savepoint);
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
EBasicVer found = DB.find(EBasicVer.class, basicVer.getId());
|
||||
assertThat(found.getOther()).isEqualTo("other1");
|
||||
|
||||
}
|
||||
|
||||
@ForPlatform({Platform.POSTGRES, Platform.H2})
|
||||
@Test
|
||||
public void rollbackOnly_expect_similarResult() {
|
||||
|
||||
EBasicVer basicVer = new EBasicVer("save2");
|
||||
basicVer.setOther("other2");
|
||||
DB.save(basicVer);
|
||||
|
||||
try (Transaction transaction = DB.beginTransaction()) {
|
||||
transaction.setRollbackOnly();
|
||||
|
||||
basicVer.setOther("changeOther");
|
||||
DB.save(basicVer);
|
||||
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
EBasicVer found = DB.find(EBasicVer.class, basicVer.getId());
|
||||
assertThat(found.getOther()).isEqualTo("other2");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.ESimple;
|
||||
|
||||
/**
|
||||
* This test tests a strange bug in the 6.2.0. sqlserver JDBC driver.
|
||||
* (Version 6.1.7.jre8-preview works)
|
||||
* <p>
|
||||
* https://github.com/Microsoft/mssql-jdbc/pull/374
|
||||
*
|
||||
* @author Roland Praml, FOCONIS AG
|
||||
*/
|
||||
public class TestSqlServerBatch extends BaseTestCase {
|
||||
|
||||
@IgnorePlatform(value = Platform.SQLSERVER)
|
||||
@Test
|
||||
public void testBasicIdentityBatch() {
|
||||
|
||||
Transaction txn = DB.beginTransaction();
|
||||
try {
|
||||
txn.setBatchMode(true);
|
||||
txn.setBatchSize(3);
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
ESimple model = new ESimple();
|
||||
model.setName("baz " + i);
|
||||
DB.save(model);
|
||||
}
|
||||
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAggressiveBatch() {
|
||||
|
||||
Transaction txn = DB.beginTransaction();
|
||||
try {
|
||||
txn.setBatchMode(true);
|
||||
txn.setBatchSize(3);
|
||||
|
||||
// control flushing when mixing save and queries
|
||||
txn.setFlushOnQuery(false);
|
||||
|
||||
// for large batch insert processing when we do not
|
||||
// ... need the generatedKeys, don't get them
|
||||
txn.setGetGeneratedKeys(false);
|
||||
|
||||
// explicitly flush the JDBC batch buffer
|
||||
txn.flush();
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
ESimple model = new ESimple();
|
||||
model.setName(i % 2 == 0 ? null : "foobar");
|
||||
DB.save(model);
|
||||
}
|
||||
|
||||
// do not commit
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.EBasic;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestStatelessUpdateClearPC extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
|
||||
DB.beginTransaction();
|
||||
try {
|
||||
EBasic newUser = new EBasic();
|
||||
newUser.setName("any@email.com");
|
||||
DB.save(newUser);
|
||||
|
||||
// load the bean into the persistence context
|
||||
EBasic dummyLoadedUser = DB.find(EBasic.class, newUser.getId()); // This row is added
|
||||
assertNotNull(dummyLoadedUser);
|
||||
|
||||
// stateless update (should clear the bean from the persistence context)
|
||||
EBasic updateUser = new EBasic();
|
||||
updateUser.setId(newUser.getId());
|
||||
updateUser.setName("anyNew@email.com");
|
||||
DB.update(updateUser);
|
||||
|
||||
EBasic loadedUser = DB.find(EBasic.class, newUser.getId());
|
||||
assertEquals("anyNew@email.com", loadedUser.getName());
|
||||
} finally {
|
||||
DB.rollbackTransaction();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.TransactionCallbackAdapter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class TestTransactionCallback extends BaseTestCase {
|
||||
|
||||
int countPreCommit;
|
||||
int countPostCommit;
|
||||
int countPreRollback;
|
||||
int countPostRollback;
|
||||
|
||||
@Test
|
||||
public void test_commitAndRollback() {
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
DB.register(new MyCallback());
|
||||
txn.connection(); // Ebean assumes writes have occurred
|
||||
txn.commit();
|
||||
}
|
||||
|
||||
assertEquals(1, countPreCommit);
|
||||
assertEquals(1, countPostCommit);
|
||||
assertEquals(0, countPreRollback);
|
||||
assertEquals(0, countPostRollback);
|
||||
|
||||
DB.beginTransaction();
|
||||
try {
|
||||
DB.register(new MyCallback());
|
||||
} finally {
|
||||
DB.rollbackTransaction();
|
||||
}
|
||||
|
||||
assertEquals(1, countPreCommit);
|
||||
assertEquals(1, countPostCommit);
|
||||
assertEquals(1, countPreRollback);
|
||||
assertEquals(1, countPostRollback);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_commit_whenNoDbWrite() {
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
DB.register(new MyCallback());
|
||||
txn.commit();
|
||||
}
|
||||
|
||||
assertEquals(1, countPreCommit);
|
||||
assertEquals(1, countPostCommit);
|
||||
assertEquals(0, countPreRollback);
|
||||
assertEquals(0, countPostRollback);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_rollback_whenNoDbWrite() {
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
DB.register(new MyCallback());
|
||||
txn.rollback();
|
||||
}
|
||||
|
||||
assertEquals(0, countPreCommit);
|
||||
assertEquals(0, countPostCommit);
|
||||
assertEquals(1, countPreRollback);
|
||||
assertEquals(1, countPostRollback);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_noActiveTransaction() {
|
||||
assertThrows(PersistenceException.class, () -> DB.register(new MyCallback()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_noActiveTransaction_withDatabase() {
|
||||
assertThrows(PersistenceException.class, () -> DB.getDefault().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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.EBasic;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestTransactionRollbackOnly {
|
||||
|
||||
private EBasic one;
|
||||
|
||||
private EBasic two;
|
||||
|
||||
@Test
|
||||
public void transaction_setRollbackOnly() {
|
||||
|
||||
doVia_currentTransaction();
|
||||
|
||||
assertThat(one.getId()).isNotNull();
|
||||
assertThat(DB.find(EBasic.class, one.getId())).isNull();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
protected void doVia_currentTransaction() {
|
||||
|
||||
one = new EBasic("WillNotSave");
|
||||
DB.save(one);
|
||||
|
||||
Transaction transaction = Transaction.current();
|
||||
assertFalse(transaction.isRollbackOnly());
|
||||
|
||||
transaction.setRollbackOnly();
|
||||
assertTrue(transaction.isRollbackOnly());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_Ebean_setRollbackOnly() {
|
||||
|
||||
do_Ebean_setRollbackOnly();
|
||||
|
||||
assertThat(two.getId()).isNotNull();
|
||||
assertThat(DB.find(EBasic.class, two.getId())).isNull();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
protected void do_Ebean_setRollbackOnly() {
|
||||
|
||||
two = new EBasic("WillNotSave");
|
||||
DB.save(two);
|
||||
|
||||
DB.setRollbackOnly();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.CacheMode;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestTransactionSkipCache extends BaseTestCase {
|
||||
|
||||
private void findUseQueryCache() {
|
||||
|
||||
DB.find(Customer.class)
|
||||
.setUseQueryCache(true)
|
||||
.setBeanCacheMode(CacheMode.PUT)
|
||||
.where().startsWith("name", "Rob")
|
||||
.findList();
|
||||
}
|
||||
|
||||
private void setup() {
|
||||
ResetBasicData.reset();
|
||||
// load into L2 query cache
|
||||
findUseQueryCache();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void skipL2QueryCache() {
|
||||
|
||||
setup();
|
||||
|
||||
Transaction transaction = DB.beginTransaction();
|
||||
try {
|
||||
transaction.setSkipCache(true);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
findUseQueryCache();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).as("We didn't use the L2 query cache").hasSize(1);
|
||||
|
||||
} finally {
|
||||
transaction.end();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DuplicateKeyException;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tests.model.draftable.Document;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestTransactionTryResources extends BaseTestCase {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TestTransactionTryResources.class);
|
||||
|
||||
@Test
|
||||
public void tryWithResources_simple() {
|
||||
|
||||
Document doc = new Document();
|
||||
|
||||
try (Transaction transaction = DB.beginTransaction()) {
|
||||
|
||||
doc.setTitle("tryWithResources");
|
||||
doc.setBody("stuff");
|
||||
doc.save();
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
Document document = Document.find.asDraft(doc.getId());
|
||||
assertThat(document).isNotNull();
|
||||
assertThat(document.isDraft()).isTrue();
|
||||
|
||||
// cleanup
|
||||
DB.delete(document);
|
||||
}
|
||||
|
||||
@Test
|
||||
@IgnorePlatform(Platform.ORACLE) // does not support uncommited reads
|
||||
public void tryWithResources_catch() {
|
||||
Document doc = null;
|
||||
try (Transaction transaction = DB.beginTransaction()) {
|
||||
|
||||
doc = new Document();
|
||||
doc.setTitle("tryWithResources_catch");
|
||||
doc.setBody("tryWithResources_catch_1");
|
||||
doc.save();
|
||||
|
||||
Document doc2 = new Document();
|
||||
doc2.setTitle("tryWithResources_catch");
|
||||
doc2.setBody("tryWithResources_catch_2");
|
||||
doc2.save();
|
||||
|
||||
transaction.commit();
|
||||
|
||||
} catch (DuplicateKeyException e) {
|
||||
|
||||
log.info("catch duplicate ... " + e);
|
||||
|
||||
Document doc3 = new Document();
|
||||
doc3.setTitle("tryWithResources_catch");
|
||||
doc3.setBody("tryWithResources_catch_3");
|
||||
doc3.save();
|
||||
|
||||
List<Document> docs = Document.find.query()
|
||||
.where().startsWith("body", "tryWithResources_catch")
|
||||
.asDraft()
|
||||
.findList();
|
||||
|
||||
assertThat(docs).hasSize(1);
|
||||
assertThat(doc).isNotNull();
|
||||
// Cleanup
|
||||
DB.delete(Document.class, doc.getId());
|
||||
DB.delete(Document.class, doc3.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import io.ebean.annotation.TxType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class TestTransactionalNever extends BaseTestCase {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TestTransactionalNever.class);
|
||||
|
||||
private Transaction outerTxn;
|
||||
|
||||
@Test
|
||||
public void testWithNever() {
|
||||
|
||||
new SomeTransactionalWithNever().doStuff();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBarfOnExisting() {
|
||||
assertThrows(PersistenceException.class, this::doWithTransaction);
|
||||
}
|
||||
|
||||
class SomeTransactionalWithNever {
|
||||
|
||||
@Transactional(type = TxType.NEVER)
|
||||
void doStuff() {
|
||||
outerTxn = DB.currentTransaction();
|
||||
log.info("currentTransaction ...{}", outerTxn);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
private void doWithTransaction() {
|
||||
|
||||
// always barf
|
||||
new SomeTransactionalWithNever().doStuff();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import io.ebean.annotation.TxType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
public class TestTransactionalNotSupports extends BaseTestCase {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TestTransactionalNotSupports.class);
|
||||
|
||||
private Transaction outerTxn;
|
||||
|
||||
private Transaction currentTxn;
|
||||
|
||||
@Test
|
||||
public void noCurrentTransaction() {
|
||||
|
||||
outerTxn = null;
|
||||
new SomeTransactionalWithNotSupported().doStuff();
|
||||
assertNull(outerTxn);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void withOuterTransaction_expect_currentTransaction_null_and_originalTxnRestored() {
|
||||
|
||||
outerTxn = null;
|
||||
DB.beginTransaction();
|
||||
try {
|
||||
currentTxn = DB.currentTransaction();
|
||||
assertNotNull(currentTxn);
|
||||
new SomeTransactionalWithNotSupported().doStuff();
|
||||
|
||||
// there was no current transaction inside the NOT_SUPPORTED
|
||||
assertNull(outerTxn);
|
||||
|
||||
// the original transaction was restored
|
||||
Transaction restored = DB.currentTransaction();
|
||||
assertSame(currentTxn, restored);
|
||||
} finally {
|
||||
DB.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
class SomeTransactionalWithNotSupported {
|
||||
|
||||
@Transactional(type = TxType.NOT_SUPPORTED)
|
||||
void doStuff() {
|
||||
outerTxn = DB.currentTransaction();
|
||||
log.info("outer ...{}", outerTxn);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import io.ebean.meta.MetaTimedMetric;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestTransactionalReadOnly extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test_readonly_datasource() {
|
||||
|
||||
resetAllMetrics();
|
||||
executeTransactionalUsingReadOnlyDataSource();
|
||||
|
||||
final List<MetaTimedMetric> timedMetrics = collectMetrics().timedMetrics();
|
||||
final Optional<MetaTimedMetric> txnReadOnly = metric(timedMetrics, "txn.readonly");
|
||||
assertThat(txnReadOnly.get().count()).isEqualTo(1);
|
||||
assertThat(metric(timedMetrics, "txn")).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_main_datasource() {
|
||||
|
||||
resetAllMetrics();
|
||||
executeTransactionalUsingMainDataSource();
|
||||
|
||||
final List<MetaTimedMetric> timedMetrics = collectMetrics().timedMetrics();
|
||||
final Optional<MetaTimedMetric> txnMain = metric(timedMetrics, "txn.main");
|
||||
assertThat(txnMain.get().count()).isEqualTo(1);
|
||||
assertThat(metric(timedMetrics, "txn.readonly")).isEmpty();
|
||||
}
|
||||
|
||||
private Optional<MetaTimedMetric> metric(List<MetaTimedMetric> timedMetrics, String name) {
|
||||
return timedMetrics.stream()
|
||||
.filter(metaTimedMetric -> metaTimedMetric.name().equals(name))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
private void executeTransactionalUsingReadOnlyDataSource() {
|
||||
DB.find(Customer.class).findCount();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
private void executeTransactionalUsingMainDataSource() {
|
||||
DB.find(Customer.class).findCount();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import io.ebean.annotation.TxType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
public class TestTransactionalRequired extends BaseTestCase {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TestTransactionalRequired.class);
|
||||
|
||||
private Transaction outerTxn;
|
||||
private Transaction innerTxn;
|
||||
private Transaction currentTxn;
|
||||
|
||||
@Test
|
||||
public void noCurrentTransaction() {
|
||||
|
||||
outerTxn = null;
|
||||
|
||||
assertNull(DB.currentTransaction());
|
||||
|
||||
new OuterTransactionalWithRequired().doOuter();
|
||||
|
||||
assertNull(DB.currentTransaction());
|
||||
|
||||
assertNotNull(outerTxn);
|
||||
assertNotNull(innerTxn);
|
||||
assertSame(outerTxn, innerTxn);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void withOuterBegin() {
|
||||
|
||||
outerTxn = null;
|
||||
DB.beginTransaction();
|
||||
try {
|
||||
currentTxn = DB.currentTransaction();
|
||||
assertNotNull(currentTxn);
|
||||
new OuterTransactionalWithRequired().doOuter();
|
||||
|
||||
// the original transaction was restored
|
||||
Transaction restored = DB.currentTransaction();
|
||||
assertSame(currentTxn, restored);
|
||||
assertSame(currentTxn, innerTxn);
|
||||
assertSame(currentTxn, outerTxn);
|
||||
} finally {
|
||||
DB.endTransaction();
|
||||
}
|
||||
|
||||
assertNull(DB.currentTransaction());
|
||||
}
|
||||
|
||||
class OuterTransactionalWithRequired {
|
||||
|
||||
@Transactional(type = TxType.REQUIRED)
|
||||
void doOuter() {
|
||||
outerTxn = DB.currentTransaction();
|
||||
log.info("outer ...{}", outerTxn);
|
||||
|
||||
new InTransactionalWithRequired().doInner();
|
||||
|
||||
Transaction current = DB.currentTransaction();
|
||||
assertSame(outerTxn, current);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class InTransactionalWithRequired {
|
||||
|
||||
@Transactional(type = TxType.REQUIRED)
|
||||
void doInner() {
|
||||
innerTxn = DB.currentTransaction();
|
||||
log.info("inner ...{}", innerTxn);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.TxScope;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import io.ebean.annotation.TxType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
public class TestTransactionalRequiresNew extends BaseTestCase {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TestTransactionalRequiresNew.class);
|
||||
|
||||
private Connection outerConn;
|
||||
private Transaction outerTxn;
|
||||
|
||||
@Test
|
||||
public void basic() {
|
||||
|
||||
outerTxn = null;
|
||||
assertNull(DB.currentTransaction());
|
||||
|
||||
new OuterTransactionalWithRequired().doOuter();
|
||||
|
||||
assertNull(DB.currentTransaction());
|
||||
assertNotNull(outerTxn);
|
||||
}
|
||||
|
||||
|
||||
class OuterTransactionalWithRequired {
|
||||
|
||||
@Transactional
|
||||
void doOuter() {
|
||||
outerTxn = DB.currentTransaction();
|
||||
log.info("outer before ...{}", outerTxn);
|
||||
outerConn = outerTxn.connection();
|
||||
|
||||
new InTransactionalWithRequiresNew().doInner();
|
||||
|
||||
// restore the outerTxn
|
||||
Transaction current = DB.currentTransaction();
|
||||
log.info("outer after ...{}", current);
|
||||
assertSame(outerConn, current.connection());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class InTransactionalWithRequiresNew {
|
||||
|
||||
@Transactional(type = TxType.REQUIRES_NEW)
|
||||
void doInner() {
|
||||
Transaction innerTxn = DB.currentTransaction();
|
||||
log.info("inner ...{} {}", innerTxn);
|
||||
|
||||
Connection connection = innerTxn.connection();
|
||||
assertNotSame(connection, outerConn);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDifferentCreation1() {
|
||||
Database server = server();
|
||||
try(Transaction txn = server.beginTransaction()) {
|
||||
String txnName = txn.toString();
|
||||
DB.beginTransaction(TxScope.requiresNew());
|
||||
try {
|
||||
DB.commitTransaction();
|
||||
} finally {
|
||||
DB.endTransaction();
|
||||
}
|
||||
assertThat(txn.toString()).isEqualTo(txnName);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDifferentCreation2() {
|
||||
Database server = server();
|
||||
try (Transaction txn = server.beginTransaction()) {
|
||||
String txnName = txn.toString();
|
||||
server.beginTransaction(TxScope.requiresNew());
|
||||
try {
|
||||
server.commitTransaction();
|
||||
} finally {
|
||||
server.endTransaction();
|
||||
}
|
||||
assertThat(txn.toString()).isEqualTo(txnName);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDifferentCreation3() {
|
||||
Database server = server();
|
||||
try (Transaction txn = server.beginTransaction()) {
|
||||
String txnName = txn.toString();
|
||||
Transaction txn2 = DB.beginTransaction(TxScope.requiresNew());
|
||||
try {
|
||||
txn2.commit();
|
||||
} finally {
|
||||
txn2.end();
|
||||
}
|
||||
assertThat(txn.toString()).isEqualTo(txnName);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDifferentCreation4() {
|
||||
Database server = server();
|
||||
try (Transaction txn = server.beginTransaction()) {
|
||||
String txnName = txn.toString();
|
||||
server.beginTransaction(TxScope.requiresNew());
|
||||
try {
|
||||
Transaction.current().commitAndContinue();
|
||||
} finally {
|
||||
Transaction.current().end();
|
||||
}
|
||||
assertThat(txn.toString()).isEqualTo(txnName);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import io.ebean.annotation.TxType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tests.model.basic.EBasic;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class TestTransactionalSupports extends BaseTestCase {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TestTransactionalSupports.class);
|
||||
|
||||
private Transaction outerTxn;
|
||||
|
||||
@Test
|
||||
public void noCurrentTransaction() {
|
||||
|
||||
outerTxn = null;
|
||||
new SomeTransactionalWithSupports().doStuff();
|
||||
assertNull(outerTxn);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void withOuterTransaction_expect_currentTransactionAvailable() {
|
||||
|
||||
outerTxn = null;
|
||||
DB.beginTransaction();
|
||||
try {
|
||||
new SomeTransactionalWithSupports().doStuff();
|
||||
assertNotNull(outerTxn);
|
||||
} finally {
|
||||
DB.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supports_withInternalSave() {
|
||||
|
||||
int got = new SomeTransactionalWithSupportsAndSave().doStuff();
|
||||
assertThat(got).isEqualTo(42);
|
||||
}
|
||||
|
||||
class SomeTransactionalWithSupports {
|
||||
|
||||
@Transactional(type = TxType.SUPPORTS)
|
||||
void doStuff() {
|
||||
outerTxn = DB.currentTransaction();
|
||||
log.info("outer ...{}", outerTxn);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class SomeTransactionalWithSupportsAndSave {
|
||||
|
||||
@Transactional(type = TxType.SUPPORTS)
|
||||
int doStuff() {
|
||||
|
||||
EBasic basic = new EBasic();
|
||||
basic.setName("checkSupports");
|
||||
|
||||
// effective creates a transaction replacing the 'No active transaction' placeholder
|
||||
DB.save(basic);
|
||||
|
||||
return 42;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tests.model.basic.EBasicVer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
|
||||
public class TestTransactionalWhenThrow {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TestTransactionalWhenThrow.class);
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
EBasicVer bean = new EBasicVer("doTransactional");
|
||||
|
||||
assertThatCode(() -> doTransactional(bean))
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessageContaining("InnerFailWithSomething");
|
||||
|
||||
final EBasicVer found = DB.find(EBasicVer.class, bean.getId());
|
||||
assertThat(found).isNull();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
private void doTransactional(EBasicVer bean) {
|
||||
DB.save(bean);
|
||||
failWithSomething();
|
||||
}
|
||||
|
||||
private void failWithSomething() {
|
||||
EBasicVer bean = new EBasicVer("failWithSomething");
|
||||
DB.save(bean);
|
||||
throw new IllegalStateException("InnerFailWithSomething");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDepth3FailWithCatch() {
|
||||
|
||||
EBasicVer outer = new EBasicVer("testDepth3FailWithCatch");
|
||||
|
||||
assertThatCode(() -> doOuterTransactional(outer))
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessageContaining("Transaction is Inactive");
|
||||
|
||||
final EBasicVer found = DB.find(EBasicVer.class, outer.getId());
|
||||
assertThat(found).isNull();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
void doOuterTransactional(EBasicVer outer) {
|
||||
DB.save(outer);
|
||||
doInnerTransactionWithCatch();
|
||||
// exiting this throws IllegalStateException("Transaction is Inactive")
|
||||
// as doInnerTransactionWithCatch() has caught and not thrown
|
||||
}
|
||||
|
||||
@Transactional
|
||||
private void doInnerTransactionWithCatch() {
|
||||
try {
|
||||
EBasicVer bean = new EBasicVer("doInnerTransactionWithCatch");
|
||||
DB.save(bean);
|
||||
doInnerInner();
|
||||
} catch (Throwable e) {
|
||||
// at this point doInnerInner() has rolled back transaction and marked as inactive
|
||||
log.error("Catch internal error (and not thrown) so caller is unaware", e);
|
||||
// catching and not throwing here means the calling @Transactional method
|
||||
// doOuterTransactional() isn't aware that the transaction has been
|
||||
// rolled back already. It will then exit with:
|
||||
// IllegalStateException("Transaction is Inactive");
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
void doInnerInner() {
|
||||
EBasicVer bean = new EBasicVer("doInnerInner");
|
||||
DB.save(bean);
|
||||
failWithSomething();
|
||||
// exiting this method rolls back transaction
|
||||
}
|
||||
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.PersistenceException;
|
||||
import javax.persistence.Table;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* It shows the issue that the user record does NOT rolback when there is an exception thrown
|
||||
*/
|
||||
public class TransactionNotTerminatedAfterRollback {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(TransactionNotTerminatedAfterRollback.class);
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
try {
|
||||
new UserService().create(new User(1L, "David"));
|
||||
fail("Exception should be thrown");
|
||||
} catch (PersistenceException pe) {
|
||||
LOG.error("e: " + pe);
|
||||
}
|
||||
List<User> users = DB.find(User.class).findList();
|
||||
LOG.debug("users: {}", users);
|
||||
assertThat(users).isEmpty();
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = PersistenceException.class)
|
||||
public class UserService {
|
||||
public void create(User i) {
|
||||
DB.save(i);
|
||||
DB.save(new User(1L, "Peter")); // make it throw exception and rollback
|
||||
}
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Table(name = "tx_user")
|
||||
public static class User {
|
||||
@Id
|
||||
Long id;
|
||||
String name;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder s = new StringBuilder();
|
||||
s.append("{");
|
||||
s.append("id: ").append(id).append(", ");
|
||||
s.append("name: ").append(name);
|
||||
s.append("}");
|
||||
return s.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user