lol i use tomcat and struts too.
ok i'll explain it
first, create this call:
package org.infohazard.pig.servlet;
import java.io.*;
import javax.servlet.*;
import javax.naming.*;
import net.sf.hibernate.*;
/**
* Filter which manages a ThreadLocal hibernate session. Obtain the session
* by calling Persistance.getSession(). Define the JNDI name of the
* hibernate session factory as an init param to the filter in your web.xml.
*
* @author <a href="mailto:
[email protected]">Jeff Schnitzer</a>
*/
public class Persistance implements Filter
{
/**
* Filter init param which defines the JNDI name for the hibernate factory
*/
public static final String HIBERNATE_FACTORY_JNDI_PARAM = "hibernateFactory";
/**
* Default value if no init param is set.
*/
public static final String HIBERNATE_FACTORY_JNDI_DEFAULT = "java:/HibernateSessionFactory";
/**
* Holds the current hibernate session, if one has been created.
*/
protected static ThreadLocal hibernateHolder = new ThreadLocal();
/**
*/
protected static SessionFactory factory;
/**
*/
public void init(FilterConfig filterConfig) throws ServletException
{
// Initialize hibernate
try
{
new Configuration().configure();
}
catch (HibernateException ex) { throw new ServletException(ex); }
// As good a place as any to initialize the factory
String factoryJndiName = filterConfig.getInitParameter(HIBERNATE_FACTORY_JNDI_PARAM);
if (factoryJndiName == null)
factoryJndiName = HIBERNATE_FACTORY_JNDI_DEFAULT;
try
{
Context ctx = new InitialContext();
factory = (SessionFactory)ctx.lookup(factoryJndiName);
}
catch (NamingException ex) { throw new ServletException(ex); }
}
/**
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
if (hibernateHolder.get() != null)
throw new IllegalStateException(
"A session is already associated with this thread! "
+ "Someone must have called getSession() outside of the context "
+ "of a servlet request.");
try
{
chain.doFilter(request, response);
}
finally
{
Session sess = (Session)hibernateHolder.get();
if (sess != null)
{
hibernateHolder.set(null);
try
{
sess.close();
}
catch (HibernateException ex) { throw new ServletException(ex); }
}
}
}
/**
* ONLY ever call this method from within the context of a servlet request
* (specifically, one that has been associated with this filter). If you
* want a Hibernate session at some other time, call getSessionFactory()
* and open/close the session yourself.
*
* @return an appropriate Session object
*/
public static Session getSession() throws HibernateException
{
Session sess = (Session)hibernateHolder.get();
if (sess == null)
{
sess = factory.openSession();
hibernateHolder.set(sess);
}
return sess;
}
/**
* @return the hibernate session factory
*/
public static SessionFactory getSessionFactory()
{
return factory;
}
/**
* This is a simple method to reduce the amount of code that needs
* to be written every time hibernate is used.
*/
public static void rollback(Transaction tx)
{
if (tx != null)
{
try
{
tx.rollback();
}
catch (HibernateException ex)
{
// Probably don't need to do anything - this is likely being
// called because of another exception, and we don't want to
// mask it with yet another exception.
}
}
}
/**
*/
public void destroy()
{
// Nothing necessary
}
}
secondly declare the filter into web.xml
<!-- The hibernate filter configuration -->
<context-param>
<param-name>HIBERNATE_FILTER_SESSION_FACTORY_JNDI</param-name>
<param-value>java:comp/env/hibernate/SessionFactory</param-value>
</context-param>
<context-param>
<param-name>HIBERNATE_FILTER_REQUEST_ATTRIBUTE_ID</param-name>
<param-value>HibernateSession</param-value>
</context-param>
<context-param>
<param-name>HIBERNATE_FILTER_USE_PROXY</param-name>
<param-value>true</param-value>
</context-param>
<filter>
<filter-name>HibernateFilter</filter-name>
<filter-class>com.xe.hibernate.HibernateFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HibernateFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
As you can see, you have static method you can call.
Instead of doing HttpSession.getAttribute....
import the class above on your classes
and only call getSession(); to retrieve a "pure" hibernate session.
You have to understand that the session is not a cache, it's a business process cache.
If you want higher level cache, use EHCache for example, it's not complicated to use.
Don't forget, disconnect / close / commit ... the session depending what you're doing.