@Test public void testExceptionInRunnable() { sql2o .createQuery( "create table testExceptionInRunnable(id integer primary key, value varchar(20))") .executeUpdate(); try { sql2o.runInTransaction( new StatementRunnable() { public void run(Connection connection, Object argument) throws Throwable { connection .createQuery("insert into testExceptionInRunnable(id, value) values(:id, :val)") .addParameter("id", 1) .addParameter("val", "something") .executeUpdate(); connection .createQuery("insert into testExceptionInRunnable(id, value) values(:id, :val)") .addParameter("id", 1) .addParameter("val", "something") .executeUpdate(); } }); } catch (Throwable t) { } int c = (Integer) sql2o .createQuery("select count(*) from testExceptionInRunnable") .executeScalar(Integer.class); assertEquals(0, c); sql2o.runInTransaction( new StatementRunnable() { public void run(Connection connection, Object argument) throws Throwable { connection .createQuery("insert into testExceptionInRunnable(id, value) values(:id, :val)") .addParameter("id", 1) .addParameter("val", "something") .executeUpdate(); try { connection .createQuery("insert into testExceptionInRunnable(id, value) values(:id, :val)") .addParameter("id", 1) .addParameter("val", "something") .executeUpdate(); } catch (Sql2oException ex) { } } }); c = (Integer) sql2o .createQuery("select count(*) from testExceptionInRunnable") .executeScalar(Integer.class); assertEquals(1, c); }
@Test public void testRunInsideTransaction() { sql2o .createQuery( "create table runinsidetransactiontable(id integer identity primary key, value varchar(50))") .executeUpdate(); boolean failed = false; try { sql2o.runInTransaction( new StatementRunnable() { public void run(Connection connection, Object argument) throws Throwable { connection .createQuery("insert into runinsidetransactiontable(value) values(:value)") .addParameter("value", "test") .executeUpdate(); throw new RuntimeException("ouch!"); } }); } catch (Sql2oException ex) { failed = true; } assertTrue(failed); long rowCount = (Long) sql2o.createQuery("select count(*) from runinsidetransactiontable").executeScalar(); assertEquals(0, rowCount); sql2o.runInTransaction( new StatementRunnable() { public void run(Connection connection, Object argument) throws Throwable { connection .createQuery("insert into runinsidetransactiontable(value) values(:value)") .addParameter("value", "test") .executeUpdate(); } }); rowCount = (Long) sql2o.createQuery("select count(*) from runinsidetransactiontable").executeScalar(); assertEquals(1, rowCount); String argument = "argument test"; sql2o.runInTransaction( new StatementRunnable() { public void run(Connection connection, Object argument) throws Throwable { Integer id = connection .createQuery("insert into runinsidetransactiontable(value) values(:value)") .addParameter("value", argument) .executeUpdate() .getKey(Integer.class); String insertedValue = connection .createQuery("select value from runinsidetransactiontable where id = :id") .addParameter("id", id) .executeScalar(String.class); assertEquals("argument test", insertedValue); } }, argument); rowCount = (Long) sql2o.createQuery("select count(*) from runinsidetransactiontable").executeScalar(); assertEquals(2, rowCount); }