mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
No effective change - Modify some tests to use transaction try with resources
This commit is contained in:
@@ -2,12 +2,13 @@ package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Transaction;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.EBasicVer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -18,8 +19,7 @@ public class TestLogTransLogOnError extends BaseTestCase {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Ebean.beginTransaction();
|
||||
try {
|
||||
try (Transaction txn = Ebean.beginTransaction()) {
|
||||
|
||||
Ebean.find(Customer.class).findList();
|
||||
Ebean.find(Order.class).where().gt("id", 1).findList();
|
||||
@@ -35,13 +35,11 @@ public class TestLogTransLogOnError extends BaseTestCase {
|
||||
Assert.assertEquals(0, list.size());
|
||||
// Get here with mysql?
|
||||
// Assert.assertTrue(false);
|
||||
txn.commit();
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
// e.printStackTrace();
|
||||
Assert.assertTrue(true);
|
||||
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,8 +47,7 @@ public class TestLogTransLogOnError extends BaseTestCase {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Ebean.beginTransaction();
|
||||
try {
|
||||
try (Transaction txn = Ebean.beginTransaction()) {
|
||||
Ebean.find(Customer.class).findList();
|
||||
|
||||
EBasicVer newBean = new EBasicVer("aName");
|
||||
@@ -65,14 +62,11 @@ public class TestLogTransLogOnError extends BaseTestCase {
|
||||
|
||||
// never get here
|
||||
Assert.assertTrue(false);
|
||||
txn.commit();
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
// e.printStackTrace();
|
||||
Assert.assertTrue(true);
|
||||
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,12 @@ package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Transaction;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.tests.model.basic.OCar;
|
||||
import org.tests.model.basic.OEngine;
|
||||
import org.tests.model.basic.OGearBox;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestMultipleOneToOneIUD extends BaseTestCase {
|
||||
|
||||
@@ -25,19 +26,14 @@ public class TestMultipleOneToOneIUD extends BaseTestCase {
|
||||
car.setName("test car");
|
||||
car.setEngine(engine);
|
||||
|
||||
Ebean.beginTransaction();
|
||||
try {
|
||||
try (Transaction txn = Ebean.beginTransaction()) {
|
||||
Ebean.save(gearBox);
|
||||
Ebean.save(car);
|
||||
|
||||
Assert.assertNotNull(car.getId());
|
||||
Assert.assertNotNull(engine.getEngineId());
|
||||
Assert.assertNotNull(gearBox.getId());
|
||||
|
||||
Ebean.commitTransaction();
|
||||
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
txn.commit();
|
||||
}
|
||||
|
||||
OCar c2 = Ebean.find(OCar.class, car.getId());
|
||||
|
||||
@@ -29,10 +29,10 @@ public class TestQueryForUpdate extends BaseTestCase {
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class)
|
||||
.forUpdate()
|
||||
.order().desc("id");
|
||||
.forUpdate()
|
||||
.order().desc("id");
|
||||
|
||||
query.findList();
|
||||
query.findList();
|
||||
if (isSqlServer()) {
|
||||
assertThat(sqlOf(query)).contains("with (updlock)");
|
||||
} else {
|
||||
@@ -48,25 +48,19 @@ public class TestQueryForUpdate extends BaseTestCase {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Ebean.beginTransaction();
|
||||
try {
|
||||
Query<Customer> query = Ebean.find(Customer.class)
|
||||
.forUpdateNoWait()
|
||||
.order().desc("id");
|
||||
Query<Customer> query = Ebean.find(Customer.class)
|
||||
.forUpdateNoWait()
|
||||
.order().desc("id");
|
||||
|
||||
query.findList();
|
||||
if (isOracle()) {
|
||||
assertThat(sqlOf(query)).contains("for update nowait");
|
||||
} else if (isH2()) {
|
||||
assertThat(sqlOf(query)).contains("for update");
|
||||
} else if (isSqlServer()) {
|
||||
assertThat(sqlOf(query)).contains("with (updlock,nowait)");
|
||||
} else {
|
||||
assertThat(sqlOf(query)).contains("for update nowait");
|
||||
}
|
||||
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
query.findList();
|
||||
if (isOracle()) {
|
||||
assertThat(sqlOf(query)).contains("for update nowait");
|
||||
} else if (isH2()) {
|
||||
assertThat(sqlOf(query)).contains("for update");
|
||||
} else if (isSqlServer()) {
|
||||
assertThat(sqlOf(query)).contains("with (updlock,nowait)");
|
||||
} else {
|
||||
assertThat(sqlOf(query)).contains("for update nowait");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,42 +74,36 @@ public class TestQueryForUpdate extends BaseTestCase {
|
||||
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
|
||||
Ebean.beginTransaction();
|
||||
try {
|
||||
Query<Customer> query = Ebean.find(Customer.class)
|
||||
.forUpdateNoWait()
|
||||
.setMaxRows(1)
|
||||
.order().desc("id");
|
||||
try (Transaction txn = Ebean.beginTransaction()) {
|
||||
Query<Customer> query = Ebean.find(Customer.class)
|
||||
.forUpdateNoWait()
|
||||
.setMaxRows(1)
|
||||
.order().desc("id");
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
Customer first = list.get(0);
|
||||
if (isSqlServer()) {
|
||||
assertThat(sqlOf(query)).contains("with (updlock,nowait)");
|
||||
} else if (isH2()){
|
||||
assertThat(sqlOf(query)).contains("for update");
|
||||
} else {
|
||||
assertThat(sqlOf(query)).contains("for update nowait");
|
||||
}
|
||||
// create a 2nd transaction to test that the
|
||||
// row is locked and we can't acquire it
|
||||
Transaction txn2 = server.createTransaction();
|
||||
try {
|
||||
logger.info("... attempt another acquire using 2nd transaction");
|
||||
Query<Customer> query2 =
|
||||
server.find(Customer.class)
|
||||
.where().idEq(first.getId())
|
||||
.forUpdateNoWait();
|
||||
List<Customer> list = query.findList();
|
||||
Customer first = list.get(0);
|
||||
if (isSqlServer()) {
|
||||
assertThat(sqlOf(query)).contains("with (updlock,nowait)");
|
||||
} else if (isH2()) {
|
||||
assertThat(sqlOf(query)).contains("for update");
|
||||
} else {
|
||||
assertThat(sqlOf(query)).contains("for update nowait");
|
||||
}
|
||||
// create a 2nd transaction to test that the
|
||||
// row is locked and we can't acquire it
|
||||
try (Transaction txn2 = server.createTransaction()) {
|
||||
logger.info("... attempt another acquire using 2nd transaction");
|
||||
Query<Customer> query2 =
|
||||
server.find(Customer.class)
|
||||
.where().idEq(first.getId())
|
||||
.forUpdateNoWait();
|
||||
|
||||
server.findOne(query2, txn2);
|
||||
assertTrue(false); // never get here
|
||||
} catch (AcquireLockException e) {
|
||||
logger.info("... got AcquireLockException " + e);
|
||||
} finally {
|
||||
txn2.end();
|
||||
}
|
||||
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
server.findOne(query2, txn2);
|
||||
assertTrue(false); // never get here
|
||||
} catch (AcquireLockException e) {
|
||||
logger.info("... got AcquireLockException " + e);
|
||||
}
|
||||
txn.commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user