Search in sources :

Example 6 with JtxTransactionMode

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

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

the class DbTransactionTest method service0.

void service0() {
    assertTotals(0, 0);
    DbJtxTransaction tx = dbtxm.requestTransaction(new JtxTransactionMode().propagationRequired());
    assertTotals(1, 1);
    s0 = tx.requestResource();
    service0_1(tx);
    tx.commit();
    assertTotals(0, 0);
}
Also used : DbJtxTransaction(jodd.db.jtx.DbJtxTransaction) JtxTransactionMode(jodd.jtx.JtxTransactionMode)

Example 8 with JtxTransactionMode

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

the class DbTransactionTest method testRollback.

/**
	 * Tests if rollback works.
	 */
@Test
public void testRollback() throws SQLException {
    // prepare manager
    JtxTransactionManager manager = new JtxTransactionManager();
    manager.registerResourceManager(new DbJtxResourceManager(cp));
    // request transaction
    JtxTransaction tx = manager.requestTransaction(new JtxTransactionMode().propagationRequired().readOnly(false));
    DbSession session = tx.requestResource(DbSession.class);
    assertNotNull(session);
    // insert two records
    DbQuery query = new DbQuery(session, "insert into GIRL values(4, 'Jeniffer', 'fighting')");
    assertEquals(1, query.executeUpdate());
    query = new DbQuery(session, "insert into GIRL values(5, 'Annita', 'bartender')");
    assertEquals(1, query.executeUpdate());
    // rollback
    tx.rollback();
    // check !!!
    session = new DbSession(cp);
    DbQuery query2 = new DbQuery(session, "select count(*) from GIRL");
    long count = query2.executeCount();
    assertEquals(0, count);
    session.closeSession();
}
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 9 with JtxTransactionMode

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

the class DbJtxTransactionManagerTest method testSessionProvider.

@Test
public void testSessionProvider() {
    // prepare
    JtxTransactionManager jtxManager = new DbJtxTransactionManager(cp);
    DbJtxSessionProvider sessionProvider = new DbJtxSessionProvider(jtxManager);
    DbManager.getInstance().setSessionProvider(sessionProvider);
    for (int i = 0; i < 2; i++) {
        // start, 0 transactions, no session
        assertEquals(0, jtxManager.totalTransactions());
        // start transaction
        jtxManager.requestTransaction(new JtxTransactionMode());
        // get session from provider!
        DbSession dbSession = sessionProvider.getDbSession();
        assertNotNull(dbSession);
        // transaction started, but connection not yet fetched as it is not used yet
        assertEquals(1, jtxManager.totalTransactions());
        assertEquals(0, cp.getConnectionsCount().getBusyCount());
        // same session as it is the same transaction
        DbSession dbSession2 = sessionProvider.getDbSession();
        assertNotNull(dbSession2);
        assertSame(dbSession, dbSession2);
        // create query, session is get from provider, the very same one
        DbQuery dbQuery = new DbQuery("SELECT 173 FROM (VALUES(0))");
        long value = dbQuery.executeCount();
        assertEquals(173, value);
        assertSame(dbSession, dbQuery.getSession());
        // transaction still active, connection still in use
        assertEquals(1, jtxManager.totalTransactions());
        assertEquals(1, cp.getConnectionsCount().getBusyCount());
        // close query
        dbQuery.close();
        // transaction still active, connection still in use (!)
        // since session is still active
        assertEquals(1, jtxManager.totalTransactions());
        assertEquals(1, cp.getConnectionsCount().getBusyCount());
        assertTrue(!dbQuery.getSession().isSessionClosed());
        // commit transaction...
        jtxManager.getTransaction().commit();
        // no transaction
        assertEquals(0, jtxManager.totalTransactions());
        // session is closed
        assertTrue(dbSession.isSessionClosed());
        // connection is returned
        assertEquals(0, cp.getConnectionsCount().getBusyCount());
    }
}
Also used : DbJtxSessionProvider(jodd.db.jtx.DbJtxSessionProvider) JtxTransactionMode(jodd.jtx.JtxTransactionMode) JtxTransactionManager(jodd.jtx.JtxTransactionManager) DbJtxTransactionManager(jodd.db.jtx.DbJtxTransactionManager) DbJtxTransactionManager(jodd.db.jtx.DbJtxTransactionManager) Test(org.junit.Test)

Example 10 with JtxTransactionMode

use of jodd.jtx.JtxTransactionMode 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)

Aggregations

JtxTransactionMode (jodd.jtx.JtxTransactionMode)13 DbJtxTransaction (jodd.db.jtx.DbJtxTransaction)10 JtxTransaction (jodd.jtx.JtxTransaction)5 JtxTransactionManager (jodd.jtx.JtxTransactionManager)5 Test (org.junit.Test)5 DbJtxResourceManager (jodd.db.jtx.DbJtxResourceManager)4 JtxException (jodd.jtx.JtxException)2 Method (java.lang.reflect.Method)1 SQLException (java.sql.SQLException)1 DbJtxSessionProvider (jodd.db.jtx.DbJtxSessionProvider)1 DbJtxTransactionManager (jodd.db.jtx.DbJtxTransactionManager)1 TransactionAnnotationData (jodd.jtx.meta.TransactionAnnotationData)1 ProxettaException (jodd.proxetta.ProxettaException)1 ProxyTarget.targetClass (jodd.proxetta.ProxyTarget.targetClass)1