Search in sources :

Example 6 with JtxTransaction

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

the class DbTransactionTest method testNoTx.

// ---------------------------------------------------------------- notx
@Test
public void testNoTx() {
    final JtxTransactionManager manager = new JtxTransactionManager();
    manager.registerResourceManager(new DbJtxResourceManager(cp));
    JtxTransaction tx = manager.requestTransaction(new JtxTransactionMode().propagationSupports());
    assertTrue(tx.isNoTransaction());
    try {
        tx.commit();
    } catch (Exception ignore) {
        fail();
    }
    assertTrue(tx.isCommitted());
    try {
        tx.rollback();
        fail("exception is already committed!");
    } catch (Exception ignore) {
    }
    tx = manager.requestTransaction(new JtxTransactionMode().propagationSupports());
    try {
        tx.rollback();
    } catch (Exception ex) {
        fail(ex.toString());
    }
    tx = manager.requestTransaction(new JtxTransactionMode().propagationSupports());
    try {
        tx.setRollbackOnly();
    } catch (Exception ex) {
        fail(ex.toString());
    }
}
Also used : DbJtxResourceManager(jodd.db.jtx.DbJtxResourceManager) JtxTransactionMode(jodd.jtx.JtxTransactionMode) DbJtxTransaction(jodd.db.jtx.DbJtxTransaction) JtxTransaction(jodd.jtx.JtxTransaction) JtxTransactionManager(jodd.jtx.JtxTransactionManager) SQLException(java.sql.SQLException) JtxException(jodd.jtx.JtxException) Test(org.junit.Test)

Example 7 with JtxTransaction

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

the class WebRunner method runWebApp.

/**
	 * Starts the app web application and {@link #run() runs} user code.
	 */
public void runWebApp(Class<? extends WebApplication> webAppClass) {
    madvoc = new Madvoc();
    madvoc.setWebAppClass(webAppClass);
    madvoc.startNewWebApplication(null);
    app = madvoc.getWebApplication();
    appCore = BeanUtil.declared.getProperty(app, "appCore");
    setJtxManager(appCore.getJtxManager());
    appDao = appCore.getPetite().getBean(AppDao.class);
    petite = appCore.getPetite();
    JtxTransaction tx = startRwTx();
    try {
        System.out.println(StringUtil.repeat('-', 55) + " start");
        System.out.println("\n\n");
        run();
        System.out.println("\n\n");
        System.out.println(StringUtil.repeat('-', 55) + " end");
        tx.commit();
    } catch (Throwable throwable) {
        throwable.printStackTrace();
        tx.rollback();
    }
    try {
        ReflectUtil.invokeDeclared(app, "destroy", new Class[] { MadvocConfig.class }, new Object[] { null });
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : Madvoc(jodd.madvoc.Madvoc) AppDao(jodd.joy.db.AppDao) JtxTransaction(jodd.jtx.JtxTransaction)

Example 8 with JtxTransaction

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

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

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

the class DbTransactionTest method testAction.

// ---------------------------------------------------------------- misc
/*
	public void testIsolation() throws SQLException {
		JtxTransactionManager manager = new JtxTransactionManager();
		manager.registerResourceManager(new DbJtxResourceManager(cp));

		JtxTransaction tx1 = manager.requestTransaction(new JtxTransactionMode().propagationRequired().readOnly(false));
		DbSession session1 = tx1.requestResource(DbSession.class);
		assertNotNull(session1);
		JtxTransaction tx2 = manager.requestTransaction(new JtxTransactionMode().propagationNotSupported());
		DbSession session2 = tx2.requestResource(DbSession.class);
		assertNotNull(session2);

		assertNotSame(session1, session2);
		assertTotals(manager, 2, 1);

		DbQuery query = new DbQuery(session1, "insert into GIRL values(4, 'Jeniffer', 'fighting')");
		assertEquals(1, query.executeUpdate());
		query.close();

		DbQuery query2 = new DbQuery(session2, "select count(*) from GIRL");
		ResultSet rs = query2.execute();
		if (rs.next()) {
			assertEquals(1, rs.getInt(1));
		}

		session1.rollbackTransaction();

		rs = query2.execute();
		if (rs.next()) {
			assertEquals(0, rs.getInt(1));
		}
		rs.close();
		
		tx2.commit();
		tx1.commit();
		
		assertTotals(manager, 0, 0);
	}
*/
// ---------------------------------------------------------------- presentation layer
@Test
public void testAction() {
    assertNotNull(dbtxm);
    assertTotals(0, 0);
    assertEquals(0, dbtxm.totalThreadTransactions());
    service0();
    JtxTransaction tx1 = service1();
    service2(tx1);
    service3(tx1);
    tx1.commit();
    assertTotals(0, 0);
}
Also used : DbJtxTransaction(jodd.db.jtx.DbJtxTransaction) JtxTransaction(jodd.jtx.JtxTransaction) Test(org.junit.Test)

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