These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Can I lock the reocrd on first request?
PostPosted: Fri Jun 13, 2008 1:05 pm 
Newbie

Joined: Fri Jun 13, 2008 12:18 pm
Posts: 2
Hi,

I am new in using hibernate and found an article related to "Open Session in View" pattern and have somes questions.
http://www.hibernate.org/43.html

1. Can I lock the records (by select for update) when the first request arrived on the server, and then display the data on the page, after user updated the data and click submit, I update the records and commit the transaction?


2. Does the impact of currentSession.getTransaction().commit() in the HibernateSessionConversationFilter same as the database transaction commit? e.g. the data are persisted in database.

3. I read the book Java Persistence with Hibernate (REVISED EDITION OF HIBERNATE IN ACTION) and it mention the sequence of the Interception of events to manage the lifecycle of a Session in figure 11.4 as follow

A. the first transaction (first request)
1. s = sf.openSession()
s.setFlushMode(MANUAL)
MSC.bind(s)
2. s.beginTransaction()
3. s = MSC.unbind()
4. commit()

B. the second transaction (second request)
5. MSC.bind(s)
6. s.beginTransaction()
7. s = MSC.unbind()
s.flush()
8. commit()
9. s.close()

Why it call commit() at the first transaction ? (in the step 4), Does this commit will persist the data to database and cannot be rollback on the second transaction?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 14, 2008 9:25 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
2. Does the impact of currentSession.getTransaction().commit() in the HibernateSessionConversationFilter same as the database transaction commit? e.g. the data are persisted in database.


Indeed, when you commit the transaction, the data is definitely persisted to the database. So, when the request comes in, you grab your session and even open your transaction. THen you do everything, including accessing your data during the view operation of your JSP, and then your transaction gets comitted, and everything that is part of that transaction, and part of that session's transaction, is persisted to the database. It's all very elegant and fairly simple!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 7:53 am 
Newbie

Joined: Fri Jun 13, 2008 12:18 pm
Posts: 2
Hi, Cameron McKenzie

Is it mean that we must commit every transaction within the session?

Can I lock the record on the first request and then update and commit it at end of the conversation? (e.g. Pessimistic Locking)

For example, when use request a web page to update the record, I lock the record first (be select for update) and show the web page for user to update. (At the moment, other users cannot update this record) After updating, the user will click on a button, when I receive this second request, I update the record and commit this transaction to release the lock of the record for other user update. (similar to the client server logic)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 3:54 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
You definitely don't want to lock the record over multiple request-response cycles. I mean, what if the person never clicks again. Then that record will have a really long lock.

Just open and close the session with each request-resposne cycle. WHen the user is ready to commit, commit everything - open the session, write to the database, commit and close. That's all there is to it!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.