From 72ae5827aa7bc2b7eba49da1491c7d32d254209a Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 18 Jun 2013 02:01:07 +0200 Subject: wip --- .../java/io/trygvis/async/QueueController.java | 2 + src/main/java/io/trygvis/async/SqlEffect.java | 12 ----- .../java/io/trygvis/async/SqlEffectExecutor.java | 52 ---------------------- .../java/io/trygvis/queue/JdbcQueueService.java | 2 - src/main/java/io/trygvis/queue/QueueExecutor.java | 2 - src/main/java/io/trygvis/queue/QueueSystem.java | 2 - src/main/java/io/trygvis/queue/SqlEffect.java | 12 +++++ .../java/io/trygvis/queue/SqlEffectExecutor.java | 50 +++++++++++++++++++++ src/main/java/io/trygvis/spring/DefaultConfig.java | 2 +- .../io/trygvis/spring/SpringJdbcAsyncService.java | 2 +- .../io/trygvis/test/jdbc/AsyncConsumerExample.java | 4 +- .../io/trygvis/test/jdbc/PlainJavaExample.java | 4 +- .../io/trygvis/test/spring/PlainSpringTest.java | 2 - 13 files changed, 70 insertions(+), 78 deletions(-) delete mode 100644 src/main/java/io/trygvis/async/SqlEffect.java delete mode 100644 src/main/java/io/trygvis/async/SqlEffectExecutor.java create mode 100644 src/main/java/io/trygvis/queue/SqlEffect.java create mode 100644 src/main/java/io/trygvis/queue/SqlEffectExecutor.java diff --git a/src/main/java/io/trygvis/async/QueueController.java b/src/main/java/io/trygvis/async/QueueController.java index 863d6a5..8aed91f 100644 --- a/src/main/java/io/trygvis/async/QueueController.java +++ b/src/main/java/io/trygvis/async/QueueController.java @@ -3,6 +3,8 @@ package io.trygvis.async; import io.trygvis.queue.QueueExecutor; import io.trygvis.queue.QueueService; import io.trygvis.queue.QueueSystem; +import io.trygvis.queue.SqlEffect; +import io.trygvis.queue.SqlEffectExecutor; import io.trygvis.queue.Task; import io.trygvis.queue.TaskEffect; import org.slf4j.Logger; diff --git a/src/main/java/io/trygvis/async/SqlEffect.java b/src/main/java/io/trygvis/async/SqlEffect.java deleted file mode 100644 index d0c4e9b..0000000 --- a/src/main/java/io/trygvis/async/SqlEffect.java +++ /dev/null @@ -1,12 +0,0 @@ -package io.trygvis.async; - -import java.sql.Connection; -import java.sql.SQLException; - -public interface SqlEffect { - A doInConnection(Connection c) throws SQLException; - - interface Void { - void doInConnection(Connection c) throws SQLException; - } -} diff --git a/src/main/java/io/trygvis/async/SqlEffectExecutor.java b/src/main/java/io/trygvis/async/SqlEffectExecutor.java deleted file mode 100644 index 3da2cd3..0000000 --- a/src/main/java/io/trygvis/async/SqlEffectExecutor.java +++ /dev/null @@ -1,52 +0,0 @@ -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 { - - private final DataSource dataSource; - - public SqlEffectExecutor(DataSource dataSource) { - this.dataSource = dataSource; - } - - public A transaction(SqlEffect effect) throws SQLException { -// int pid; - - try (Connection c = dataSource.getConnection()) { -// pid = getPid(c); -// System.out.println("pid = " + pid); - - boolean ok = false; - try { - A a = effect.doInConnection(c); - c.commit(); - ok = true; - return a; - } finally { -// System.out.println("Closing, pid = " + pid); - if (!ok) { - try { - c.rollback(); - } catch (SQLException e) { - // ignore - } - } - } - } - } - - public void transaction(final SqlEffect.Void effect) throws SQLException { - transaction(new SqlEffect() { - @Override - public Object doInConnection(Connection c) throws SQLException { - effect.doInConnection(c); - return null; - } - }); - } -} diff --git a/src/main/java/io/trygvis/queue/JdbcQueueService.java b/src/main/java/io/trygvis/queue/JdbcQueueService.java index a366838..ef0b5bb 100644 --- a/src/main/java/io/trygvis/queue/JdbcQueueService.java +++ b/src/main/java/io/trygvis/queue/JdbcQueueService.java @@ -1,7 +1,5 @@ package io.trygvis.queue; -import io.trygvis.async.SqlEffectExecutor; - import java.sql.Connection; import java.sql.SQLException; import java.util.HashMap; diff --git a/src/main/java/io/trygvis/queue/QueueExecutor.java b/src/main/java/io/trygvis/queue/QueueExecutor.java index 3739532..468059d 100644 --- a/src/main/java/io/trygvis/queue/QueueExecutor.java +++ b/src/main/java/io/trygvis/queue/QueueExecutor.java @@ -1,8 +1,6 @@ package io.trygvis.queue; import io.trygvis.async.QueueStats; -import io.trygvis.async.SqlEffect; -import io.trygvis.async.SqlEffectExecutor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/io/trygvis/queue/QueueSystem.java b/src/main/java/io/trygvis/queue/QueueSystem.java index 6710bf4..5954526 100644 --- a/src/main/java/io/trygvis/queue/QueueSystem.java +++ b/src/main/java/io/trygvis/queue/QueueSystem.java @@ -1,8 +1,6 @@ package io.trygvis.queue; import io.trygvis.async.JdbcAsyncService; -import io.trygvis.async.SqlEffect; -import io.trygvis.async.SqlEffectExecutor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/io/trygvis/queue/SqlEffect.java b/src/main/java/io/trygvis/queue/SqlEffect.java new file mode 100644 index 0000000..7864bcd --- /dev/null +++ b/src/main/java/io/trygvis/queue/SqlEffect.java @@ -0,0 +1,12 @@ +package io.trygvis.queue; + +import java.sql.Connection; +import java.sql.SQLException; + +public interface SqlEffect { + A doInConnection(Connection c) throws SQLException; + + interface Void { + void doInConnection(Connection c) throws SQLException; + } +} diff --git a/src/main/java/io/trygvis/queue/SqlEffectExecutor.java b/src/main/java/io/trygvis/queue/SqlEffectExecutor.java new file mode 100644 index 0000000..92802da --- /dev/null +++ b/src/main/java/io/trygvis/queue/SqlEffectExecutor.java @@ -0,0 +1,50 @@ +package io.trygvis.queue; + +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 transaction(SqlEffect effect) throws SQLException { +// int pid; + + try (Connection c = dataSource.getConnection()) { +// pid = getPid(c); +// System.out.println("pid = " + pid); + + boolean ok = false; + try { + A a = effect.doInConnection(c); + c.commit(); + ok = true; + return a; + } finally { +// System.out.println("Closing, pid = " + pid); + if (!ok) { + try { + c.rollback(); + } catch (SQLException e) { + // ignore + } + } + } + } + } + + public void transaction(final SqlEffect.Void effect) throws SQLException { + transaction(new SqlEffect() { + @Override + public Object doInConnection(Connection c) throws SQLException { + effect.doInConnection(c); + return null; + } + }); + } +} diff --git a/src/main/java/io/trygvis/spring/DefaultConfig.java b/src/main/java/io/trygvis/spring/DefaultConfig.java index 3ba28de..6890d58 100644 --- a/src/main/java/io/trygvis/spring/DefaultConfig.java +++ b/src/main/java/io/trygvis/spring/DefaultConfig.java @@ -1,7 +1,7 @@ package io.trygvis.spring; import io.trygvis.async.AsyncService; -import io.trygvis.async.SqlEffectExecutor; +import io.trygvis.queue.SqlEffectExecutor; import io.trygvis.queue.QueueService; import io.trygvis.queue.QueueSystem; import org.springframework.context.annotation.Bean; diff --git a/src/main/java/io/trygvis/spring/SpringJdbcAsyncService.java b/src/main/java/io/trygvis/spring/SpringJdbcAsyncService.java index a1c9cda..12dc71e 100644 --- a/src/main/java/io/trygvis/spring/SpringJdbcAsyncService.java +++ b/src/main/java/io/trygvis/spring/SpringJdbcAsyncService.java @@ -3,7 +3,7 @@ package io.trygvis.spring; import io.trygvis.async.AsyncService; import io.trygvis.async.JdbcAsyncService; import io.trygvis.async.QueueController; -import io.trygvis.async.SqlEffect; +import io.trygvis.queue.SqlEffect; import io.trygvis.queue.JdbcQueueService; import io.trygvis.queue.Queue; import io.trygvis.queue.QueueExecutor; diff --git a/src/test/java/io/trygvis/test/jdbc/AsyncConsumerExample.java b/src/test/java/io/trygvis/test/jdbc/AsyncConsumerExample.java index 5bcffe7..14db21b 100644 --- a/src/test/java/io/trygvis/test/jdbc/AsyncConsumerExample.java +++ b/src/test/java/io/trygvis/test/jdbc/AsyncConsumerExample.java @@ -3,8 +3,8 @@ package io.trygvis.test.jdbc; import io.trygvis.async.JdbcAsyncService; import io.trygvis.async.QueueController; import io.trygvis.async.QueueStats; -import io.trygvis.async.SqlEffect; -import io.trygvis.async.SqlEffectExecutor; +import io.trygvis.queue.SqlEffect; +import io.trygvis.queue.SqlEffectExecutor; import io.trygvis.queue.JdbcQueueService; import io.trygvis.queue.QueueExecutor; import io.trygvis.queue.QueueService; diff --git a/src/test/java/io/trygvis/test/jdbc/PlainJavaExample.java b/src/test/java/io/trygvis/test/jdbc/PlainJavaExample.java index 0e11ab3..0b7ba50 100644 --- a/src/test/java/io/trygvis/test/jdbc/PlainJavaExample.java +++ b/src/test/java/io/trygvis/test/jdbc/PlainJavaExample.java @@ -1,7 +1,7 @@ package io.trygvis.test.jdbc; -import io.trygvis.async.SqlEffect; -import io.trygvis.async.SqlEffectExecutor; +import io.trygvis.queue.SqlEffect; +import io.trygvis.queue.SqlEffectExecutor; import io.trygvis.queue.JdbcQueueService; import io.trygvis.queue.QueueExecutor; import io.trygvis.queue.QueueService; diff --git a/src/test/java/io/trygvis/test/spring/PlainSpringTest.java b/src/test/java/io/trygvis/test/spring/PlainSpringTest.java index cde0c97..93372f6 100644 --- a/src/test/java/io/trygvis/test/spring/PlainSpringTest.java +++ b/src/test/java/io/trygvis/test/spring/PlainSpringTest.java @@ -13,7 +13,6 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.sql.SQLException; -import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.atomic.AtomicReference; @@ -23,7 +22,6 @@ import static java.lang.System.setProperty; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static org.fest.assertions.Assertions.assertThat; -import static org.junit.Assert.assertNotNull; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {TestConfig.class, DefaultConfig.class}) -- cgit v1.2.3