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());
}
}
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();
}
}
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();
}
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();
}
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);
}
Aggregations