Search in sources :

Example 1 with JtxException

use of jodd.jtx.JtxException 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 2 with JtxException

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

the class DbJtxResourceManager method rollbackTransaction.

/**
	 * {@inheritDoc}
	 */
public void rollbackTransaction(DbSession resource) {
    try {
        if (resource.isTransactionActive()) {
            log.debug("rollback tx");
            resource.rollbackTransaction();
        }
    } catch (Exception ex) {
        throw new JtxException(ex);
    } finally {
        resource.closeSession();
    }
}
Also used : JtxException(jodd.jtx.JtxException) JtxException(jodd.jtx.JtxException)

Example 3 with JtxException

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

the class DbTransactionTest method testTime.

// ---------------------------------------------------------------- test time
@Test
public void testTime() {
    JtxTransactionManager manager = new JtxTransactionManager();
    manager.registerResourceManager(new DbJtxResourceManager(cp));
    JtxTransaction tx1 = manager.requestTransaction(new JtxTransactionMode().propagationRequired().transactionTimeout(1));
    DbSession session1 = tx1.requestResource(DbSession.class);
    assertNotNull(session1);
    executeCount(session1, "select count(*) from GIRL");
    ThreadUtil.sleep(2000);
    try {
        DbSession session2 = tx1.requestResource(DbSession.class);
        assertNotNull(session2);
        assertSame(session1, session2);
        executeCount(session1, "select count(*) from GIRL");
        fail();
    } catch (JtxException ignore) {
    }
    tx1.rollback();
}
Also used : DbJtxResourceManager(jodd.db.jtx.DbJtxResourceManager) JtxTransactionMode(jodd.jtx.JtxTransactionMode) JtxException(jodd.jtx.JtxException) DbJtxTransaction(jodd.db.jtx.DbJtxTransaction) JtxTransaction(jodd.jtx.JtxTransaction) JtxTransactionManager(jodd.jtx.JtxTransactionManager) Test(org.junit.Test)

Example 4 with JtxException

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

the class DbPropagationTest method testRequiredToNever.

@Test
public void testRequiredToNever() {
    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, never
    try {
        worker.maybeRequestTransaction(never(), 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)

Aggregations

JtxException (jodd.jtx.JtxException)4 JtxTransaction (jodd.jtx.JtxTransaction)3 Test (org.junit.Test)3 DbJtxSessionProvider (jodd.db.jtx.DbJtxSessionProvider)2 LeanJtxWorker (jodd.jtx.worker.LeanJtxWorker)2 DbJtxResourceManager (jodd.db.jtx.DbJtxResourceManager)1 DbJtxTransaction (jodd.db.jtx.DbJtxTransaction)1 JtxTransactionManager (jodd.jtx.JtxTransactionManager)1 JtxTransactionMode (jodd.jtx.JtxTransactionMode)1