-->
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.  [ 7 posts ] 
Author Message
 Post subject: open session in view over servlets
PostPosted: Thu May 08, 2008 5:50 am 
Newbie

Joined: Thu Feb 21, 2008 8:39 am
Posts: 15
I'm working with servlets and hibernate, and I would like to implement an open session in view pattern, a bit different of the normal implementation (the documentation way).

What I would like to do is to keep one hibernate session per http session. When user logs, open the session, when user log out close the session. And among user calls begin and end transactions, but with the same session I start at logging time. In that way just use one session per user.

What do you(all) think about this approach?

Or is better to start a session for each http request?

Thanks in advance.

Albert


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 6:23 am 
Regular
Regular

Joined: Wed Apr 25, 2007 11:44 pm
Posts: 59
most probably i think you should use the implementation as mentioned in the documentation

but

this is not a hard & fast rule you still can use the technique as you mentioned; but you should be able to judge properly that

does this technique suits your application?
is it an extensible solution?
if it is a web application how many users can do it simultaneously ?


Top
 Profile  
 
 Post subject: :_-(
PostPosted: Thu May 08, 2008 8:04 am 
Newbie

Joined: Thu Feb 21, 2008 8:39 am
Posts: 15
I was trying to implement the documentation architecture but I already had problems with concurrency. The filter I use is the documentation one:

Code:
   SessionFactory factory = null;
   Session session = null;
   @Override
   public void destroy() {
      // TODO Auto-generated method stub      
   }
   
   @Override
   public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {   
            HttpServletRequest httpReq = (HttpServletRequest)req;
            session = factory.getCurrentSession();         
                        
            try{
               session.beginTransaction();
               chain.doFilter(req, resp);
               session.getTransaction().commit();
            }catch(Exception e){
               if(session.getTransaction().isActive()){
                  session.getTransaction().rollback();
               }
               throw new ServletException(e.getMessage());
            }
   }

   @Override
   public void init(FilterConfig arg0) throws ServletException {
      factory = HibernateHelper.getSessionFactory();
   }


but when I execute, let say 5 servlet http request simultaneously, hibernate does not work at all? it just says:

org.hibernate.SessionException: Session is closed!
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)

--> session.beginTransaction() function could not create a session.

I think this problems are caused because the first transaction has not finished when I'm trying to open a new transaction. I'm trying to change the behavior of getCurrentSession in the hibernate config file, but no luck yet.
Code:
        <property name="current_session_context_class">thread</property>


Any idea, or design pattern I could use?

Thanks for answering.

Albert Solé


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 2:34 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
This is not at all how Session is meant to be used. Hibernate Session matches up with a single HTTPRequest (and a single Transaction and a single pooled Connection) very nicely - they both encapsulate a unit of work. OSIV is a well-established pattern because it works.

You can use long-running Sessions and extended persistence contexts in certain cases when you know what you're doing, but trying to save resources by attaching a Session to a ... Session (hehe) is no good; just don't do it.

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 09, 2008 1:18 am 
Regular
Regular

Joined: Wed Apr 25, 2007 11:44 pm
Posts: 59
i have done this thing long time ago but didn't had any exception like that

check my sample from location http://www.1-clickshare.com/download.ph ... ec31f_6118


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 3:12 am 
Newbie

Joined: Thu Feb 21, 2008 8:39 am
Posts: 15
First of all, thanks for answering.

I just would like to say that I'm not trying to invent anything, I'm just trying to solve a problem that I think is really frequent, but due to my skills on hibernate, I don't know how to solve.

The main problem I found on using he open-session-in-view pattern is that all the http requests share the same session, I mean one session per thread. This is manage by the session factory class.
The Filter executes the three following sentences:
Code:
1:            sf.getCurrentSession().beginTransaction();
            // Call the next filter (continue request processing)
2:            chain.doFilter(request, response);
            // Commit and cleanup
            log.debug("Committing the database transaction");
3:            sf.getCurrentSession().getTransaction().commit();


If you take a look at this code, it is very easy to have two or three http calls before line 2 finish for the first http call. If this happens, you are trying to open as much transactions as the http calls you have. Which produces an exception. This is from where my exception comes from.

What msj4u is doing is a bit different (which is an smart approach), open a new session each time you get a new connection, however, this will decrease the performance of the web applications.

Your two approaches will fulfill my requirements, open a session for each http request. I'll take the idea.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 4:11 am 
Regular
Regular

Joined: Wed Apr 25, 2007 11:44 pm
Posts: 59
mention not buddy

keep sharing the ideas and techniques with others

indeed you have to decide yourself that what to choose according to your need; mine was reliability so i selected that

:)


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