1

Good Morning, I have a column in a table which increments on success.In my service layer I have got

MyDomain myDomain = myDomainDAO.findById(myId);
myDomain.setValue(myDomain.getValue()+1); 
myDAO.saveorupdate(myDomain);

secondary cache is switched off in my case. When multiple threads call to increment the value the results are inconsistent. Which can happen as hibernate threads will generate the sql in random and execute at random. I solved the problem using HQL. I would like to know how this can be achieved by the above code. Given that my secondary cache is off is there anyway to know the last save value in the database. Many Thanks

4
  • 2
    The only way you could serialise the updates in the above code in a multithreaded environment would be to use a locking strategy, but I would definitely recommend using the HQL solution instead and let the database deal with the locking. Commented Dec 9, 2011 at 11:21
  • Correct and thats what i have done but then it locks the LOCK.FORCE in hibernate 3.2 and does not allow to anyone to read that record . so not good. Commented Dec 9, 2011 at 12:47
  • Is there some problem with using database transactions? Commented Dec 9, 2011 at 13:23
  • Not really . the problem is hibernate will create the update statements and execute at random. but i had to use lock to stop other threads accessing that record which is not good at all. Commented Dec 9, 2011 at 15:15

1 Answer 1

1

You may read about transaction isolation, it's a key concept about transactions that can be set at different levels as far as i know (you can set it in your db config, your app server and also in Spring Transactionnal annotations but i don't know exactly the rules to garantee which level will be applied). http://static.springsource.org/spring/docs/1.2.9/api/org/springframework/transaction/annotation/Isolation.html

Btw in a concurrent environnent if you just want to increment a counter you must know that transaction isolation has an impact on performances. Using @Transactionnal(Isolation.SERIALIZABLE) will garantee consistency but perhaps it will be as fast as putting a synchronized block in your dao incrementhing method...i'm not an expert, figure it out yourself :)

Sign up to request clarification or add additional context in comments.

4 Comments

How do i get an object back from hibernate secondary cache . i could then compare the values in current session against secondary cache.Any idea?
??? If the object is cached then you just have to ask for it and you'll get it, and it will be the same object
when i do session.get() it will get me from the current session but i want to compare it against what is cached in the l2 cache which might be different as a result of it being updated by another thread in a different session. Hence i want to know how i can retrieve it from the secondary cache
Then perhaps you should try to retrieve the same object in another session, sorry i'm not familiar with L2 cache

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.