-4

I want to write a program which polls a particular table for new entries and writes them to a message queue.

I was thinking of writing a thread which will loop infinitely and try to get database exclusive lock and thread.sleep() will be called if lock is not acquired.

Then after getting lock if no new records are found in database thread.sleep() will be called again. Is thread the best way for this or should something like timer be used?

EDIT: Basically the question boils down to this: If I need to poll an oracle database using java should I write a thread, or something else like a timer/sheduledExecutor/trigger?

  • 3
    "Is there a better approach?" questions are not a good fit for Stack Overflow. You're better off writing some code yourself and posting it to [Code Review](http://codereview.stackexchange.com). – Michael Aug 09 '17 at 12:05
  • now a days every task related with polling or long polling is considered a bad practice... you should take a look to some listeners for that... – ΦXocę 웃 Пepeúpa ツ Aug 09 '17 at 12:07
  • @ΦXocę웃Пepeúpaツ Could you elaborate ? – user8096068 Aug 09 '17 at 12:17

1 Answers1

0

Getting lock on table being used on production is a bad idea. I would suggest to create a trigger table and add entries in trigger table only when new entry is being inserted in the original table. Now you can use your approach on that trigger table.

  • Thanks. But i would still need lock on the trigger table as well as a column in the table needs to be updated once it is picked by the poller. – user8096068 Aug 09 '17 at 12:16
  • You will need lock on trigger table, but you have created that table for these purposes only. Why do you need to update column in original table? – Mukesh Kumar Aug 09 '17 at 12:25
  • I meant I would still need to lock the trigger table as there is a column in the trigger table that needs to be updated. Anyway, my main issue is not about the lock. It's about whether an infinite looping thread is the best way to poll the table. – user8096068 Aug 09 '17 at 12:45