You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2015/09/05 09:27:28 UTC

camel git commit: CAMEL-9088: Use prepared statement and close them. Thanks to James Lindstorff for the patch.

Repository: camel
Updated Branches:
  refs/heads/master 10920cb02 -> 198d45d1c


CAMEL-9088: Use prepared statement and close them. Thanks to James Lindstorff for the patch.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/198d45d1
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/198d45d1
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/198d45d1

Branch: refs/heads/master
Commit: 198d45d1c66c54f32627d4d0d0ebe3e154426da0
Parents: 10920cb
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Sep 5 09:28:11 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Sep 5 09:28:11 2015 +0200

----------------------------------------------------------------------
 .../component/pgevent/PgEventConsumer.java      | 14 +++++++--
 .../component/pgevent/PgEventProducer.java      |  7 ++++-
 .../apache/camel/pgevent/IntegrationTest.java   | 31 +++++++++++++++-----
 3 files changed, 41 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/198d45d1/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventConsumer.java b/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventConsumer.java
index a0a5f2c..92ad1ee 100644
--- a/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventConsumer.java
+++ b/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventConsumer.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.pgevent;
 
+import java.sql.PreparedStatement;
+
 import com.impossibl.postgres.api.jdbc.PGConnection;
 import com.impossibl.postgres.api.jdbc.PGNotificationListener;
 import org.apache.camel.Exchange;
@@ -41,13 +43,15 @@ public class PgEventConsumer extends DefaultConsumer implements PGNotificationLi
     @Override
     protected void doStart() throws Exception {
         super.doStart();
-
         dbConnection = endpoint.initJdbc();
-        dbConnection.createStatement().execute("LISTEN " + endpoint.getChannel());
+        String sql = String.format("LISTEN %s", endpoint.getChannel());
+        try (PreparedStatement statement = dbConnection.prepareStatement(sql)) {
+            statement.execute();
+        }
         dbConnection.addNotificationListener(endpoint.getChannel(), endpoint.getChannel(), this);
     }
 
-    @Override
+
     public void notification(int processId, String channel, String payload) {
         if (LOG.isDebugEnabled()) {
             LOG.debug("Notification processId: {}, channel: {}, payload: {}", new Object[]{processId, channel, payload});
@@ -70,6 +74,10 @@ public class PgEventConsumer extends DefaultConsumer implements PGNotificationLi
     protected void doStop() throws Exception {
         if (dbConnection != null) {
             dbConnection.removeNotificationListener(endpoint.getChannel());
+            String sql = String.format("UNLISTEN %s", endpoint.getChannel());
+            try (PreparedStatement statement = dbConnection.prepareStatement(sql)) {
+                statement.execute();
+            }
             dbConnection.close();
         }
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/198d45d1/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventProducer.java b/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventProducer.java
index 2f45dde..cfa7d57 100644
--- a/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventProducer.java
+++ b/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventProducer.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.pgevent;
 
+import java.sql.PreparedStatement;
 import java.sql.SQLException;
 
 import com.impossibl.postgres.api.jdbc.PGConnection;
@@ -48,7 +49,11 @@ public class PgEventProducer extends DefaultAsyncProducer {
         }
 
         try {
-            dbConnection.createStatement().execute("NOTIFY " + endpoint.getChannel() + ", '" + exchange.getIn().getBody(String.class) + "'");
+            String payload = exchange.getIn().getBody(String.class);
+            String sql = String.format("NOTIFY %s, '%s'", endpoint.getChannel(), payload);
+            try (PreparedStatement statement = dbConnection.prepareStatement(sql)) {
+                statement.execute();
+            }
         } catch (SQLException e) {
             exchange.setException(e);
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/198d45d1/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/IntegrationTest.java
----------------------------------------------------------------------
diff --git a/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/IntegrationTest.java b/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/IntegrationTest.java
index 885b94b..9e6f8d8 100644
--- a/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/IntegrationTest.java
+++ b/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/IntegrationTest.java
@@ -17,6 +17,7 @@
 package org.apache.camel.pgevent;
 
 import com.impossibl.postgres.jdbc.PGDataSource;
+import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.main.Main;
 import org.junit.Before;
@@ -24,17 +25,32 @@ import org.junit.Test;
 
 public class IntegrationTest {
 
+    private String host;
+    private String port;
+    private String database;
+    private String user;
+    private String password;
+
     private Main main;
 
     private PGDataSource ds;
 
     @Before
     public void setUp() throws Exception {
+        this.host = System.getProperty("pgjdbc.test.server", "localhost");
+        this.port = System.getProperty("pgjdbc.test.port", "5432");
+        this.database = System.getProperty("pgjdbc.test.db", "event_tests");
+        this.user = System.getProperty("pgjdbc.test.user", "dphillips");
+        this.password = System.getProperty("pgjdbc.test.password");
+
         ds = new PGDataSource();
-        ds.setHost(System.getProperty("pgjdbc.test.server", "localhost"));
-        ds.setPort(Integer.parseInt(System.getProperty("pgjdbc.test.port", "5432")));
-        ds.setDatabase(System.getProperty("pgjdbc.test.db", "event_tests"));
-        ds.setUser(System.getProperty("pgjdbc.test.user", "dphillips"));
+        ds.setHost(this.host);
+        ds.setPort(Integer.parseInt(this.port));
+        ds.setDatabase(this.database);
+        ds.setUser(this.user);
+        if (this.password != null) {
+            ds.setPassword(this.password);
+        }
 
         main = new Main();
         main.enableHangupSupport();
@@ -48,8 +64,8 @@ public class IntegrationTest {
 
             @Override
             public void configure() throws Exception {
-                from("pgevent://127.0.0.1:5432/event_tests/testchannel?user=dphillips")
-                        .to("log:org.apache.camel.pgevent.PgEventConsumer?level=DEBUG");
+                fromF("pgevent://%s:%s/%s/testchannel?user=%s&pass=%s", host, port, database, user, password)
+                    .to("log:org.apache.camel.pgevent.PgEventConsumer?level=DEBUG");
             }
         };
 
@@ -62,7 +78,8 @@ public class IntegrationTest {
             @Override
             public void configure() throws Exception {
                 from("timer://test?fixedRate=true&period=5000")
-                        .to("pgevent://127.0.0.1:5432/event_tests/testchannel?user=dphillips");
+                    .setBody(header(Exchange.TIMER_FIRED_TIME))
+                    .toF("pgevent://%s:%s/%s/testchannel?user=%s&pass=%s", host, port, database, user, password);
             }
         };