Created
February 22, 2017 11:56
-
-
Save brettwooldridge/e23b7be6b984567521d12a74738e4922 to your computer and use it in GitHub Desktop.
Revisions
-
brettwooldridge created this gist
Feb 22, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,112 @@ package com.zaxxer.hikari.pool; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.junit.Test; import org.postgresql.PGConnection; import com.zaxxer.hikari.HikariDataSource; public class Issue828 { private HikariDataSource hikariDataSource; @Test public void issue828() throws ClassNotFoundException, SQLException, InterruptedException { Class.forName("org.postgresql.Driver"); String url = "jdbc:postgresql://localhost:5432/test"; // Connection lConn = DriverManager.getConnection(url, "brettw", ""); Connection lConn = getDBConnection(url).unwrap(Connection.class); System.out.println("lConn: " +lConn.toString()); // Create two threads, one to issue notifications and // the other to receive them. Listener listener = new Listener(lConn); Notifier notifier = new Notifier(lConn); listener.start(); notifier.start(); notifier.join(); } private Connection getDBConnection(String jdbcUrl) throws SQLException { hikariDataSource = new HikariDataSource(); hikariDataSource.setJdbcUrl(jdbcUrl); return hikariDataSource.getConnection(); } static class Listener extends Thread { private Connection conn; private org.postgresql.PGConnection pgconn; Listener(Connection conn) throws SQLException { this.conn = conn; this.pgconn = conn.unwrap(PGConnection.class); Statement stmt = conn.createStatement(); stmt.execute("LISTEN new_facility_added"); stmt.close(); } public void run() { for (int i = 0; i < 40; i++) { try { // issue a dummy query to contact the backend // and receive any pending notifications. Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT 1"); rs.close(); stmt.close(); org.postgresql.PGNotification notifications[] = pgconn.getNotifications(); if (notifications == null) { System.out.println("Got no notification"); } else { for (int j = 0; j < notifications.length; j++) { System.out.println("Got notification: " + notifications[j].getName()); } } // wait a while before checking again for new // notifications Thread.sleep(1000); } catch (SQLException sqle) { sqle.printStackTrace(); } catch (InterruptedException ie) { ie.printStackTrace(); } } } } static class Notifier extends Thread { private Connection conn; public Notifier(Connection conn) { this.conn = conn; } public void run() { for (int i = 0; i < 20; i++) { try { Statement stmt = conn.createStatement(); stmt.execute("NOTIFY new_facility_added"); stmt.close(); Thread.sleep(2000); } catch (SQLException sqle) { sqle.printStackTrace(); } catch (Exception ie) { ie.printStackTrace(); } } } } }