@Override public T withHandle(Handle handle) throws Exception { final Connection connection = handle.getConnection(); final boolean readOnly = connection.isReadOnly(); connection.setReadOnly(true); try { return handle.inTransaction(callback); } finally { try { connection.setReadOnly(readOnly); } catch (SQLException e) { // at least try to log it so we don't swallow exceptions log.error(e, "Unable to reset connection read-only state"); } } } }
@Override public T withHandle(Handle handle) throws Exception { final Connection connection = handle.getConnection(); final boolean readOnly = connection.isReadOnly(); connection.setReadOnly(true); try { return handle.inTransaction(callback); } finally { try { connection.setReadOnly(readOnly); } catch (SQLException e) { // at least try to log it so we don't swallow exceptions LOGGER.error(e, "Unable to reset connection read-only state"); } } } }
@Override public ReturnType withHandle(Handle handle) throws Exception { return handle.inTransaction(callback); } });
public ReturnType withHandle(Handle handle) throws Exception { return handle.inTransaction(callback); } });
@Override public ReturnType withHandle(Handle handle) throws Exception { return handle.inTransaction(isolation, callback); } });
public ReturnType withHandle(Handle handle) throws Exception { return handle.inTransaction(isolation, callback); } });
@Override public Void withHandle(Handle handle) throws Exception { handle.inTransaction(new TransactionCallback<Void>() { @Override public Void inTransaction(Handle conn, TransactionStatus status) throws Exception { dao.insert(new Something(1, "x")); List<String> rs = conn.createQuery("select name from something where id = 1") .mapTo(String.class) .list(); assertThat(rs.size(), equalTo(1)); conn.createQuery("SELECT 1").list(); return null; } }); return null; } });
private int[] executeBatch(final Handle handle, final PreparedBatch batch) { if (!handle.isInTransaction() && transactional) { // it is safe to use same prepared batch as the inTransaction passes in the same // Handle instance. return handle.inTransaction(new TransactionCallback<int[]>() { public int[] inTransaction(Handle conn, TransactionStatus status) throws Exception { return batch.execute(); } }); } else { return batch.execute(); } }
private int[] executeBatch(final Handle handle, final PreparedBatch batch) { if (!handle.isInTransaction() && transactional) { // it is safe to use same prepared batch as the inTransaction passes in the same // Handle instance. return handle.inTransaction(new TransactionCallback<int[]>() { @Override public int[] inTransaction(Handle conn, TransactionStatus status) throws Exception { return batch.execute(); } }); } else { return batch.execute(); } }
public Object invoke(HandleDing h, final Object target, Object[] args, MethodProxy mp) { h.retain("transaction#withlevel"); try { final TransactionIsolationLevel level = (TransactionIsolationLevel) args[0]; final Transaction t = (Transaction) args[1]; return h.getHandle().inTransaction(level, new TransactionCallback() { public Object inTransaction(Handle conn, TransactionStatus status) throws Exception { return t.inTransaction(target, status); } }); } finally { h.release("transaction#withlevel"); } } }
@Test public void testLogBeginCommit() throws Exception { h.inTransaction(new TransactionCallback<Object>() { @Override public Object inTransaction(Handle handle, TransactionStatus status) throws Exception { assertTrue(logged.contains("begin")); return null; } }); assertTrue(logged.contains("commit")); }
@Test public void testLogBeginCommit() throws Exception { h.inTransaction(new TransactionCallback<Object>() { @Override public Object inTransaction(Handle handle, TransactionStatus status) throws Exception { assertTrue(logged.contains("begin")); return null; } }); assertTrue(logged.contains("commit")); }
@Test(groups = "slow", description = "Verify SQL bound arguments are properly serialized") public void testQueryGeneratesNoWarning() throws Exception { final Handle handle = getDBI().open(); try { final Date date = new DateTime().toDate(); handle.inTransaction(new TransactionCallback<Object>() { @Override public Object inTransaction(final Handle conn, final TransactionStatus status) throws Exception { final NotificationSqlDao notificationSqlDao = conn.attach(NotificationSqlDao.class); final List<NotificationEventModelDao> entries = notificationSqlDao.getReadyEntries(date, 3, hostname, notificationQueueConfig.getTableName()); assertNull(conn.getConnection().getWarnings()); assertEquals(entries.size(), 0); return null; } }); } finally { handle.close(); } }
@Test public void testEventuallySucceeds() throws Exception { final AtomicInteger tries = new AtomicInteger(3); Handle handle = openHandle(); handle.inTransaction(TransactionIsolationLevel.SERIALIZABLE, new TransactionCallback<Void>() { @Override public Void inTransaction(Handle conn, TransactionStatus status) throws Exception { if (tries.decrementAndGet() == 0) { return null; } throw new SQLException("serialization", "40001"); } }); Assert.assertEquals(0, tries.get()); } }
@Test public void testEventuallySucceeds() throws Exception { final AtomicInteger tries = new AtomicInteger(3); Handle handle = openHandle(); handle.inTransaction(TransactionIsolationLevel.SERIALIZABLE, new TransactionCallback<Void>() { @Override public Void inTransaction(Handle conn, TransactionStatus status) throws Exception { if (tries.decrementAndGet() == 0) { return null; } throw new SQLException("serialization", "40001"); } }); Assert.assertEquals(0, tries.get()); } }
@Test public void testInTransaction() throws Exception { Handle h = this.openHandle(); String value = h.inTransaction(new TransactionCallback<String>() { @Override public String inTransaction(Handle handle, TransactionStatus status) throws Exception { handle.insert("insert into something (id, name) values (1, 'Brian')"); return handle.createQuery("select name from something where id = 1").map(Something.class).first().getName(); } }); assertEquals("Brian", value); }
@Test public void testCallback() throws Exception { Handle h = this.openHandle(); String woot = h.inTransaction(new TransactionCallback<String>() { @Override public String inTransaction(Handle handle, TransactionStatus status) throws Exception { return "Woot!"; } }); assertEquals("Woot!", woot); }
@Test public void testCallback() throws Exception { Handle h = this.openHandle(); String woot = h.inTransaction(new TransactionCallback<String>() { @Override public String inTransaction(Handle handle, TransactionStatus status) throws Exception { return "Woot!"; } }); assertEquals("Woot!", woot); }
@Test public void testEventuallyFails() throws Exception { final AtomicInteger tries = new AtomicInteger(5); Handle handle = openHandle(); try { handle.inTransaction(TransactionIsolationLevel.SERIALIZABLE, new TransactionCallback<Void>() { @Override public Void inTransaction(Handle conn, TransactionStatus status) throws Exception { tries.decrementAndGet(); throw new SQLException("serialization", "40001"); } }); } catch (TransactionFailedException e) { Assert.assertEquals("40001", ((SQLException) e.getCause()).getSQLState()); } Assert.assertEquals(0, tries.get()); }
@Test public void testInTransaction() throws Exception { Handle h = this.openHandle(); String value = h.inTransaction(new TransactionCallback<String>() { @Override public String inTransaction(Handle handle, TransactionStatus status) throws Exception { handle.insert("insert into something (id, name) values (1, 'Brian')"); return handle.createQuery("select name from something where id = 1").map(Something.class).first().getName(); } }); assertEquals("Brian", value); }