aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/trygvis/async/SqlEffectExecutor.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/io/trygvis/async/SqlEffectExecutor.java')
-rw-r--r--src/main/java/io/trygvis/async/SqlEffectExecutor.java55
1 files changed, 50 insertions, 5 deletions
diff --git a/src/main/java/io/trygvis/async/SqlEffectExecutor.java b/src/main/java/io/trygvis/async/SqlEffectExecutor.java
index c8abbd3..e03cf5e 100644
--- a/src/main/java/io/trygvis/async/SqlEffectExecutor.java
+++ b/src/main/java/io/trygvis/async/SqlEffectExecutor.java
@@ -12,22 +12,67 @@ public class SqlEffectExecutor {
this.dataSource = dataSource;
}
- public <A> A execute(SqlEffect<A> effect) {
+ public <A> A transaction(SqlEffect<A> effect) {
+/*
+ int pid;
+
try (Connection c = dataSource.getConnection()) {
- return effect.doInConnection(c);
+
+ try (Statement statement = c.createStatement()) {
+ ResultSet rs = statement.executeQuery("SELECT pg_backend_pid()");
+ rs.next();
+ pid = rs.getInt(1);
+ }
+
+ System.out.println("pid = " + pid);
+
+ try {
+ effect.doInConnection(c);
+ c.commit();
+ } catch (SQLException e) {
+ c.rollback();
+ e.printStackTrace();
+ } finally {
+ System.out.println("Closing pid=" + pid);
+ try {
+ c.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
} catch (SQLException e) {
+ e.printStackTrace();
throw new SqlExecutionException(e);
}
- }
+*/
- public void execute(SqlEffect.Void effect) {
try (Connection c = dataSource.getConnection()) {
- effect.doInConnection(c);
+ boolean ok = false;
+ try {
+ A a = effect.doInConnection(c);
+ c.commit();
+ ok = true;
+ return a;
+ } finally {
+ if (!ok) {
+ c.rollback();
+ }
+ }
} catch (SQLException e) {
throw new SqlExecutionException(e);
}
}
+ public void transaction(final SqlEffect.Void effect) {
+ transaction(new SqlEffect<Object>() {
+ @Override
+ public Object doInConnection(Connection c) throws SQLException {
+ effect.doInConnection(c);
+ return null;
+ }
+ });
+ }
+
public static class SqlExecutionException extends RuntimeException {
public final SQLException exception;