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.