These two classes should do the trick.
package data;
import java.lang.reflect.Proxy;
import javax.servlet.http.HttpSession;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import servlet.MyServlet;
import utils.Log;
import audit.AuditInterceptor;
public class HibHelper {
private static final Configuration fConfiguration;
private static final SessionFactory fSessionFactory;
private static final ThreadLocal fThreadSession = new ThreadLocal();
private static final ThreadLocal fThreadTransaction = new
ThreadLocal();
private static final String SESSION_KEY = "HIBERNATE_SESSION";
// private static Session fDebug;
static {
fConfiguration = new Configuration().configure();
fSessionFactory = fConfiguration.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return fSessionFactory;
}
/*
* private static Session getSessionFromServlet() { HttpSession hs =
* MyServlet.getCurrentSession();
*
* Object o = hs.getAttribute(SESSION_KEY); if (o == null) return
null;
* Session rc = (Session) o; return rc; }
*/
private static Session getSessionFromStorage() {
return (Session) fThreadSession.get();
}
private static Session createNewSession() {
AuditInterceptor intercept = new AuditInterceptor();
Session s = fSessionFactory.openSession(intercept);
intercept.setSession(s);
s = (Session)
(Proxy.newProxyInstance(Session.class.getClassLoader(),
new Class[] { Session.class }, new
CorinnaSession(s)));
return s;
}
public static Session getSession() {
// Session s = fDebug;
Session s = (Session) getSessionFromStorage();
try {
if (s == null) {
// s = fSessionFactory.openSession();
s = createNewSession();
fThreadSession.set(s);
}
} catch (HibernateException ex) {
ex.printStackTrace();
}
return s;
}
public static void cleanupSession() {
Session s = getSessionFromStorage();
try {
if (s != null) {
commitTransaction();
if (s.isConnected())
s.disconnect();
}
} catch (Exception e) {
Log.error(e);
}
}
public static void closeSession() {
try {
Session s = getSessionFromStorage();
MyServlet.getCurrentSession().setAttribute(SESSION_KEY, null);
if (s != null && s.isOpen()) {
s.close();
}
} catch (HibernateException ex) {
ex.printStackTrace();
}
}
public static void beginTransaction() {
Transaction tx = (Transaction) fThreadTransaction.get();
try {
if (tx == null) {
tx = getSession().beginTransaction();
fThreadTransaction.set(tx);
}
} catch (HibernateException ex) {
ex.printStackTrace();
}
}
public static void commitTransaction() {
Transaction tx = (Transaction) fThreadTransaction.get();
try {
if (tx != null && !tx.wasCommitted() &&
!tx.wasRolledBack()) {
tx.commit();
fThreadTransaction.set(null);
}
} catch (HibernateException ex) {
rollbackTransaction();
ex.printStackTrace();
}
}
public static void rollbackTransaction() {
Transaction tx = (Transaction) fThreadTransaction.get();
try {
fThreadTransaction.set(null);
if (tx != null && !tx.wasCommitted() &&
!tx.wasRolledBack()) {
tx.rollback();
}
} catch (HibernateException ex) {
rollbackTransaction();
ex.printStackTrace();
} finally {
closeSession();
}
}
public static Configuration getConfiguration() {
return fConfiguration;
}
public static void attachSession(HttpSession hs) {
Session s = (Session)
MyServlet.getCurrentSession().getAttribute(
SESSION_KEY);
if (s == null) {
s = createNewSession();
MyServlet.getCurrentSession().setAttribute(SESSION_KEY, s);
}
if (!s.isConnected())
s.reconnect();
fThreadSession.set(s);
}
}
package presentation;
import javax.servlet.http.HttpSession;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.engine.BaseEngine;
import org.apache.tapestry.request.RequestContext;
import servlet.MyServlet;
import data.HibHelper;
public class CorinnaEngine extends BaseEngine {
private static final long serialVersionUID = 3257284742721648952L;
protected void cleanupAfterRequest(IRequestCycle cycle) {
HibHelper.cleanupSession();
super.cleanupAfterRequest(cycle);
}
protected void setupForRequest(RequestContext context) {
HttpSession hs = MyServlet.getCurrentSession();
HibHelper.attachSession(hs);
HibHelper.getSession();
super.setupForRequest(context);
}
}
> -----Original Message-----
> From: Lukáš Kolísko [mailto:[EMAIL PROTECTED]
> Sent: Sunday, August 07, 2005 12:44 PM
> To: Tapestry users
> Subject: Hibernate Long term session
>
> Hello,
> maybe this is a newbie question, but is there a possible pattern how
> to implement long term session in Tapestry application in thread safe
> way.
>
> I am using tapestry ver. 3.0.3. I open session using ThreadLocal
> object from a static method when it is needed (The pattern show in
> Hibernate in Action book) and I close session using detach method of
> each page. This pattern works good for me, but when I want to use some
> object across many pages this pattern of course does not work with
> lazy initialization. I have to reconnect object to hibernate session
> or load a part of object tree into memory with lazy=false attributes (
> but this is very memory consuming )
>
> I am not sure if it is possible to use setupForRequest and
> cleanupAfterRequest methods to store hibernate session to user session
> and restore it after next request comes and connect it back to jdbc. I
> think this is not thread safe when user for example clicks more times
> submit button before the first request is completed. In this case more
> same hibernate session object would be taken from user session but
> after each request end the persistent session will be overwritten in
> non consistent way and hibernate session object is not thread safe
> itself.
>
> I would be very grateful for any suggestion or response.
> I solve this problem in my dissertation application.
>
> Sorry for my english.
>
> Lukas Kolisko
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]