use of jodd.db.jtx.DbJtxResourceManager 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());
}
}
use of jodd.db.jtx.DbJtxResourceManager 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();
}
use of jodd.db.jtx.DbJtxResourceManager 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();
}
use of jodd.db.jtx.DbJtxResourceManager 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();
}
Aggregations