java - JDBC determine active transaction -
java - JDBC determine active transaction -
is way determine jdbc i'm in not committed active transaction? want sure transaction committed or rollbacked before connection.close().
there's no standard jdbc method want. if want create sure transactions committed / rollbacked before connection.close(), can this:
connection conn = null; seek { conn = ...; conn.setautocommit(false); // stuff // commit if successful conn.commit(); } grab (someexception e) { // log exceptions } { if (conn != null) { /* rollback before closing connection * if there's caught/uncaught exception, transaction rolled * if successful / commit successful, rollback have no effect */ seek { conn.rollback(); } grab (sqlexception e) {} seek { conn.close(); } grab (sqlexception e) {} } }
this improve rolling if there's exception (in grab block) because, if there's uncaught exception:
rollback() not called connection.close() called while there's active transaction, , happens active transaction implementation-specific. see bogdan calmac's answer. java jdbc
Comments
Post a Comment