### Question:
Sql2o { @SuppressWarnings("unchecked") public <V> V withConnection(StatementRunnableWithResult<V> runnable, Object argument) { Connection connection = null; try{ connection = open(); return (V)runnable.run(connection, argument); } catch (Throwable t) { throw new Sql2oException("An error occurred while executing StatementRunnable", t); } finally { if (connection != null) { connection.close(); } } } Sql2o(String jndiLookup);  Sql2o(String url, String user, String pass);  Sql2o(String url, String user, String pass, Quirks quirks);  Sql2o(DataSource dataSource);  Sql2o(DataSource dataSource, Quirks quirks); Quirks getQuirks(); @Deprecated DataSource getDataSource(); ConnectionSource getConnectionSource(); void setConnectionSource(ConnectionSource connectionSource); Map<String, String> getDefaultColumnMappings(); void setDefaultColumnMappings(Map<String, String> defaultColumnMappings); boolean isDefaultCaseSensitive(); void setDefaultCaseSensitive(boolean defaultCaseSensitive); @Deprecated Query createQuery(String query, boolean returnGeneratedKeys); @Deprecated Query createQuery(String query); Connection open(ConnectionSource connectionSource); Connection open(); @SuppressWarnings("unchecked") V withConnection(StatementRunnableWithResult<V> runnable, Object argument); V withConnection(StatementRunnableWithResult<V> runnable); void withConnection(StatementRunnable runnable); void withConnection(StatementRunnable runnable, Object argument); Connection beginTransaction(int isolationLevel); Connection beginTransaction(ConnectionSource connectionSource, int isolationLevel); Connection beginTransaction(); Connection beginTransaction(ConnectionSource connectionSource); void runInTransaction(StatementRunnable runnable); void runInTransaction(StatementRunnable runnable, Object argument); void runInTransaction(StatementRunnable runnable, Object argument, int isolationLevel); V runInTransaction(StatementRunnableWithResult<V> runnableWithResult); V runInTransaction(StatementRunnableWithResult<V> runnableWithResult, Object argument); @SuppressWarnings("unchecked") V runInTransaction(StatementRunnableWithResult<V> runnableWithResult, Object argument, int isolationLevel);  }### Answer:
@Test public void testWithConnection() { createAndFillUserTable(); final String insertsql = "insert into User(name, email, text) values (:name, :email, :text)"; sql2o.withConnection(new StatementRunnable() { public void run(Connection connection, Object argument) throws Throwable { connection.createQuery(insertsql) .addParameter("name", "Sql2o") .addParameter("email", "sql2o@sql2o.org") .addParameter("text", "bla bla") .executeUpdate(); connection.createQuery(insertsql) .addParameter("name", "Sql2o2") .addParameter("email", "sql2o@sql2o.org") .addParameter("text", "bla bla") .executeUpdate(); connection.createQuery(insertsql) .addParameter("name", "Sql2o3") .addParameter("email", "sql2o@sql2o.org") .addParameter("text", "bla bla") .executeUpdate(); } }); List<User> users = sql2o.withConnection(new StatementRunnableWithResult<List<User>>() { public List<User> run(Connection connection, Object argument) throws Throwable { return sql2o.createQuery("select * from User").executeAndFetch(User.class); } }); assertThat(users.size(), is(equalTo(10003))); try{ sql2o.withConnection(new StatementRunnable() { public void run(Connection connection, Object argument) throws Throwable { connection.createQuery(insertsql) .addParameter("name", "Sql2o") .addParameter("email", "sql2o@sql2o.org") .addParameter("text", "bla bla") .executeUpdate(); throw new RuntimeException("whaa!"); } }); } catch (Exception e) { } List<User> users2 = sql2o.createQuery("select * from User").executeAndFetch(User.class); assertThat(users2.size(), is(equalTo(10004))); }