Reputation: 813
I have code that select all rows of a database and deletes them. It performs this process continuously. When there is no data in the database, it sleeps for 2 seconds then tries again. My problem is the following, when new data is added to the database, my code does not see it.
The test:
I ran my code, it was waiting for new data and a few seconds later I manually added data to the database. My code did not see the new row at all. I am 100% sure the data I added is actually in the database.
I am using Hibernate with MySQL.
The code:
String queryString = "select * from rawleads";
SQLQuery query = session.createSQLQuery(queryString);
ArrayList list = (ArrayList) query.list();
while (list.isEmpty()) {
Thread.sleep(2000);
query = session.createSQLQuery(queryString );
list = (ArrayList) query.list();
System.out.println("refreshing table");
}
System.out.println("Processing...");
I think it could be related to the cache since the code is unable to see changes made to the database. Thanks for your help.
Upvotes: 0
Views: 777
Reputation: 5181
You will probably have to create a new transaction before each new iteration.
edit:
while (running) {
Transaction t = session.beginTransaction();
try {
String queryString = "select * from rawleads";
SQLQuery query = session.createSQLQuery(queryString);
List pendingLeads = query.list();
if (pendingLeads.isEmpty()) {
Thread.sleep(2000);
} else {
process(pendingLeads);
}
t.commit();
} catch (Exception e) {
t.rollback();
}
}
Upvotes: 2