From 1c0fc92c719f3856653d0efcc5fe4a1fa30b7bac Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 9 Jun 2013 23:22:39 +0200 Subject: wip --- .../java/io/trygvis/async/SqlEffectExecutor.java | 58 +++++++--------------- 1 file changed, 18 insertions(+), 40 deletions(-) (limited to 'src/main/java/io/trygvis/async/SqlEffectExecutor.java') 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 transaction(SqlEffect effect) { -/* + public A transaction(SqlEffect 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() { @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; } } -- cgit v1.2.3