Skip to content

Instantly share code, notes, and snippets.

@brettwooldridge
Created February 22, 2017 11:56
Show Gist options
  • Select an option

  • Save brettwooldridge/e23b7be6b984567521d12a74738e4922 to your computer and use it in GitHub Desktop.

Select an option

Save brettwooldridge/e23b7be6b984567521d12a74738e4922 to your computer and use it in GitHub Desktop.

Revisions

  1. brettwooldridge created this gist Feb 22, 2017.
    112 changes: 112 additions & 0 deletions Issue828.java
    Original 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();
    }
    }
    }
    }
    }