java - Hibernate postgresql notify functionality -


i`m writing app using hibernate + jpa orm , postgresql 9.3 database backend , need react java code database events.
more precise want build trigger uses pg_notify() when new row inserted table.
have read tutorials direct jdbc connection , not through hibernate.
(think i) cannot use hibernate events rows not inserted through hibernate 3rd party app.

is there way can receive notifications send pg_notify through hibernate ?

-- update
right have classcastexception :

java.lang.classcastexception: com.sun.gjc.spi.jdbc40.connectionwrapper40 cannot cast org.postgresql.pgconnection     @ com.xxx.core.impl.dao.postgresqllowlevelnotificationdaoimpl$1.execute(postgresqllowlevelnotificationdaoimpl.java:36)     @ com.xxx.core.impl.dao.postgresqllowlevelnotificationdaoimpl$1.execute(postgresqllowlevelnotificationdaoimpl.java:1) 

i have mention use glassfish 4.0 as. connection pool created on glassfish , accessed app through jndi. entitymanager injected container spring. here code:

@named public class postgresqllowlevelnotificationdaoimpl implements postgresqllowlevelnotificationdao{      @persistencecontext(type =persistencecontexttype.transaction,synchronization=synchronizationtype.synchronized,unitname="ccpu")     private entitymanager em;  @override public arraylist<pgnotification> getnotifications(){      session session = em.unwrap(session.class);      pgnotification[] notifications = session.doreturningwork(new returningwork<pgnotification[]>() {          @override         public pgnotification[] execute(connection connection) throws sqlexception {             pgnotification[] notifications = ((pgconnection) connection).getnotifications();             return notifications;         }      });      return (arraylist) arrays.aslist(notifications); }    

}

-- update
have fixed classcast exception :

@override public arraylist<pgnotification> getnotifications(){      session session = em.unwrap(session.class);       pgnotification[] notifications = session.doreturningwork(new returningwork<pgnotification[]>() {          @override         public pgnotification[] execute(connection connection) throws sqlexception {              pgconnection pgc = null;               if (connection.iswrapperfor(pgconnection.class)) {                 pgc = (pgconnection) connection.unwrap(pgconnection.class);             }                                             pgnotification[] notifications = pgc.getnotifications();                             return notifications;         }     }); 

but still seems not receive notifications.

update ---
after have implemented solution proposed neil have error in glassfish logs when undeploy app:

    2014-06-27t11:03:24.278+0300|severe: web application [/myapp] created threadlocal    key of type [io.netty.buffer.pooledbytebufallocator$1] (value [io.netty.buffer.pooledbytebufallocator$1@28ad6479]) , value of type [io.netty.buffer.poolthreadcache] (value [io.netty.buffer.poolthreadcache@f9f58cc]) failed remove when web application stopped. threads going renewed on time try , avoid probable memory leak. 2014-06-27t11:03:24.279+0300|severe: web application [/myapp] created threadlocal key of type [io.netty.util.recycler$1] (value [io.netty.util.recycler$1@267ec117]) , value of type [io.netty.util.recycler.stack] (value [io.netty.util.recycler$stack@4bb6e0bf]) failed remove when web application stopped. threads going renewed on time try , avoid probable memory leak. 2014-06-27t11:03:24.279+0300|severe: web application [/myapp] created threadlocal key of type [io.netty.util.recycler$1] (value [io.netty.util.recycler$1@535d426e]) , value of type [io.netty.util.recycler.stack] (value [io.netty.util.recycler$stack@fb46e84]) failed remove when web application stopped. threads going renewed on time try , avoid probable memory leak. 2014-06-27t11:03:24.280+0300|severe: web application [/myapp] created threadlocal key of type [io.netty.util.internal.threadlocalrandom$2] (value [io.netty.util.internal.threadlocalrandom$2@ec3a42a]) , value of type [io.netty.util.internal.threadlocalrandom] (value [io.netty.util.internal.threadlocalrandom@4e4ec8f8]) failed remove when web application stopped. threads going renewed on time try , avoid probable memory leak. 2014-06-27t11:03:24.280+0300|severe: web application [/myapp] created threadlocal key of type [io.netty.util.recycler$1] (value [io.netty.util.recycler$1@166c39f2]) , value of type [io.netty.util.recycler.stack] (value [io.netty.util.recycler$stack@1b504a5e]) failed remove when web application stopped. threads going renewed on time try , avoid probable memory leak. 2014-06-27t11:03:24.281+0300|severe: web application [/myapp] created threadlocal key of type [io.netty.util.recycler$1] (value [io.netty.util.recycler$1@34426f54]) , value of type [io.netty.util.recycler.stack] (value [io.netty.util.recycler$stack@759b0e99]) failed remove when web application stopped. threads going renewed on time try , avoid probable memory leak. 2014-06-27t11:03:24.282+0300|severe: web application [/myapp] created threadlocal key of type [io.netty.util.concurrent.defaultpromise$1] (value [io.netty.util.concurrent.defaultpromise$1@16db9b21]) , value of type [java.lang.integer] (value [0]) failed remove when web application stopped. threads going renewed on time try , avoid probable memory leak. 2014-06-27t11:03:24.282+0300|severe: web application [/myapp] created threadlocal key of type [io.netty.util.recycler$1] (value [io.netty.util.recycler$1@166c39f2]) , value of type [io.netty.util.recycler.stack] (value [io.netty.util.recycler$stack@2ba59f40]) failed remove when web application stopped. threads going renewed on time try , avoid probable memory leak. 2014-06-27t11:03:24.282+0300|severe: web application [/myapp] created threadlocal key of type [io.netty.buffer.pooledbytebufallocator$1] (value [io.netty.buffer.pooledbytebufallocator$1@28ad6479]) , value of type [io.netty.buffer.poolthreadcache] (value [io.netty.buffer.poolthreadcache@67a3923]) failed remove when web application stopped. threads going renewed on time try , avoid probable memory leak. 2014-06-27t11:03:24.283+0300|severe: web application [/myapp] created threadlocal key of type [io.netty.util.recycler$1] (value [io.netty.util.recycler$1@166c39f2]) , value of type [io.netty.util.recycler.stack] (value [io.netty.util.recycler$stack@423d2c27]) failed remove when web application stopped. threads going renewed on time try , avoid probable memory leak. 2014-06-27t11:03:24.283+0300|severe: web application [/myapp] created threadlocal key of type [io.netty.util.recycler$1] (value [io.netty.util.recycler$1@535d426e]) , value of type [io.netty.util.recycler.stack] (value [io.netty.util.recycler$stack@3e1dd66a]) failed remove when web application stopped. threads going renewed on time try , avoid probable memory leak. 2014-06-27t11:03:24.283+0300|severe: web application [/myapp] created threadlocal key of type [io.netty.buffer.pooledbytebufallocator$1] (value [io.netty.buffer.pooledbytebufallocator$1@28ad6479]) , value of type [io.netty.buffer.poolthreadcache] (value [io.netty.buffer.poolthreadcache@18e7e902]) failed remove when web application stopped. threads going renewed on time try , avoid probable memory leak. 

i have mention destroy() method invoked when app undeployed. , if spy visualvm thread still alive after application undeployed.

public void destroy(){      try{         statement statement = pgconnection.createstatement();                           statement.addbatch("unlisten xxxtest");         statement.executebatch();         statement.close();               }catch(sqlexception sqle)   {         sqle.printstacktrace();      }  } 

  1. why need hibernate here? use jdbc. want fire application-wide event anyways (perhaps send websocket msg or bust ehcache), , if need w hibernate, subscribe event , hibernate.

  2. http://impossibl.github.io/pgjdbc-ng/ friend here. no polling required.

see http://blog.databasepatterns.com/2014/04/postgresql-nofify-websocket-spring-mvc.html


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -