/** * 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. * * @param operations A sequence of DB operations * @param <T> The type of object that the operations returns. Note that T could be null * @return T The object returned by the SQL statement, expected by the caller */ 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); } }
@Before public void setUp() throws Exception { final AzkabanDataSource datasource = new AzDBTestUtility.EmbeddedH2BasicDataSource(); final DatabaseTransOperator operator = new DatabaseTransOperator(new QueryRunner(), datasource.getConnection()); }