LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2009, 11:22 AM   #1
firewiz87
Member
 
Registered: Jan 2006
Distribution: OpenSUSE 11.2, OpenSUSE 11.3,Arch
Posts: 240

Rep: Reputation: 37
Java program that detects changes in a database??


Is it possible to detect the changes in a database in Java by raising an event whenever a change of data occurs in the data base??

Can somebody pint me to something related to this??

thanking u in advance,
firewiz
 
Old 03-11-2009, 12:23 PM   #2
mesiol
Member
 
Registered: Nov 2008
Location: Lower Saxony, Germany
Distribution: CentOS, RHEL, Solaris 10, AIX, HP-UX
Posts: 731

Rep: Reputation: 137Reputation: 137
Hi,

inside a database triggers can do this job.
 
Old 03-11-2009, 01:18 PM   #3
Developer Dude
LQ Newbie
 
Registered: May 2004
Location: Seattle
Posts: 4

Rep: Reputation: 0
As mentioned, SQL triggers are the usual approach to this problem. How to implement this depends on the DBMS you are using, but here is one example:

http://blog.mclaughlinsoftware.com/j...rigger-sample/

Googling 'java sql trigger' and your DBMS name (Mysql or Postgres, etc.) will probably give you some examples for whatever DBMS you use - assuming it supports triggers (I think older versions of MySQL did not support triggers IIRC).
 
Old 03-12-2009, 12:48 AM   #4
firewiz87
Member
 
Registered: Jan 2006
Distribution: OpenSUSE 11.2, OpenSUSE 11.3,Arch
Posts: 240

Original Poster
Rep: Reputation: 37
Thankyou for your quick response

Well i am using mySQL as the DBMS.

My situation is that a particular table stores a sensor status as a boolean value (true or false) when the sensor is activated, the value changes from false to true. The java application has to detect this change and perform the required actions.

Hope this clarifies the situation better...

Is it possible to do it without triggers????
 
Old 03-12-2009, 02:47 PM   #5
Developer Dude
LQ Newbie
 
Registered: May 2004
Location: Seattle
Posts: 4

Rep: Reputation: 0
Quote:
Originally Posted by firewiz87 View Post
Is it possible to do it without triggers????
If you can't use triggers because you are using an older version of MySQL, then if the process that changes the value is running in the same JVM, you could implement a listener for when some logic changes the value. I.E., if you have Java class A that is responsible for reading/writing/changing the sensor value in the DB, then you can modify that class to allow the addition of listeners for various events, including the changing of the value. You could then write another class, class B for example, that implements the listener interface and does what you need when the value changes. This is a nice way to decouple the logic in one class from another: class A is responsible for reading/writing the sensor value and class B is responsible for doing some task when the value changes.

The listener pattern is very common in Java and you should have not trouble finding examples. There is even some support built into the language libs.

But maybe you have multiple Java apps running on different machines and/or different VM instances such that you can't just add a listener to the class that is responsible for persistence of the sensor value. There are various messaging solutions you could try where one Java app can talk to another via the message channel. Or you could use some mechanism like Terracotta where specified objects can be kept synchronized between different VMs. Those might be huge overkill though.

If you have a totally different app, maybe something written in C++, running someplace else, something you have no control over or no source code/etc. to modify it, or hook into it to connect it up to a listener or a messaging solution, then you could periodically poll the database, but that is usually not very performant, although the frequency and the cost of the polling might be so low that it doesn't matter.

If the sensor value is modified via some stored procedure, then maybe that procedure could be modified to somehow call some other process. IIRC, in Oracle you can embed Java in a stored procedure.

That said - triggers or listeners would be the preferred solutions. If I had a Java app that was setting reading the sensor value, and I had access to the code and I was allowed to modify it, I would prefer the listener method myself as then that isolates me from the DB - although if some other process could conceivably modify the value without the Java app knowing then the triggers approach would be preferred. It just depends on your situation.

Last edited by Developer Dude; 03-12-2009 at 02:51 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Wrote java program and can't connect to sql database mokoena Programming 1 07-12-2008 10:01 AM
Which database program silkmaze Linux - Software 1 10-18-2005 06:07 PM
sms program and database in linux web program in windows.. does not see each other.. keikun_naruchan Programming 0 07-06-2005 01:40 AM
Stop java program(threaded program..should end cleanly) rmanocha Programming 4 11-09-2004 09:36 AM
java prog with database keikun_naruchan Programming 1 06-29-2004 04:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration