Search code examples
postgresqlpljava

Connection to PostgreSQL using PL/Java in a trigger fails


I defined a trigger in PostgreSQL 9.1 that should fire whenever a record is updated using:

CREATE OR REPLACE FUNCTION check() RETURNS TRIGGER AS $$
 BEGIN
   SELECT doubletEngine.checkForDoublet(NEW.id);
 RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS check_upd on people;
CREATE TRIGGER check_upd AFTER UPDATE ON people
  FOR EACH ROW
  WHEN (OLD.* IS DISTINCT FROM NEW.*)
  EXECUTE PROCEDURE check();

The method doubletEngine.checkForDoublet() was introduced to PL/Java 1.4.3 using

CREATE OR REPLACE FUNCTION doubletEngine.checkForDoublet(varchar) RETURNS void AS 'DoubletSearcher.checkForDoublet' LANGUAGE java;

Inside the method checkForDoublet(), I try to connect to the database using

Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://127.0.0.1:5432/db";
c = DriverManager.getConnection(url, "postgres", "postgres");
if ( c == null ) {
   throw new RuntimeException("Could not connect to server");
}

...but c is still null. Calling this code directly from my IDE works perfectly, but when the trigger fires, it only throws the RuntimeException. Is there anything I miss...???


Solution

  • According to the PL/Java documentation the connection should be obtained as follows:

    Connection conn = DriverManager.getConnection("jdbc:default:connection");