S
S
satmurat2014-01-23 17:44:03
Java
satmurat, 2014-01-23 17:44:03

How can I get "fresh" data from hibernate?

The problem is this: I have a MySQL DB. First, the first Java application will start, it will hibernate on DB1. Then another application updates the records in the same database1. After that, when trying to get the updated data in the first application through a SQL query, hibernate still returns the old data, as if from a cache. Google did not help: I ​​tried to disable the level 2 cache, session.clear(), etc. What could be the problem?

Query query = session.createSQLQuery("select id from users where city = 'Moscow' limit 1");
List result = query.list();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DieMust, 2014-01-24
@satmurat

Did you do session.flush() or transaction.commit() (depending on how you implement requests) after you sent new data?
The save and saveOrUpdate methods send information to the cache, after which the request must be confirmed in order for it to be executed.

N
NightFantom, 2014-08-16
@NightFantom

This is how it should be when changing data:

Session sess = factory.openSession();
 Transaction tx;
 try {
     tx = sess.beginTransaction();
     //do some work
     ...
     tx.commit();
 }
 catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
 }
 finally {
     sess.close();
 }

Source

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question