When I execute some queries inside Hibernate transaction -> the data successfully updated in my mysql, but in my application there are still old values. When i restart - it`s Ok. If i set autocommit mode - works fine, but i have to use transaction ;-). Any ideas? Thanks in advance.
+1
A:
Manipulating the database directly with UPDATE doesn't affect the objects cached in the session. You should clear the session (Session.clear()). Something like:
session.flush()
session.clear()
query.executeUpdate()
Or even better, you can avoid the problem by not using update queries and manipulating the object state in memory:
myobj.setName(newValue)
session.saveOrUpdate(myobj)
Tzvetan Mikov
2009-12-23 18:09:40
yeah I have a feeling that this not being done, but not sure without more info :)
Arthur Thomas
2009-12-23 18:33:39
Yeah, I suspect there are some fundamental misunderstandings. 2nd level caching may be enabled without him knowing. The best advice I can give Nikolay at this point is to read the Hibernate manual from beginning to end :-)
Tzvetan Mikov
2009-12-23 19:00:02
I read all day about hibernate session/transactions... and mistery was solved ;-) The problem was not in update method, but in those which returns result. it simply construct criteria, and returns "uniqueResult" but it wasn`t inside transaction. Why i need transaction here? It`s only one select query? Thanks everyone for helping!
NikolayGS
2009-12-23 23:21:16
A:
May be i`m doing something wrong here is conqrete method:
public void updateOperation(OperationObject operationObject) throws Exception {
Session dbSession = openSession();
Transaction tx = dbSession.beginTransaction();
try {
//update turn over report
turnOverManager.updateTurnOver(dbSession, operationObject);
// if step 1 is OK update operation table
dbSession.merge(operationObject);
dbSession.flush();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
// send back error message to client
throw new Exception(e.getMessage());
} finally {
dbSession.close();
}
}
NikolayGS
2009-12-23 18:26:53
you should edit your original post instead of making answers like this. Stackoverflow doesn't work like a forum so this may make things confusing.
Arthur Thomas
2009-12-23 18:31:06
you may see a delete link at the bottom. I am not sure if you need to accumulate points for that one or not. Its ok if not.. just letting you know for future questions. :)
Arthur Thomas
2009-12-23 18:50:38