15

Greetings all I want to do something like a trigger or a listener (I don't know what) that will listen on a specific database table, and with each new record inserted on this table, do some java code, I mean that it detects that a new record was inserted and get it's data if it's possible I need some guide about how this process can be accomplished ?

I am using Spring-Hibernate-PostgreSQL

1
  • I think after reading all the comments and such that I am going to implement my own solution. I am going to use JMS anyway so when a client updates the database through JMS send, I will send out a JMS message to all clients that the data has changed.. seems like the only guaranteed way to do this, as triggers seem to be a nightmare and Polling is a horrible solution. Commented Nov 15, 2015 at 19:12

3 Answers 3

12

This is what LISTEN/NOTIFY was created for.

The only drawback is that you will need to have some kind of background thread that polls the database on a regular basis to see if any notifications are available.

You can also use the code from the Postgres Wiki to have a starting point

Sign up to request clarification or add additional context in comments.

14 Comments

Your not polling the database... your polling the client: jdbc.postgresql.org/documentation/publicapi/org/postgresql/…
@AdamGent: of course you are polling the server. The client (PGConnection) will ask the server "are there any messages?".
Maybe I'm reading wireshark wrong but I swear it looked as if the pg driver was receiving the notifications with out asking for them. I read that Postgres had added some asynchronous support and assumed that this might be some of that.
@AdamGent: it is indeed transmitted "automatically", but only as a result of a request to the server. So as long as the client doesn't send a request, no notifications will be received. The request can be as simple as select 42 though. See the examle in the JDBC manual: jdbc.postgresql.org/documentation/head/listennotify.html
PQnotifies does not actually read data from the server; it just returns messages previously absorbed by another libpq function. In prior releases of libpq, the only way to ensure timely receipt of NOTIFY messages was to constantly submit commands, even empty ones, and then check PQnotifies after each PQexec. While this still works, it is deprecated as a waste of processing power. postgresql.org/docs/9.1/static/libpq-notify.html. Update sorry didnt see last message
|
1

I assume you mean that the DB content is added through your hibernate code.

If so, consult this previous answer of mine for how to set up Hibernate Event Listeners with Spring.

Otherwise, a-horse-with-no-name's answer should be best.

Comments

1

You could add an Interceptor to your Hibernate configuration to detect save events.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.