Close Session in Filter

xiaoxiao2021-03-06  35

Package COM.

Upstate.

Cellculture.

Om.

Persist;

Import Net.

sf.

Hibernate.

HibernateException;

Import Net.

sf.

Hibernate.

JDBCEXCEPTION;

Import Net.

sf.

Hibernate.

SESSION;

Import Net.

sf.

Hibernate.

SessionFactory;

Import org.

Apache.

COMMONS.

Logging.

Log;

Import org.

Apache.

COMMONS.

Logging.

Logfactory;

/ ** * this class is used to get hibernate sessions and may * also contain methods (in the fulure) 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 ServiceLocator

);

e.

PrintStackTrace

(

);

}

}

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

Private serviceLocator

(

)

Throws HibernateException, JDBCEXCEPTION

{

}

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

Static session currentsssion

(

)

Throws PersistenceException

{

Session s =

(Session

Sense.

get

(

);

IF

(s ==

NULL

)

{

s = persistencemanager.

OpenSession

(

);

IF

LOG.

ISDebugene

(

)

)

{

Log.

Debug

"" Opened Hibernate Session. "

);

}

SESSION.

set

(s)

);

}

Return S;

}

public

Static

Void CloseSession

(

)

Throws HibernateException, JDBCEXCEPTION

{

Session s =

(Session

Sense.

get

(

);

SESSION.

set

(

NULL

);

IF

(s! =

NULL

)

{

IF

(s.

Isopen

(

)

)

{

S.

Flush

(

);

S.

Close

(

);

IF

LOG.

ISDebugene

(

)

)

{

Log.

Debug

"" Closed Hibernate session. "

);

}

}

}

Else

{

Log.

Warn

"" Hibernate session was inadvertly already closed. "

);

}

}

}

If you use Lazy Loading, you need to use a servlet filter

Java code:

Package COM.

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.

Om.

Persist.

ServiceLocator;

public

Class ActionFilter

IMPLEMENTS FILTER

{

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

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

/ ** * The log instance for this class * /

PRIVATE log log = logfactory.

Getlog

ActionFilter.

Class

);

Private filterConfig filter =

NULL;

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

public

Voidin

(FilterConfig Filterconfig)

)

Throws servletexception

{

THIS.

FilterConfig = filter;

}

/ ** * 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 Request = (httpservletRequest

Req;

HttpservletResponse response =

(Httpservletresponse

) response;

HttpSession session = request.

GetSession

(

True

);

Session SES =

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 PersistenceException

{

Try

{

Return ServiceLocator.

CurrentSession

(

);

}

Catch

(

Exception E

)

{

Throw

New PersistenceException

("Could Not Find Current Hibernate Session.", E

);

}

}

}

In addition, in order to set up the database, we will use some Manager classes, that is, DAO

Java code:

/ * * CREATED ON APR 28, 2003 * * /

Package COM.

Upstate.

Cellculture.

Om.

Persist;

Import Java.

Util.

List;

Import Net.

sf.

Hibernate.

Query;

Import Net.

sf.

Hibernate.

SESSION;

Import COM.

Upstate.

Cellculture.

Om.

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 PersistenceException

{

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 TechnicianManager interface and a TechnicianManagerImpl Class. It is better. Pack all thrown exceptions via an exception defined.

Java code:

Package COM.

Upstate.

Cellculture.

Om.

Persist;

Import org.

Apache.

COMMONS.

LANG.

EXCEPTION.

NestableException;

/ ** * a general persistenceException That than thrown by all all ganager classes. * /

public

Class PersistenceException

Extends NestableException

{

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

/ ** * constructor for personistenceException. * /

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-67216.html

New Post(0)