thanks guys, i'm starting to understand all this stuff about sessions and units of work. however i'm still having some trouble here, at least this time it occurs all the time.
I'm using the HibernateUtil as is from caveat-empor (this is nice!) ... I'm also using the HibernateThreadFilter and HibernateListener from that project as well.
I've changed my DAO to look like this:
Code:
public class DAO<T, PK extends Serializable> implements GenericDAO<T, PK> {
private Session session;
private Class<T> type;
public DAO() {
session = HibernateUtil.getSessionFactory().getCurrentSession();
this.type = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
}
@SuppressWarnings("unchecked")
public PK create(T o) {
Serializable pk = null;
try {
pk = session.save(o);
} catch (HibernateException e) {
handleException(e);
}
return (PK)pk;
}
//similar crud methods omitted
this is better! now the problem i'm having is, the first hibernate session opens and gets closed. when i try to do a second database transaction, i get a session is closed exception.
Code:
avax.servlet.ServletException: Unhandled exception caught by the default exception handler.
edu.upmc.ccweb.hibernate.filter.HibernateThreadFilter.doFilter(HibernateThreadFilter.java:75)
root cause
net.sourceforge.stripes.exception.StripesServletException: Unhandled exception caught by the default exception handler.
net.sourceforge.stripes.exception.DefaultExceptionHandler.handle(DefaultExceptionHandler.java:40)
net.sourceforge.stripes.controller.StripesFilter.doFilter(StripesFilter.java:218)
edu.upmc.ccweb.hibernate.filter.HibernateThreadFilter.doFilter(HibernateThreadFilter.java:48)
root cause
edu.upmc.ccweb.hibernate.DataAccessLayerException: org.hibernate.SessionException: Session is closed!
edu.upmc.ccweb.hibernate.DAO.handleException(DAO.java:93)
edu.upmc.ccweb.hibernate.DAO.getQuery(DAO.java:86)
edu.upmc.ccweb.dosimetry.DAO.PatientDAO.getInactive(Unknown Source)
edu.upmc.ccweb.dosimetry.manager.PatientManager.getInactive(Unknown Source)
edu.upmc.ccweb.dosimetry.controller.ListInactivePatientsActionBean.listInactivePatients(Unknown Source)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
net.sourceforge.stripes.controller.DispatcherServlet$6.intercept(DispatcherServlet.java:601)
net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:145)
net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor.intercept(BeforeAfterMethodInterceptor.java:104)
net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:142)
net.sourceforge.stripes.controller.ExecutionContext.wrap(ExecutionContext.java:72)
net.sourceforge.stripes.controller.DispatcherServlet.invokeEventHandler(DispatcherServlet.java:599)
net.sourceforge.stripes.controller.DispatcherServlet.doPost(DispatcherServlet.java:155)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
net.sourceforge.stripes.controller.StripesFilter.doFilter(StripesFilter.java:215)
edu.upmc.ccweb.hibernate.filter.HibernateThreadFilter.doFilter(HibernateThreadFilter.java:48)
so what am i doing wrong?
here's my web.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<filter>
<filter-name>Hibernate Filter</filter-name>
<filter-class>edu.upmc.ccweb.hibernate.filter.HibernateThreadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Hibernate Filter</filter-name>
<dispatcher>REQUEST</dispatcher>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter>
<display-name>Stripes Filter</display-name>
<filter-name>StripesFilter</filter-name>
<filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class>
<init-param>
<param-name>ActionResolver.UrlFilters</param-name>
<param-value>/WEB-INF/classes</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>StripesFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>StripesFilter</filter-name>
<servlet-name>Freemarker</servlet-name>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>StripesFilter</filter-name>
<servlet-name>StripesDispatcher</servlet-name>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>StripesDispatcher</servlet-name>
<servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>StripesDispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Freemarker</servlet-name>
<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
<init-param>
<param-name>TemplatePath</param-name>
<param-value>/</param-value>
</init-param>
<init-param>
<param-name>template_update_delay</param-name>
<param-value>0</param-value> <!-- 0 is for dev only! Use higher value otherwise. -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
<listener>
<listener-class>edu.upmc.ccweb.hibernate.listener.HibernateListener</listener-class>
</listener>
</web-app>
again, the first database access works great, any one after that and the session is closed. also i have
hibernate.current_session_context_class=thread
in my hibernate.properties
thanks again, actaully reading those articles was a big help. I now understand why managing this stuff really isn't part of hibernate's job (i wish it was though :P ).
the only thing i changed in the Filter, Listener and Util classes was the package name, I'm not sure why it's not working correctly (it's probably something simple i hope)