June 2018
Intermediate to advanced
280 pages
7h 46m
English
One of the most-used templates when working with database transaction is getting a transaction, making the appropriate calls, and making sure to commit or rollback on exception and close the transaction. This can be implemented as a loan pattern, where the moving part is the calls within the transaction. The following code shows how this can be achieved:
jshell> class Connection {...> public void commit() {};public void rollback() {};public void close() {};public void setAutoCommit(boolean autoCommit) {};...> public static void runWithinTransaction(Consumer<Connection> c) {...> Connection t = null;...> try { t = new Connection(); t.setAutoCommit(false);...> c.accept(t);...> t.commit();...> } catch(Exception e) { t.rollback(); } finally ...Read now
Unlock full access