Skip to content

Commit

Permalink
add transaction support to DatabaseHelper (#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgardens authored Oct 13, 2020
1 parent 0fffd07 commit 8dd3933
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions airbyte-db/src/main/java/io/airbyte/db/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,24 @@ public static BasicDataSource getConnectionPool(String username,
return connectionPool;
}

public static <T> T query(BasicDataSource connectionPool, ContextQueryFunction<T> transform)
throws SQLException {
public static <T> T query(BasicDataSource connectionPool, ContextQueryFunction<T> transform) throws SQLException {

try (Connection connection = connectionPool.getConnection()) {
DSLContext context = getContext(connection);
return transform.apply(context);
}
}

public static <T> T transaction(BasicDataSource connectionPool, ContextQueryFunction<T> transform) throws SQLException {
try (Connection connection = connectionPool.getConnection()) {
DSLContext context = getContext(connection);
return context.transactionResult(configuration -> {
DSLContext transactionContext = DSL.using(configuration);
return transform.apply(transactionContext);
});
}
}

private static DSLContext getContext(Connection connection) {
return DSL.using(connection, SQLDialect.POSTGRES);
}
Expand Down

0 comments on commit 8dd3933

Please sign in to comment.