简体   繁体   中英

Intimation to JAVA web application when a DB table is altered

I know many people would strike this question off by just looking at the title but it is important for me.

I have a requirement where our Java application should be aware if there is any table altered in DB(we use mysql.). The best I could think right now is, I write a trigger, if a table 'x' is altered then I insert a record into table 'Y' stating that table 'X' is altered.

Now, in my java web application, I'm thinking of running a thread for every minute by Quartz scheduler to check whether there are any new records in table 'y' that way I could find that table 'X' is altered.

here's where I need suggestions. 1)How to write sql trigger which gets fired when a particular table is altered? 2) My boss told me, running a thread every minute is an expensive operation to consider.

Is there any alternative other than running a thread to find if a table altered in DB?

I'm really in need of finding any solution to this problem. Help is appreciated.

Update :

I have two web applications say, WA1 and WA2 and both share same database say DBS. If user performs an operation in WA2 then DBS table gets altered(new column gets added). So, my WA1 should be aware that an operation is performed in WA2. I thought of following the 2 points I mentioned above. Is there any better way to do this?

And can anyone give me a mysql Trigger which gets executed if there's a table say mytbl is altered?

I keep hearing about Java Stored Procedures and Triggers and UTL_HTTP.REQUEST but it is for Oracle. Is there anyway to achieve this in MySQL?

My simple solution:

  1. Implement a quartz scheduler job which runs every minute. Taking note that quartz is effecitvely a thread pool so it is up to how you can make it not least expensive.
  2. In the job, cross check information schema with whatever mechanism which allows you to compare the difference in the columns. Hopefully, keep this to subseconds...

Just my two cents of thoughts

The simplest solution would be to have the person who alters the MySQL tables run the Java application.

Assuming you can't do that, you can read the MySQL binary log to see the “events” that describe database changes such as table creation operations or changes to table data. You would have to run your thread every so often. Maybe once every hour.

Well, I am not sure about your requirement and DB design or if it'll be suitable for you, but as I think in WA1 whatever code is responsible to alter your table, there itself you can write some code to intimate your WA2 that what columns are added and in which table those are added. If both applications are using same technology , a simple request can be generated to wA2 to behave accordingly otherwise web-services can be used.

Edit: DataBase is meant for persistence storage and DataBase is not recommended For communication between multiple application. For communication between multiple applications Web-Services/Rest should be used.

Have a look at this project: Q4M

This is a message engine based on MySql Still in its early stages but it should be good enough for your requirments.

try to look at JPA/Hibernate listeners. might be a good place to start. I did some googling: https://code.google.com/p/simplejpa/wiki/EntityListeners , or here: http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/listeners.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM