public < T > T transaction ( final SQLTransaction < T > operations ) throws SQLException { Connection conn = null ; try { conn = this . queryRunner . getDataSource ( ) . getConnection ( ) ; conn . setAutoCommit ( false ) ; final DatabaseTransOperator transOperator = new DatabaseTransOperator ( this . queryRunner , conn ) ; final T res = operations . execute ( transOperator ) ; conn . commit ( ) ; return res ; } catch ( final SQLException ex ) { // todo kunkun-tang: Retry logics should be implemented here. logger . error ( "transaction failed" , ex ) ; if ( this . dbMetrics != null ) { this . dbMetrics . markDBFailTransaction ( ) ; } throw ex ; } finally { DbUtils . closeQuietly ( conn ) ; } }
Provide a way to allow users define custom SQL operations without relying on fixed SQL interface . The common use case is to group a sequence of SQL operations without commit every time .