Thank you, I actually got it working, but i'm not sure it is the best way.
I've set the global transaction level in mysql to read committed.
The code when a user selects something to update:
Code:
net.sf.hibernate.Session sess = InitServlet.currentSession();
sess.connection().setAutoCommit(false);
Ond ond = (Ond)sess.load(Ond.class, ondVAF.getOndnr(),LockMode.UPGRADE);
(1 <-- )
I'm using Threadlocal for the session:
Code:
public static final ThreadLocal session = new ThreadLocal();
...
public static Session currentSession()
throws HibernateException {
Session s = (Session) session.get();
if (s == null) {
System.out.println("new session");
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
When the user submits the updated data:
Code:
sess = InitServlet.currentSession();
...
sess.saveOrUpdate(ondUpdate);
sess.flush();
...
InitServlet.closeSession()
The result is that the record is locked and that when a second user selects a record to update that a first user already is updating, the second user has to wait until either the first user commits his update or the lock timeout is reached, then a sqlexception is thrown.
Now, to achieve this , i have to keep the session open for as long as the user is updating the record. I have tried to use session.disconnect() at (1 <-- ) in the first code snippet and session.reconnect() later on, but that doesnt work; the record is no longer locked.
Is the the way i'm doing it now actually considered bad practice, is there a better way?