Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a database listener with java?

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

like image 290
Mahmoud Saleh Avatar asked Jan 04 '11 14:01

Mahmoud Saleh


People also ask

What is a database listener?

The listener is a separate process that runs on the database server computer. It receives incoming client connection requests and manages the traffic of these requests to the database server.

How do you call a database in Java?

STEP 1: Allocate a Connection object, for connecting to the database server. STEP 2: Allocate a Statement object, under the Connection created earlier, for holding a SQL command. STEP 3: Write a SQL query and execute the query, via the Statement and Connection created. STEP 4: Process the query result.

How do you add a listener to a class in Java?

You probably want to look into the observer pattern. class Test { public static void main(String[] args) { Initiater initiater = new Initiater(); Responder responder = new Responder(); initiater. addListener(responder); initiater. sayHello(); // Prints "Hello!!!" and "Hello there..." } }

How do I create a listener in Oracle 19c manually?

Create a listener in Oracle Database 19c with NETCALaunch the Network Configuration Assistant with necta command. Select “Listener Configuration” and click “Next“. Select “Add” then click “Next“. Give a name to the listener to easily identify it from other configured listeners and provide Oracle Home user password.


1 Answers

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

like image 175
a_horse_with_no_name Avatar answered Oct 24 '22 00:10

a_horse_with_no_name