Search in sources :

Example 16 with JtxTransaction

use of jodd.jtx.JtxTransaction in project jodd by oblac.

the class DbPropagationTest method testRequired.

@Test
public void testRequired() {
    LeanJtxWorker worker = new LeanJtxWorker(dbtxm);
    DbJtxSessionProvider sessionProvider = new DbJtxSessionProvider(worker.getTransactionManager());
    // session #1: required - commit
    JtxTransaction jtx = worker.maybeRequestTransaction(required(), CTX_1);
    assertTrue(jtx.isActive());
    assertNotNull(jtx);
    DbSession session = sessionProvider.getDbSession();
    executeUpdate(session, "insert into GIRL values(1, 'Sophia', null)");
    assertTrue(jtx.isActive());
    assertFalse(jtx.isCommitted());
    assertFalse(jtx.isNoTransaction());
    assertTrue(worker.maybeCommitTransaction(jtx));
    assertFalse(jtx.isActive());
    assertTrue(jtx.isCommitted());
    assertFalse(jtx.isNoTransaction());
    // session #2: required - rollback
    jtx = worker.maybeRequestTransaction(required(), CTX_1);
    assertNotNull(jtx);
    session = sessionProvider.getDbSession();
    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(0, 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 17 with JtxTransaction

use of jodd.jtx.JtxTransaction in project jodd by oblac.

the class DbPropagationTest method testSupportsToSupportsCommit.

@Test
public void testSupportsToSupportsCommit() {
    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(supports(), 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 18 with JtxTransaction

use of jodd.jtx.JtxTransaction in project jodd by oblac.

the class DbPropagationTest method testRequiredToMandatoryCommit.

@Test
public void testRequiredToMandatoryCommit() {
    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, mandatory
    JtxTransaction jtx2 = worker.maybeRequestTransaction(mandatory(), CTX_2);
    assertNull(jtx2);
    DbSession session2 = sessionProvider.getDbSession();
    assertSame(session1, session2);
    executeUpdate(session2, "insert into GIRL values(2, 'Gloria', null)");
    assertFalse(worker.maybeCommitTransaction(jtx2));
    assertTrue(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 19 with JtxTransaction

use of jodd.jtx.JtxTransaction in project jodd by oblac.

the class DbTransactionTest method testThread.

// ---------------------------------------------------------------- thread
@Test
public void testThread() {
    final JtxTransactionManager manager = new JtxTransactionManager();
    manager.registerResourceManager(new DbJtxResourceManager(cp));
    final int[] count = new int[1];
    new Thread() {

        @Override
        public void run() {
            JtxTransaction tx = manager.requestTransaction(new JtxTransactionMode().propagationRequired().transactionTimeout(1));
            count[0]++;
            assertEquals(count[0], manager.totalTransactions());
            assertEquals(1, manager.totalTransactions());
            assertEquals(1, manager.totalThreadTransactions());
            ThreadUtil.sleep(1000);
            tx.commit();
            count[0]--;
        }
    }.start();
    ThreadUtil.sleep(500);
    new Thread() {

        @Override
        public void run() {
            JtxTransaction tx = manager.requestTransaction(new JtxTransactionMode().propagationRequired().transactionTimeout(1));
            count[0]++;
            assertEquals(count[0], manager.totalTransactions());
            assertEquals(2, manager.totalTransactions());
            assertEquals(1, manager.totalThreadTransactions());
            ThreadUtil.sleep(1000);
            tx.commit();
            count[0]--;
        }
    }.start();
}
Also used : DbJtxResourceManager(jodd.db.jtx.DbJtxResourceManager) JtxTransactionMode(jodd.jtx.JtxTransactionMode) DbJtxTransaction(jodd.db.jtx.DbJtxTransaction) JtxTransaction(jodd.jtx.JtxTransaction) JtxTransactionManager(jodd.jtx.JtxTransactionManager) Test(org.junit.Test)

Example 20 with JtxTransaction

use of jodd.jtx.JtxTransaction in project jodd by oblac.

the class AnnotationTxAdvice method execute.

public Object execute() throws Exception {
    Class type = targetClass();
    String methodName = targetMethodName();
    Class[] methodArgsTypes = createArgumentsClassArray();
    String methodDescription = targetMethodDescription();
    // read transaction mode from annotation
    JtxTransactionMode txMode = manager.getTxMode(type, methodName, methodArgsTypes, methodDescription);
    // request transaction
    JtxTransaction tx = null;
    try {
        String scope = manager.resolveScope(type, methodName);
        tx = manager.getJtxWorker().maybeRequestTransaction(txMode, scope);
        Object result = invoke();
        manager.getJtxWorker().maybeCommitTransaction(tx);
        return result;
    } catch (Exception ex) {
        manager.getJtxWorker().markOrRollbackTransaction(tx, ex);
        throw ex;
    }
}
Also used : JtxTransactionMode(jodd.jtx.JtxTransactionMode) ProxyTarget.targetClass(jodd.proxetta.ProxyTarget.targetClass) JtxTransaction(jodd.jtx.JtxTransaction)

Aggregations

JtxTransaction (jodd.jtx.JtxTransaction)21 Test (org.junit.Test)18 DbJtxSessionProvider (jodd.db.jtx.DbJtxSessionProvider)13 LeanJtxWorker (jodd.jtx.worker.LeanJtxWorker)13 DbJtxTransaction (jodd.db.jtx.DbJtxTransaction)5 JtxTransactionMode (jodd.jtx.JtxTransactionMode)5 DbJtxResourceManager (jodd.db.jtx.DbJtxResourceManager)4 JtxException (jodd.jtx.JtxException)4 JtxTransactionManager (jodd.jtx.JtxTransactionManager)4 SQLException (java.sql.SQLException)1 AppDao (jodd.joy.db.AppDao)1 Madvoc (jodd.madvoc.Madvoc)1 ProxyTarget.targetClass (jodd.proxetta.ProxyTarget.targetClass)1