-->
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.  [ 3 posts ] 
Author Message
 Post subject: Session member variable in servlet destroys the world??
PostPosted: Tue Feb 28, 2006 12:51 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
We had a crazy issue with connections staying open, and our solution was to move the Session variable from class scope to inside the doGet(). Nothing else was mucking with the member variable, and I can't figure out why this could be an issue. It was only a problem when the servlet was being hammered.

Here is a (sample) of the bad code:

Code:
public class FileServlet extends HttpServlet  {
    private static final Log log = LogFactory.getLog( FileServlet.class );

    private Session session;
   

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            session = MySessionFactory.openSession();
            StoredFile file = (StoredFile) session.load( StoredFile.class, 5 );
            file.doAwesome();
        } catch (Exception e) {
        } finally {
            try {
                if (session != null) {
                    session.close();
                }
            } catch (Exception e) {
            }
        }
    }
}


Then all we did was make the Session object local to doGet() and it worked:

Code:
public class FileServlet extends HttpServlet  {
    private static final Log log = LogFactory.getLog( FileServlet.class );
   

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Session session = null;
        try {
            session = MySessionFactory.openSession();
            StoredFile file = (StoredFile) session.load( StoredFile.class, 5 );
            file.doAwesome();
        } catch (Exception e) {
        } finally {
            try {
                if (session != null) {
                    session.close();
                }
            } catch (Exception e) {
            }
        }
    }
}


It's always possible that something else was at play, but I really think the only thing we changed was the scope of that variable. It was NOT static. Is there something about Java, servlets, or Hibernate that I don't understand that would cause this behavior? We could duplicate it (and solve it) in both Tomcat and WebLogic environments.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 2:12 pm 
Beginner
Beginner

Joined: Fri Jul 30, 2004 2:53 pm
Posts: 33
Location: Washington, DC
Every class scoped variable in a Servlet is essentially static. When the app server first loads your web application, it reads the servlet configuration from web.xml. For each servlet defined in there, it instantiates an instance of the Servlet class, then calls the servlet's init() method. After it has done that for each servlet, your app is running. When the app server receives incoming requests, it maps the URL of the request to the servlet-mappings in your web.xml. If it finds a match, and the request is an HTTP GET request, it then calls the doGet method on the servlet that is already running. Another way of putting it is that servlets act as a Singleton. So, putting your Session variable as a class level variable is a big no-no. Having any hibernate code in a Servlet is a big arcitectual no-no as well, but that's a whole different story.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 3:51 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
That is a little bit terrifying.


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