Search in sources :

Example 1 with LeanJtxWorker

use of jodd.jtx.worker.LeanJtxWorker in project jodd by oblac.

the class DbPropagationTest method testSupportsToNeverCommit.

@Test
public void testSupportsToNeverCommit() {
    LeanJtxWorker worker = new LeanJtxWorker(dbtxm);
    DbJtxSessionProvider sessionProvider = new DbJtxSessionProvider(worker.getTransactionManager());
    // session #1: supports
    JtxTransaction jtx = worker.maybeRequestTransaction(supports(), CTX_1);
    assertNotNull(jtx);
    assertFalse(jtx.isActive());
    DbSession session1 = sessionProvider.getDbSession();
    executeUpdate(session1, "insert into GIRL values(1, 'Sophia', null)");
    assertFalse(jtx.isActive());
    assertFalse(jtx.isCommitted());
    assertTrue(jtx.isNoTransaction());
    // session #2: inner, supports
    JtxTransaction jtx2 = worker.maybeRequestTransaction(never(), CTX_2);
    assertNull(jtx2);
    DbSession session2 = sessionProvider.getDbSession();
    assertSame(session1, session2);
    executeUpdate(session2, "insert into GIRL values(2, 'Gloria', null)");
    assertFalse(worker.maybeCommitTransaction(jtx2));
    assertFalse(jtx.isActive());
    // session #1: commit
    assertTrue(worker.maybeCommitTransaction(jtx));
    assertFalse(jtx.isActive());
    assertTrue(jtx.isCommitted());
    // test
    session1 = new DbSession(cp);
    assertEquals(1, executeCount(session1, "select count(*) from GIRL where id = 1"));
    assertEquals(1, executeCount(session1, "select count(*) from GIRL where id = 2"));
    session1.closeSession();
}
Also used : LeanJtxWorker(jodd.jtx.worker.LeanJtxWorker) DbJtxSessionProvider(jodd.db.jtx.DbJtxSessionProvider) JtxTransaction(jodd.jtx.JtxTransaction) Test(org.junit.Test)

Example 2 with LeanJtxWorker

use of jodd.jtx.worker.LeanJtxWorker in project jodd by oblac.

the class DbPropagationTest method testNotSupported.

@Test
public void testNotSupported() {
    LeanJtxWorker worker = new LeanJtxWorker(dbtxm);
    DbJtxSessionProvider sessionProvider = new DbJtxSessionProvider(worker.getTransactionManager());
    // session #1: not supported - commit
    JtxTransaction jtx = worker.maybeRequestTransaction(notSupported(), CTX_1);
    assertNotNull(jtx);
    DbSession session = sessionProvider.getDbSession();
    executeUpdate(session, "insert into GIRL values(1, 'Sophia', null)");
    assertFalse(jtx.isActive());
    assertTrue(jtx.isNoTransaction());
    assertTrue(worker.maybeCommitTransaction(jtx));
    assertFalse(jtx.isActive());
    assertTrue(jtx.isCommitted());
    // session #2: not supported - rollback
    jtx = worker.maybeRequestTransaction(notSupported(), CTX_2);
    assertNotNull(jtx);
    session = sessionProvider.getDbSession();
    assertFalse(jtx.isActive());
    assertTrue(jtx.isNoTransaction());
    executeUpdate(session, "insert into GIRL values(2, 'Gloria', null)");
    assertTrue(worker.markOrRollbackTransaction(jtx, null));
    // test
    session = new DbSession(cp);
    assertEquals(1, executeCount(session, "select count(*) from GIRL where id = 1"));
    assertEquals(1, executeCount(session, "select count(*) from GIRL where id = 2"));
    session.closeSession();
}
Also used : LeanJtxWorker(jodd.jtx.worker.LeanJtxWorker) DbJtxSessionProvider(jodd.db.jtx.DbJtxSessionProvider) JtxTransaction(jodd.jtx.JtxTransaction) Test(org.junit.Test)

Example 3 with LeanJtxWorker

use of jodd.jtx.worker.LeanJtxWorker in project jodd by oblac.

the class DbPropagationTest method testSupportsToRequiredCommit.

@Test
public void testSupportsToRequiredCommit() {
    LeanJtxWorker worker = new LeanJtxWorker(dbtxm);
    DbJtxSessionProvider sessionProvider = new DbJtxSessionProvider(worker.getTransactionManager());
    // session #1: supports
    JtxTransaction jtx = worker.maybeRequestTransaction(supports(), CTX_1);
    assertNotNull(jtx);
    assertFalse(jtx.isActive());
    DbSession session1 = sessionProvider.getDbSession();
    executeUpdate(session1, "insert into GIRL values(1, 'Sophia', null)");
    assertFalse(jtx.isActive());
    assertFalse(jtx.isCommitted());
    assertTrue(jtx.isNoTransaction());
    // session #2: inner, required
    JtxTransaction jtx2 = worker.maybeRequestTransaction(required(), CTX_2);
    assertNotNull(jtx2);
    DbSession session2 = sessionProvider.getDbSession();
    assertNotSame(session1, session2);
    executeUpdate(session2, "insert into GIRL values(2, 'Gloria', null)");
    assertTrue(worker.maybeCommitTransaction(jtx2));
    assertFalse(jtx2.isActive());
    // session #1: rollback
    assertTrue(worker.markOrRollbackTransaction(jtx, null));
    assertFalse(jtx.isActive());
    assertFalse(jtx.isCommitted());
    // test
    session1 = new DbSession(cp);
    assertEquals(1, executeCount(session1, "select count(*) from GIRL where id = 1"));
    assertEquals(1, executeCount(session1, "select count(*) from GIRL where id = 2"));
    session1.closeSession();
}
Also used : LeanJtxWorker(jodd.jtx.worker.LeanJtxWorker) DbJtxSessionProvider(jodd.db.jtx.DbJtxSessionProvider) JtxTransaction(jodd.jtx.JtxTransaction) Test(org.junit.Test)

Example 4 with LeanJtxWorker

use of jodd.jtx.worker.LeanJtxWorker in project jodd by oblac.

the class DbPropagationTest method testSupportsToMandatory.

@Test
public void testSupportsToMandatory() {
    LeanJtxWorker worker = new LeanJtxWorker(dbtxm);
    DbJtxSessionProvider sessionProvider = new DbJtxSessionProvider(worker.getTransactionManager());
    // session #1: supports
    JtxTransaction jtx = worker.maybeRequestTransaction(supports(), CTX_1);
    assertNotNull(jtx);
    assertFalse(jtx.isActive());
    DbSession session1 = sessionProvider.getDbSession();
    executeUpdate(session1, "insert into GIRL values(1, 'Sophia', null)");
    assertFalse(jtx.isActive());
    assertFalse(jtx.isCommitted());
    assertTrue(jtx.isNoTransaction());
    // session #2: inner, mandatory
    try {
        worker.maybeRequestTransaction(mandatory(), CTX_2);
        fail();
    } catch (JtxException ignore) {
    }
}
Also used : LeanJtxWorker(jodd.jtx.worker.LeanJtxWorker) DbJtxSessionProvider(jodd.db.jtx.DbJtxSessionProvider) JtxException(jodd.jtx.JtxException) JtxTransaction(jodd.jtx.JtxTransaction) Test(org.junit.Test)

Example 5 with LeanJtxWorker

use of jodd.jtx.worker.LeanJtxWorker in project jodd by oblac.

the class DbPropagationTest method testRequiredToRequiredRollback.

@Test
public void testRequiredToRequiredRollback() {
    LeanJtxWorker worker = new LeanJtxWorker(dbtxm);
    DbJtxSessionProvider sessionProvider = new DbJtxSessionProvider(worker.getTransactionManager());
    // session #1: required
    JtxTransaction jtx = worker.maybeRequestTransaction(required(), CTX_1);
    assertNotNull(jtx);
    DbSession session1 = sessionProvider.getDbSession();
    executeUpdate(session1, "insert into GIRL values(1, 'Sophia', null)");
    assertTrue(jtx.isActive());
    assertFalse(jtx.isCommitted());
    assertFalse(jtx.isNoTransaction());
    // session #2: inner, required
    JtxTransaction jtx2 = worker.maybeRequestTransaction(required(), CTX_2);
    assertNull(jtx2);
    DbSession session2 = sessionProvider.getDbSession();
    assertSame(session1, session2);
    executeUpdate(session2, "insert into GIRL values(2, 'Gloria', null)");
    assertFalse(worker.markOrRollbackTransaction(jtx2, null));
    assertFalse(jtx.isActive());
    assertTrue(jtx.isRollbackOnly());
    // session #1: commit
    assertTrue(worker.markOrRollbackTransaction(jtx, null));
    assertFalse(jtx.isActive());
    assertFalse(jtx.isCommitted());
    assertTrue(jtx.isRolledback());
    // test
    session1 = new DbSession(cp);
    assertEquals(0, executeCount(session1, "select count(*) from GIRL where id = 1"));
    assertEquals(0, executeCount(session1, "select count(*) from GIRL where id = 2"));
    session1.closeSession();
}
Also used : LeanJtxWorker(jodd.jtx.worker.LeanJtxWorker) DbJtxSessionProvider(jodd.db.jtx.DbJtxSessionProvider) JtxTransaction(jodd.jtx.JtxTransaction) Test(org.junit.Test)

Aggregations

LeanJtxWorker (jodd.jtx.worker.LeanJtxWorker)14 Test (org.junit.Test)14 DbJtxSessionProvider (jodd.db.jtx.DbJtxSessionProvider)13 JtxTransaction (jodd.jtx.JtxTransaction)13 JtxException (jodd.jtx.JtxException)2 WorkSession (jodd.jtx.data.WorkSession)1