How to poll database and trace the latest record using java?
I have a situation where user upload a new file when file successful submitte开发者_运维问答d then one records is inserted into database table.i will run other class,that poll database if new records is inserted retrieve file name then read file and insert all file records into database table.i'm not getting any idea to sortout this problem,please help me and provide your views for this situation.
Thanks
Well , it seems that you want to have a Java class that will periodically check a table (say TableA
) and process those new records that are inserted since last checking time.
You should at least have a column (eg ,polled_time
) to capture if a record is polled before or not . polled_time
is the timestamp when the record is last polled . It is null
if the record is never polled before.
Whenever the Java class starts ,it should select the records that are not polled for process (select * from TableA where polled_time is null
) . After process each record , you should update the polled_time
to indicate it is polled and avoid it is selected out again to process when the Java class runs next time ( update TableA set polled_time = now() where id= xxxxx
).
Finally, you have to setup a schedule task ( For Window platform) / a cron job (For Linux / Unix platform) to run this java class periodically ,
Why can't you perform both operations on the same servlet doPost()? Or get rid of the Filename table and just have the contents table and avoid the whole polling situation?
doPost(...) {
try {
validateFile(...);
updateFileTable(...);
updateFilenameTable(...);
} catch (...) {...} finally {...}
精彩评论