package io.trygvis.async; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; public class SqlEffectExecutor { private final DataSource dataSource; public SqlEffectExecutor(DataSource dataSource) { this.dataSource = dataSource; } public A execute(SqlEffect effect) { try (Connection c = dataSource.getConnection()) { return effect.doInConnection(c); } catch (SQLException e) { throw new SqlExecutionException(e); } } public void execute(SqlEffect.Void effect) { try (Connection c = dataSource.getConnection()) { effect.doInConnection(c); } catch (SQLException e) { throw new SqlExecutionException(e); } } public static class SqlExecutionException extends RuntimeException { public final SQLException exception; public SqlExecutionException(SQLException ex) { super(ex); this.exception = ex; } } }