0

Could someone please guide me into the right direction for this:

I wan to have an asynchronous function in my Java program that when called, it continuously monitors changes of some table in the DB. And when a change occurs, it returns some event. But it keeps doing this, as long as the application is running.

Any help would be great... thnx!

3
  • I am honestly not sure where to start. I wanted some help to know where/what I should start reading, that would help solve this problem. Commented Jun 26, 2011 at 9:47
  • What database are you planing to monitor, I know Microsoft SQL Server has a Notification feature, don't know if other systems have some sort of their own. Commented Jun 26, 2011 at 10:05
  • postgresql.org/docs/8.3/static/sql-notify.html Commented Jun 26, 2011 at 10:20

1 Answer 1

1
  • You can use a thread in function which can monitor the changes in some table.
  • In thread write code to access the database related queries.

    You can make use of Quartz - an open source job scheduling service.

    http://www.quartz-scheduler.org/

Source :Trigger example with Java method getting invoked whenever triiger is called

Trigger Example

This example creates a trigger. It follows the development model described in "Model 2: Using the Attached Stored Procedure Development Model". For an example of creating triggers using the load and publish model, see "Trigger Arguments Example". In the example, you first create a table and a Java class. Then you attach the class to the table. And finally, you create and fire the trigger.

The SalaryTrigger class contains the check_sal_raise method. The method prints a message if an employee gets a salary raise of more than ten percent. The trigger fires the method before updating a salary in the EMP table.

Since check_sal_raise writes a message to standard output, you should use the DOS-based version of SQL*Plus to issue the SQL commands in the example. Start SQL*Plus in a DOS Command Prompt window by typing:

plus80 username/password@connect_string

connect_string is ODBC:data_source_name. For example, to connect to the default database as user SYSTEM, at the DOS prompt type:

plus80 system/pw@odbc:polite

At the SQL*Plus command line, create and populate the EMP table as follows:

CREATE TABLE EMP(E# int, name char(10), salary real,

Constraint E#_PK primary key (E#));

INSERT INTO EMP VALUES (123,'Smith',60000);

INSERT INTO EMP VALUES (234,'Jones',50000);

Place the following class in SalaryTrigger.java:

 class SalaryTrigger {

      private int eno;

      public SalaryTrigger(int enum) {

      eno = enum;

      }

     public void check_sal_raise(float old_sal,float new_sal)
     {
        if (((new_sal - old_sal)/old_sal) > .10)
        {
           // raise too high  do something here
           System.out.println("Raise too high for employee " + eno);
        }
     }
   }

The SalaryTrigger class constructor takes an integer, which it assigns to attribute eno (the employee number). An instance of SalaryTrigger is created for each row (that is, for each employee) in the table EMP.

The check_sal_raise method is a non-static method. To execute, it must be called by an object of its class. Whenever the salary column of a row in EMP is modified, an instance of SalaryTrigger corresponding to that row is created (if it does not already exist) with the employee number (E#) as the argument to the constructor. The trigger then calls the check_sal_raise method.

After creating the Java class, you attach it to the table, as follows:

ALTER TABLE EMP ATTACH JAVA SOURCE "SalaryTrigger" IN '.'

WITH CONSTRUCTOR ARGS(E#);

This statement directs Oracle Lite to compile the Java source file SalaryTrigger.java found in the current directory, and attach the resulting class to the EMP table. The statement also specifies that, when instantiating the class, Oracle Lite should use the constructor that takes as an argument the value in the E# column.

After attaching the class to the table, create the trigger as follows:

CREATE TRIGGER CHECK_RAISE BEFORE UPDATE OF SALARY ON EMP FOR EACH ROW

"check_sal_raise"(old.salary, new.salary);

/

This statement creates a trigger called check_raise, which fires the check_sal_raise method before any update to the salary column of any row in EMP. Oracle Lite passes the old value and the new value of the salary column as arguments to the method.

In the example, a row-level trigger fires a row-level procedure (a non-static method). A row-level trigger can also fire table-level procedures (static methods). However, because statement-level triggers are fired once for an entire statement and a statement may affect multiple rows, a statement-level trigger can only fire a table-level procedure.

The following command updates the salary and fires the trigger:

UPDATE EMP SET SALARY = SALARY + 6100 WHERE E# = 123;

This produces the following output:

Raise too high for employee 123

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

3 Comments

but how do I do table monitoring? I mean I could continuously poll, but is there a more efficient way. Could you explain maybe how triggers can be used here? Or, some type of data event notification system?
This is for Oracle Lite .Have to check what can be done for postgres DB as u mentioned.
@Larry is this helpful for you.

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.