diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2013-06-02 12:32:29 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2013-06-02 12:32:29 +0200 |
commit | 52084f7b4e6f50c90b3255cdf2eb9deab560c970 (patch) | |
tree | eed9abd7fe9825aaacfd4fe24c8fd363cc41fed1 /src/main/java/io/trygvis/async/SqlEffectExecutor.java | |
parent | 7d704feb86c44fca57941d223e8605b55fcf68f0 (diff) | |
download | quartz-based-queue-52084f7b4e6f50c90b3255cdf2eb9deab560c970.tar.gz quartz-based-queue-52084f7b4e6f50c90b3255cdf2eb9deab560c970.tar.bz2 quartz-based-queue-52084f7b4e6f50c90b3255cdf2eb9deab560c970.tar.xz quartz-based-queue-52084f7b4e6f50c90b3255cdf2eb9deab560c970.zip |
o Making some test cases.
Diffstat (limited to 'src/main/java/io/trygvis/async/SqlEffectExecutor.java')
-rw-r--r-- | src/main/java/io/trygvis/async/SqlEffectExecutor.java | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/main/java/io/trygvis/async/SqlEffectExecutor.java b/src/main/java/io/trygvis/async/SqlEffectExecutor.java new file mode 100644 index 0000000..c8abbd3 --- /dev/null +++ b/src/main/java/io/trygvis/async/SqlEffectExecutor.java @@ -0,0 +1,39 @@ +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> A execute(SqlEffect<A> 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; + } + } +} |