Skip to content

Instantly share code, notes, and snippets.

@varrix
Created September 20, 2017 01:55
Show Gist options
  • Save varrix/71801a29d4315b70124cdb6ee366cf50 to your computer and use it in GitHub Desktop.
Save varrix/71801a29d4315b70124cdb6ee366cf50 to your computer and use it in GitHub Desktop.
If you want to use pooled connection in transaction, you should use it in this way. This sample code handles setting autocommit values. Source: https://stackoverflow.com/a/28719870
try (Connection conn = source.getConnection()) {
conn.setAutoCommit(false);
SQLException savedException = null;
try {
// Do things with connection in transaction here...
conn.commit();
} catch (SQLException ex) {
savedException = ex;
conn.rollback();
} finally {
conn.setAutoCommit(true);
if(savedException != null) {
throw savedException;
}
}
} catch (SQLException ex1) {
throw new DataManagerException(ex1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment