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.java58
1 files changed, 18 insertions, 40 deletions
diff --git a/src/main/java/io/trygvis/async/SqlEffectExecutor.java b/src/main/java/io/trygvis/async/SqlEffectExecutor.java
index e03cf5e..51ad31d 100644
--- a/src/main/java/io/trygvis/async/SqlEffectExecutor.java
+++ b/src/main/java/io/trygvis/async/SqlEffectExecutor.java
@@ -2,7 +2,9 @@ package io.trygvis.async;
import javax.sql.DataSource;
import java.sql.Connection;
+import java.sql.ResultSet;
import java.sql.SQLException;
+import java.sql.Statement;
public class SqlEffectExecutor {
@@ -12,41 +14,13 @@ public class SqlEffectExecutor {
this.dataSource = dataSource;
}
- public <A> A transaction(SqlEffect<A> effect) {
-/*
+ public <A> A transaction(SqlEffect<A> effect) throws SQLException {
int pid;
try (Connection c = dataSource.getConnection()) {
-
- try (Statement statement = c.createStatement()) {
- ResultSet rs = statement.executeQuery("SELECT pg_backend_pid()");
- rs.next();
- pid = rs.getInt(1);
- }
-
+ pid = getPid(c);
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);
- }
-*/
-
- try (Connection c = dataSource.getConnection()) {
boolean ok = false;
try {
A a = effect.doInConnection(c);
@@ -54,16 +28,19 @@ public class SqlEffectExecutor {
ok = true;
return a;
} finally {
+ System.out.println("Closing, pid = " + pid);
if (!ok) {
- c.rollback();
+ try {
+ c.rollback();
+ } catch (SQLException e) {
+ // ignore
+ }
}
}
- } catch (SQLException e) {
- throw new SqlExecutionException(e);
}
}
- public void transaction(final SqlEffect.Void effect) {
+ public void transaction(final SqlEffect.Void effect) throws SQLException {
transaction(new SqlEffect<Object>() {
@Override
public Object doInConnection(Connection c) throws SQLException {
@@ -73,12 +50,13 @@ public class SqlEffectExecutor {
});
}
- public static class SqlExecutionException extends RuntimeException {
- public final SQLException exception;
-
- public SqlExecutionException(SQLException ex) {
- super(ex);
- this.exception = ex;
+ private int getPid(Connection c) throws SQLException {
+ int pid;
+ try (Statement statement = c.createStatement()) {
+ ResultSet rs = statement.executeQuery("SELECT pg_backend_pid()");
+ rs.next();
+ pid = rs.getInt(1);
}
+ return pid;
}
}