开发者

how to call a java method when a record inserted in mysql database

开发者 https://www.devze.com 2023-03-16 23:16 出处:网络
Please tell me is it possible to call a java method or a struts action class when a record is inserted in mysql database

Please tell me is it possible to call a java method or a struts action class when a record is inserted in mysql database Actuall my problem is i have 2 databases(both are mysql databases namely db1 and db2) running in two different locations.I need communication between them.When a record is inserted in X table in db1 at the开发者_Python百科 same time some data must be insert in Y table in db2 depending up on the data inserted in db2 some data must be inserted in Z table in db1.

Actuall my project is patient care project for a group of patients one care taker is there

for each and every patient one embeded button is there. the mapping between the patient details and the embeded button and the corresponding care taker is there in db2 database when the patient pressed the embeded button embeded information is inserted in db1 and inorder to send a mobile message to care taker i haveto get the data from db2 depending up on the embeded information inserted in db1 as patient and caretaker mapping is in db2

the businesslogic communicate with db1 iswritten in vc++

the businesslogic communicate with db2 iswritten in java

so i need communication between db1 and db2

technologies used

java,struts 1.2

database : mysql


I don't think it it is possible to trigger java code actively from db insert. What we do is the opposite:

  • when an insert occurs, we write the rowid to a dedicated table
  • we have a Java job that runs regularly and checks that table for new entries

You should not do any such thing in a web context, so forget struts in this context!


You can have a java process periodically query the table to check for new records. A better solution would be to have a database trigger perform the business logic you need (though you can't call the struts action directly). If you can provide more detail on what business logic you need performed after the row is inserted, we can maybe provide a much cleaner design.


Even though it's possible to use another Struts action (As it's a simple POJO class that can be instantiated like SomeAction action = new SomeAction()) I would not recommend it for few reasons:

  • You will have to pass various Struts configurations to the action in order to work.
  • Handle the ActionForward response.

My suggestion is to put your business logic in a service that you can either pass the same data to 2 databases or have strategy that understands that the data needs to be recorded to 2 databases.


Here's an extension for MySQL (UDF) that allows execution of other programs from MySQL.

It should be relatively straightforward how to use it. I don't know ins and outs of your application, but you can create a trigger that calls a program and passes certain parameters you require to establish your db communication.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号