-->
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.  [ 5 posts ] 
Author Message
 Post subject: Open session in view
PostPosted: Sat Sep 12, 2009 6:31 am 
Newbie

Joined: Sat Sep 12, 2009 6:03 am
Posts: 5
Hi
i am a newbie to the hibernate
Can you explain me what is the need
of to open the session in the view

is n't it a bad design pattern

why not we close the session in our controller class
but before close the session
we store all the data in a collection
and set that collection in the request scope
then show the data in the view

after open the session in the view
then get the object from the session
is n't we trying to access the database entity
in our view

here where is the MVC pattern

i am confuse
please help me


Top
 Profile  
 
 Post subject: Re: Open session in view
PostPosted: Sat Sep 12, 2009 10:24 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, open session in view allows you to access the data you need, when you need it. Otherwise, you need to anticipate all of the data your view will need, and then close the session. Having the back end try and figure out the needs of the front end IS a bad design.

Open session in view is very efficient.

_________________
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: Re: Open session in view
PostPosted: Tue Sep 15, 2009 1:59 am 
Newbie

Joined: Sat Sep 12, 2009 6:03 am
Posts: 5
Hi
thanks for your reply
but i have to ask another question

suppose we use the open session in 50 view(osiv)
and there would be reqirement to delete that data
which is used by these view
then will it not cumbersome to update all these views
according to our current data

suppose we delete that references from our tables
that is used by the views

so test all these view according to our updated data
and have to prepare to catch all the exception
that is generated by these types of views

and one more question
how i integrate hibernate in my web application

1) by using servlet filter
2) by using plugin in my struts-config file
3) Simply call the HibernateUtil class in my action class
and get the session and then doing all the operation

Suggest me which one will be better to use


Top
 Profile  
 
 Post subject: Re: Open session in view
PostPosted: Tue Sep 15, 2009 8:21 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
suppose we use the open session in 50 view(osiv)


Well, a transaction isolation level eliminates any problem here, so it's not to worry. And updating is not cumbersome - that is the whole purpose of Hibernate!!! :)

Quote:
suppose we delete that references from our tables


It is all done transactionally and each transaction is isolated. This is how every J2EE works, so it's no different for Hibernate or Toplink or Open Session in View or Session per query.

Quote:
how i integrate hibernate in my web application


Each way you mention will work! The best and easiest way to implement "Open Session in View" is with a filter. That's always been my preference.

Great questions!

_________________
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: Re: Open session in view
PostPosted: Wed Sep 16, 2009 5:18 am 
Newbie

Joined: Sat Sep 12, 2009 6:03 am
Posts: 5
Thanks for your
reply
i want to discuss with you
of the making of HibernateUtil class
i declare in the HibernateUtil class

Code:
   private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

that is right
from that code we generate a particular session for each thread
that sounds good by using ThreadLocal class


but i saw
another declaration

Code:

private static final ThreadLocal threadTransaction =new ThreadLocal();



that code is generate one transaction for particular thread
that sounds, little bit confuse me
suppose if thread first goes to save the data
but first it delete the data
then it save

then for all sql operation the one transaction is used

is it right
suppose transaction executes a long batch of query

why not consider a separate transaction

for each operation in the database
like save() or delete() or find by id()
means

for save operation
Transaction tx=session.beginTransaction()
getting the session
doing the database operation
then commit the transaction

same for delete

in the first case
for
one request
one session
one transaction

in second case
for
one request
one session
multiple transaction

can transaction should be unique for one request

ya i have one more question
do i need for setting the transaction
isolation
or
can i use default hibernate transaction isolation


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.