Oui nous avons instauré un filtre qui permet d'ouvrir et fermer une session. Pour ce faire tu dois créer la classe SessionFilter voir code ci-dessous et configurer ton ficher web.xml voir exemple ci-dessous:
Bonne chance
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>XXX.Util.SessionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
public class SessionFilter implements Filter {
/**
* A servlet filter that opens and closes a Hibernate Session for each request.
* <p>
* This filter guarantees a sane state, committing any pending database
* transaction once all other filters (and servlets) have executed. It also
* guarantees that the Hibernate <tt>Session</tt> of the current thread will
* be closed before the response is send to the client.
* <p>
* Use this filter for the <b>session-per-request</b> pattern and if you are
* using <i>Detached Objects</i>.
*
* @see org.hibernate.ce.auction.persistence.HibernateUtil
* @author Christian Bauer <
[email protected]>
*/
private static Log log = LogFactory.getLog(SessionFilter.class);
public void init(FilterConfig filterConfig) throws ServletException {
log.info("Servlet filter init, now opening/closing a Session for each request.");
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// There is actually no explicit "opening" of a Session, the
// first call to HibernateUtil.beginTransaction() in control
// logic (e.g. use case controller/event handler, or even a
// DAO factory) will get a fresh Session.
try {
chain.doFilter(request, response);
// Commit any pending database transaction.
HibernateUtil.commitTransaction();
} catch (RuntimeException ex) {
log.debug("Rolling back the database transaction.");
HibernateUtil.rollbackTransaction(); // Also closes the Session
System.err.println("Rollback Transaction");
// Just rollback and let others handle the exception, e.g. for display
throw ex;
} finally {
// No matter what happens, close the Session.
HibernateUtil.closeSession();
}
}
public void destroy() {}
}