This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes JDBC and the fly likes MySQL triggers calling java method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "MySQL triggers calling java method" Watch "MySQL triggers calling java method" New topic
Author

MySQL triggers calling java method

Kyle Fernandes
Greenhorn

Joined: Apr 20, 2011
Posts: 8
Hi all,

I wanted to know if it is possible to create a trigger in a mysql database that calls/runs a java class from a java desktop application.How can one go about doing this?any help would be very appreciated...

thank you.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

MySQL is a RDBMS which doesn't know about Java or any other language for that matter. What exactly you are trying to accomplish here? You may need to do the reverse? Or can't you use a procedure/trigger in the MySQL itself to do this?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 371
Different RDBMS know about different languages for triggers. E.g. you can write Java in Oracle.
Kyle Fernandes
Greenhorn

Joined: Apr 20, 2011
Posts: 8
ok lemme give more details.
there are two java systems that are communicating with the mysql database. One inserts data and the other only views data. How it should work is that when the first system inserts some data into the database the second system should display the information that was inserted. I was trying to do this by placing a trigger where when there is an insert into the database a method in the second system should be called that reads the data and then displays it.

Im not sure if this is the best approach towards this.

Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 371
So basically you want to notify another system when changes in the database appear. While there may be database systems that allow this (IIRC MS SQL Server has a kind of change notification) it is not a good idea to trigger a change from a system lower in the layer hierarchy to a higher system. You certainly don't want your database to be dependent on response times of another system. But in general this would be independent of the language the trigger is written in; if you could call a webservice in a trigger you just could use this. You can't, in MySQL, as far as I know, for the very same reason.

You need a business layer, another app, that controls database access and notifies other systems. Sounds like an ESB could get handy here. Or some other means of distributed messaging. You certainly don't want the whole system to hang just because the notification for the view fails.

If it is a really, really tiny system, that will not be extended and reliance is not important, you could just let the input system notify the view system. No, don't do this. Don't go to the dark side.
Ramesh Sunkara
Ranch Hand

Joined: Mar 01, 2010
Posts: 78
My approach is..

Write a quartz job which checks the database for every configured period of time and notifies if there is any change.

It may not be a good method but just an another way to do your job.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 371
Sure, polling can be a viable way, if the circumstances fit (few systems, new real time updates, not many requests, no possibility to introduce a business layer).
Ramesh Sunkara
Ranch Hand

Joined: Mar 01, 2010
Posts: 78
Thanks Hauke Ingmar Schmidt for making me more clear when I can use Quartz.
Arthur de Vaan
Greenhorn

Joined: Jun 13, 2012
Posts: 1
You may try a package I created for exactly this purpose: MySQL Trigger calling Java method

No polling but event based so much better performance than polling. Uses some native code though but this is included in the jar.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MySQL triggers calling java method
 
Similar Threads
Advice of database type is needed
MySql connection issue
No suitable driver
Problem with com.mysql.jdbc.Driver driver
connecting to online SQL DB