Search in sources :

Example 1 with DbJtxTransactionManager

use of jodd.db.jtx.DbJtxTransactionManager in project jodd by oblac.

the class DbH2TestCase method setUp.

@Before
public void setUp() throws Exception {
    LoggerFactory.setLoggerFactory(new TestLoggerFactory());
    if (dbtxm != null) {
        return;
    }
    cp = new CoreConnectionPool();
    cp.setDriver("org.h2.Driver");
    cp.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    cp.setUser("sa");
    cp.setPassword("");
    cp.init();
    dbtxm = new DbJtxTransactionManager(cp);
}
Also used : CoreConnectionPool(jodd.db.pool.CoreConnectionPool) DbJtxTransactionManager(jodd.db.jtx.DbJtxTransactionManager) Before(org.junit.Before)

Example 2 with DbJtxTransactionManager

use of jodd.db.jtx.DbJtxTransactionManager in project jodd by oblac.

the class DbHsqldbTestCase method setUp.

@Before
public void setUp() throws Exception {
    DbManager.getInstance().setQueryMap(new DbPropsQueryMap());
    LoggerFactory.setLoggerFactory(new TestLoggerFactory());
    cp = new CoreConnectionPool();
    cp.setDriver("org.hsqldb.jdbcDriver");
    cp.setUrl("jdbc:hsqldb:mem:test");
    cp.setUser("sa");
    cp.setPassword("");
    cp.init();
    dbtxm = new DbJtxTransactionManager(cp);
    // initial data
    DbSession session = new DbSession(cp);
    createTables(session);
    session.closeSession();
}
Also used : DbPropsQueryMap(jodd.db.querymap.DbPropsQueryMap) CoreConnectionPool(jodd.db.pool.CoreConnectionPool) DbJtxTransactionManager(jodd.db.jtx.DbJtxTransactionManager) Before(org.junit.Before)

Example 3 with DbJtxTransactionManager

use of jodd.db.jtx.DbJtxTransactionManager 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 4 with DbJtxTransactionManager

use of jodd.db.jtx.DbJtxTransactionManager in project jodd by oblac.

the class DbHsqldbTestCase method setUp.

@Before
public void setUp() throws Exception {
    cp = new CoreConnectionPool();
    cp.setDriver("org.hsqldb.jdbcDriver");
    cp.setUrl("jdbc:hsqldb:mem:test");
    cp.setUser("sa");
    cp.setPassword("");
    cp.init();
    dbtxm = new DbJtxTransactionManager(cp);
    // initial data
    DbSession session = new DbSession(cp);
    executeUpdate(session, "drop table BOY if exists");
    executeUpdate(session, "drop table GIRL if exists");
    String sql = "create table GIRL (" + "ID			integer		not null," + "NAME		varchar(20)	not null," + "SPECIALITY	varchar(20)	null," + "primary key (ID)" + ')';
    executeUpdate(session, sql);
    sql = "create table BOY (" + "ID			integer	not null," + "GIRL_ID	integer	null," + "NAME	varchar(20)	null," + "primary key (ID)," + "FOREIGN KEY (GIRL_ID) REFERENCES GIRL (ID)" + ')';
    executeUpdate(session, sql);
    session.closeSession();
}
Also used : DbSession(jodd.db.DbSession) CoreConnectionPool(jodd.db.pool.CoreConnectionPool) DbJtxTransactionManager(jodd.db.jtx.DbJtxTransactionManager) Before(org.junit.Before)

Aggregations

DbJtxTransactionManager (jodd.db.jtx.DbJtxTransactionManager)4 CoreConnectionPool (jodd.db.pool.CoreConnectionPool)3 Before (org.junit.Before)3 DbSession (jodd.db.DbSession)1 DbJtxSessionProvider (jodd.db.jtx.DbJtxSessionProvider)1 DbPropsQueryMap (jodd.db.querymap.DbPropsQueryMap)1 JtxTransactionManager (jodd.jtx.JtxTransactionManager)1 JtxTransactionMode (jodd.jtx.JtxTransactionMode)1 Test (org.junit.Test)1