Created
September 20, 2017 01:55
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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