Close Session in Filter

xiaoxiao2021-03-06  41

Package com.upstate.cellculture.com.Persist;

Import net.sf.hibernate.hibernateException;

Import Net.sf.hibEnate.jdbcexception;

Import net.sf.hibernate.Session;

Import Net.sf.hibActory;

Import org.apache.commons.logging.log;

Import org.apache.commons.logging.logfactory;

/ **

* This class is buy to get hibernate sessions and may

* Also Contain Methods (In The Future) To Get Dbconnections

* or transactions from JNDI.

* /

Public Class ServiceLocator

{

// ~ static fields / initializers =========================================== ==

Public final static string session_factory = "hibernate / sessionFactory";

PUBLIC Static Final Threadlocal session = new threadlocal ();

Private static sessionFactory sf = null;

Private static serviceLocator me;

Private static log log = logfactory.getlog (serviceLocator.class);

STATIC {

Try

{

ME = new serviceLocator ();

}

Catch (Exception E)

{

Log.fatal ("Error Occurred Initializing Service);

E.PrintStackTrace ();

}

}

// ~ constructors =============================================== =============

Private ServiceLocator () THROWS HibernateException, JDBCEXCEPTION

{}

/ / ~ Methods ============================================== ================== Public static session currentations () throws persistenceException

{

Session s = (session) session.get ();

IF (s == NULL)

{

S = persistenceManager.opensession ();

IF (log.Indebugenabled ())

{

Log.debug ("Opened Hibernate Session.");

}

Session.set (s);

}

Return S;

}

Public static void closesession () THROWS HibernateException, JDBCEXCEPTION

{

Session s = (session) session.get ();

session.set (null);

IF (s! = null)

{

IF (s.isopen ())

{

S.flush ();

s.close ();

IF (log.Indebugenabled ())

{

Log.debug ("Closed Hibernate Session.");

}

}

}

Else

{

"" Hibernate Session Was INADVERTENTLY Already Closed. ");

}

}

}

If you use Lazy Loading, you need to use a servlet filterpage.upstate.cellculture.filter;

Import java.io.ioException;

Import javax.servlet.filter;

Import javax.servlet.filterchain;

Import javax.servlet.filterconfig;

Import javax.servlet.servletException;

Import javax.servlet.servletRequest;

Import javax.servlet.servletResponse;

Import javax.servlet.http.httpservletRequest;

Import javax.servlet.http.httpservletResponse;

Import javax.servlet.http.httpsession;

Import net.sf.hibernate.Session;

Import org.apache.commons.logging.log;

Import org.apache.commons.logging.logfactory;

Import com.upstate.cellculture.om.Persist.PersistenceException;

Import com.upstate.cellculture.com.Persist.serviceLocator;

Public class actionfilter imports filter {

// ~ static fields / initializers =========================================== ==

// ~ instance fields ================================================================================================================================================================================================ ======

/ **

* The log Instance for this class

* /

Private log log = logfactory.getlog (actionfilter.class);

Private filterconfig filterfig = null;

/ / ~ Methods ============================================== ==================

Public void init (FilterConfig FilterConfig) THROWS ServletException

{

THIS.FILTERCONFIG = FilterConfig;

}

/ **

* DESTROYS the filter.

* /

Public void destroy ()

{

FilterConfig = NULL;

}

Public void Dofilter (ServletRequest Req, ServletResponse Resp, Filterchain Chain)

THROWS IOEXCEPTION, ServletException

{

// Cast to the Types I want to us

HTTPSERVLETREQUEST REQEST = (httpservletRequest) Req;

HTTPSERVLETRESPONSE RESPONSE = (HTTPSERVLETRESPONSE) RESP;

HttpSession session = request.getations (TRUE);

Session sees = null;

Boolean sessioncreated = false;

Try

{

Chain.dofilter (Request, Response);

}

Finally

{

Try

{

ServiceLocator.closeSession ();

}

Catch (Exception EXC)

{

Log.Error ("Error Closing Hibernate Session.", EXC);

Exc.printStackTrace ();

}

}

}

Public static session getsession () throws personistenceException

{

Try

{

Return ServiceLocator.currentSession ();

}

Catch (Exception E)

{

Throw New PersistenceException ("Could Not Find Current Hibernate Session.", E);

}

}

}

In addition, in order to concentrate the database access, we will use some Manager classes, that is, DAO / *

* Created ON Apr 28, 2003

*

* /

Package com.upstate.cellculture.com.Persist;

Import java.util.list;

Import net.sf.hibernate.query;

Import net.sf.hibernate.Session;

Import com.upstate.cellculture.com.technician;

/ **

* @Author TMCKINNEY

*

* Centralizes All Access to the Technicians Table

* /

Public Class TechnicianManager

{

Private static list alltechnicians;

Public Static Void Save (Technician Technician) Throws PersistenceException

{

Try

{

ServiceLocator.currentSession (). Save (technician);

}

Catch (Exception E)

{

Throw New PersistenceException ("Could Not Save.", E);

}

}

Public Static Technician RetrievebyPk (Long Technicianid) Throws PersistenceException

{

Try

{

Technician Technician = (Technician) ServiceLocator.currentSession ().

LOAD (Technician.class, New Long (TechnicianID));

Return techni;

}

Catch (Exception E)

{

Throw New PersistenceException ("COULD NOT RETRIEVE.", E);

}

}

Public Static List RetrieveAlltechnicians () THROWS PERSISISTENCEEXCEPTION

{

IF (alltechnicians == null)

{

Try

{

Query Q = ServiceLocator.currentSession (). CreateQuery ("from Technician In Class"

Technician.class "ORDER BY UPPER (Technician.name)");

Alltechnicians = Q.List ();

Session.flush ();

Catch (Exception E)

{

E.PrintStackTrace ();

Throw New PersistenceException (e);

}

}

Return Alltechnicians;

}

}

If you can use a Technician Manager interface and a TechnicianManagerImpl Class. It is better. Pack all thrown exceptions via an exception defined. Package com.upstate.cellculture.com.Persist;

Import org.apache.commons.lang.exception.nestableException;

/ **

* A General PersistenceException That Is Thrown by all all all manager classes.

*

* /

Public Class PersistenceException Extends NestableException

{

// ~ constructors =============================================== =============

/ **

* Constructor for PersistenceException.

* /

Public persistenceException ()

{

Super ();

}

/ **

* Constructor for PersistenceException.

*

* @Param Message

* /

Public persistenceException (String Message)

{

SUPER (Message);

}

/ **

* Constructor for PersistenceException.

*

* @Param Message

* @Param Cause

* /

Public PersistenceException (String Message, Throwable Cause)

{

SUPER (Message, Cause);

}

/ **

* Constructor for PersistenceException.

*

* @Param Cause

* /

Public PersistenceException (throwable cause)

{

Super (Cause);

}

}

转载请注明原文地址:https://www.9cbs.com/read-66538.html

New Post(0)