Greetings all, my first foray into the message groups, so forgive is a thread already exists (I've been searching!) In retrospect, sorry for the long message - I'm not great with all the terminology.
This is a c concurrency issue - I don't understand the best way to deal with it (the code below works, but is there a better way to control this?) I'm not a subject matter expert, so forgive my ignorance as I learn...
I'm working w/ Spring 1.2x / Hibernate 3.1 in a Struts frontend enviro:
Here's the issue: I do NOT want to store the actual (detached) domain POJO into an HttpSession, etc., but will do so if need be if I can find a better way around this issue... My struts forms don't represent the entire domain object, just a few fields that it has. Nor can I move to Spring Web - stuck w/ Struts for the time being...
On the callback for an update from a simple web form, I move the struts fields that were edited (including the hidden version field) back into the domain object AFTER issuing a getById (to fill in the fields that the user doesn't get access to!) The problem is, that when I move the struts version tag into the POJO, no matter what the value is, an update is always successful (aka - Hibernate seems to maintain the version check / value separately in the Session, not referring to the POJO value at this point). As a "fix" to this, after updating the POJO, I evict and then lock it - that does the version check stuff successfully!
Is there a better pattern / way to deal with this? I could change the call to get the current object w/ a version check in the query, but don't want to deal with issues why I get 0 rows back (is the row deleted, is the version incorrect, etc.?) (If I don't execute an evict/lock, the update always works regardless of the POJO version value).
Here's a flow as stands now:
0) User requests to edit a "Buiness Rule" row.
1) Get Object by ID.
2) Copy editable fields from POJO domain object to struts form (including the retrieved version # from the read to a hidden field on the web form)
(User makes edits, submits form)
3) Get the domain Object by ID. (based on the id in the Struts form)
4) Populate changes into the Domain Object (move struts fields into POJO fields, including the "original" version value)
5) Evict the Object
6) Lock the Object (if the version if out of synch, I get the expected "dirty" result)
Here's a simple code snippet:
Code:
public void update(BusinessRule bR) throws Exception {
HibernateTemplate ht = getHibernateTemplate();
BusinessRule originalBR = getById(bR.getPlayerId());
originalBR.setRuleNm(bR.getRuleNm());
originalBR.setVersion(bR.getVersion());
...
ht.evict(originalBR);
ht.lock(originalBR, LockMode.READ); //versions not the same, dirty exception!!
ht.saveOrUpdate(originalBR);
Thanks.
Psantt.