Omit Transaction.start() in favor of ctx.startTx().
This commit is contained in:
@@ -216,6 +216,21 @@ public class PersistenceContext
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts a transaction bound to this persistence context. You <em>must</em> eventually call
|
||||||
|
* {@link Transaction#commit} or {@link Transaction#rollback} on the returned transaction. Also
|
||||||
|
* no other database operations may be performed on other persitence contexts, on the calling
|
||||||
|
* thread, until this transaction is complete.
|
||||||
|
*/
|
||||||
|
public Transaction startTx ()
|
||||||
|
{
|
||||||
|
Transaction tx = Transaction.get();
|
||||||
|
if (tx != null) throw new DatabaseException("Nested transactions not supported.");
|
||||||
|
tx = new Transaction(this);
|
||||||
|
Transaction._activeTx.set(tx);
|
||||||
|
return tx;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a snapshot of our current runtime statistics.
|
* Returns a snapshot of our current runtime statistics.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -47,19 +47,6 @@ import java.sql.SQLException;
|
|||||||
*/
|
*/
|
||||||
public class Transaction {
|
public class Transaction {
|
||||||
|
|
||||||
/**
|
|
||||||
* Starts a transaction using the supplied persistence context. You <em>must</em> eventually
|
|
||||||
* call {@link #commit} or {@link #rollback} on the returned transaction.
|
|
||||||
*/
|
|
||||||
public static Transaction start (PersistenceContext ctx)
|
|
||||||
{
|
|
||||||
Transaction tx = get();
|
|
||||||
if (tx != null) throw new DatabaseException("Nested transactions not supported.");
|
|
||||||
tx = new Transaction(ctx);
|
|
||||||
_activeTx.set(tx);
|
|
||||||
return tx;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the currently active transaction (on the caller's thread), or null.
|
* Returns the currently active transaction (on the caller's thread), or null.
|
||||||
*/
|
*/
|
||||||
@@ -82,7 +69,7 @@ public class Transaction {
|
|||||||
*/
|
*/
|
||||||
public static void perform (PersistenceContext ctx, Runnable op)
|
public static void perform (PersistenceContext ctx, Runnable op)
|
||||||
{
|
{
|
||||||
Transaction tx = start(ctx);
|
Transaction tx = ctx.startTx();
|
||||||
try {
|
try {
|
||||||
op.run();
|
op.run();
|
||||||
tx.commit();
|
tx.commit();
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import static org.junit.Assert.*;
|
|||||||
public class TransactionTest extends TestBase
|
public class TransactionTest extends TestBase
|
||||||
{
|
{
|
||||||
@Test public void testSuccessfulCommit () {
|
@Test public void testSuccessfulCommit () {
|
||||||
Transaction tx = Transaction.start(_repo.ctx());
|
Transaction tx = _repo.ctx().startTx();
|
||||||
TestRecord in = createTestRecord(1);
|
TestRecord in = createTestRecord(1);
|
||||||
_repo.insert(in);
|
_repo.insert(in);
|
||||||
tx.commit();
|
tx.commit();
|
||||||
@@ -28,7 +28,7 @@ public class TransactionTest extends TestBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test public void testRollback () {
|
@Test public void testRollback () {
|
||||||
Transaction tx = Transaction.start(_repo.ctx());
|
Transaction tx = _repo.ctx().startTx();
|
||||||
TestRecord in1 = createTestRecord(1);
|
TestRecord in1 = createTestRecord(1);
|
||||||
_repo.insert(in1);
|
_repo.insert(in1);
|
||||||
TestRecord in2 = createTestRecord(2);
|
TestRecord in2 = createTestRecord(2);
|
||||||
|
|||||||
Reference in New Issue
Block a user