I use JDBC, I need to constantly check the database for changing values.
What I have is an infinite loop, an inner loop repeating variable values, and every iteration check against the database.
public void runInBG() { //this method called from another thread while(true) { while(els.hasElements()) { Test el = (Test)els.next(); String sql = "SELECT * FROM Test WHERE id = '" + el.getId() + "'"; Record r = db.getTestRecord(sql);//this function makes connection, executeQuery etc...and return Record object with values if(r != null) { //do something } } } }
I think this is not the best way.
Another way that I think of is the opposite to continue to iterate over the database.
UPDATE
Thanks for the tip about the timers, but I don't think this will solve my problem. As soon as a change occurs in the database, I need to process the results almost instantly against the changing values ββ("els" from the example code).
Even if the database does not change, it should still constantly check for changes in values.
UPDATE 2
Well, to everyone who is interested in the answer, I believe that I have a solution now. Basically the solution is NOT to use the database for this. Download, update, add, etc. Just what you need from the database to memory. Thus, you do not need to constantly open and close the database, you only deal with the database when you make changes to it, and reflect these changes in memory and do only what is in memory at that time. I am sure that this is a more intensive amount of memory, but performance is the absolute key here.
Regarding the periodic answers "timer", sorry, but this is not at all. No one answered for some reason how using timers would solve this particular situation.
But thanks again for the feedback, but it helped anyway.
user1597002
source share