You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Lukáš Kolísko <lk...@gmail.com> on 2005/08/07 21:44:04 UTC
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: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: Hibernate Long term session
Posted by Lukáš Kolísko <lk...@gmail.com>.
Yes
access must be serialized or it will not work.
On 8/9/05, Patrick Casey <pa...@isdsd.com> wrote:
>
> That's why I'd suggest synchronizing so that all of a given
> session's requests get serialized. Otherwise, I dunno how to take a crack at
> this one sort of manually serializing each and every interaction with the
> Hibernate session which strikes me as rather error prone.
>
> Dunno; maybe somebody else has a better idea.
>
> --- Pat
>
> > -----Original Message-----
> > From: Pablo Ruggia [mailto:pruggia@gmail.com]
> > Sent: Monday, August 08, 2005 3:36 PM
> > To: Tapestry users
> > Subject: Re: Hibernate Long term session
> >
> > Having many sessions for one user won't work well with long session
> > pattern.
> > What session you connect and disconnect ?? You will be having
> > LazyInitializationExceptions everewhere.
> >
> > On 8/8/05, Lukáš Kolísko <lk...@gmail.com> wrote:
> > >
> > > Your solution is thread safe. I was thinking about ThreadLocal
> > > variable where you store session. If I understand the process all
> > > right it goes this way:
> > >
> > > 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();
> > >
> > > And here a copy of hibernate session is created for this thread. If
> > > other thread enters this method, another copy of session is created
> > > and these two sessions can be in different states.
> > >
> > > fThreadSession.set(s);
> > >
> > > }
> > >
> > > So each thread is working with his copy of the session assigned to
> > > user session. And these session can have different internal states.
> > >
> > > This should be true if I understand this text from Java documentation:
> > >
> > > Threadlocal
> > >
> > > This class provides thread-local variables. These variables differ
> > > from their normal counterparts in that each thread that accesses one
> > > (via its get or set method) has its own, independently initialized
> > > copy of the variable.
> > >
> > > Lucky:o)
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> > >
> > >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
Re: Hibernate Long term session
Posted by Lukáš Kolísko <lk...@gmail.com>.
Yes, you are completely right.
I just connect and disconnect jdbc connection from the session before
and after the request.
public static void reconnect(Session session) {
try {
session.reconnect();
threadSession.set(session);
} catch (HibernateException ex) {
throw new ScsException(ex);
}
}
public static Session disconnectSession(){
Session session = getSession();
try {
threadSession.set(null);
if (session.isConnected() && session.isOpen())
session.disconnect();
} catch (HibernateException ex) {
throw new ScsException(ex);
}
return session;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
RE: Hibernate Long term session
Posted by Patrick Casey <pa...@isdsd.com>.
That's why I'd suggest synchronizing so that all of a given
session's requests get serialized. Otherwise, I dunno how to take a crack at
this one sort of manually serializing each and every interaction with the
Hibernate session which strikes me as rather error prone.
Dunno; maybe somebody else has a better idea.
--- Pat
> -----Original Message-----
> From: Pablo Ruggia [mailto:pruggia@gmail.com]
> Sent: Monday, August 08, 2005 3:36 PM
> To: Tapestry users
> Subject: Re: Hibernate Long term session
>
> Having many sessions for one user won't work well with long session
> pattern.
> What session you connect and disconnect ?? You will be having
> LazyInitializationExceptions everewhere.
>
> On 8/8/05, Lukáš Kolísko <lk...@gmail.com> wrote:
> >
> > Your solution is thread safe. I was thinking about ThreadLocal
> > variable where you store session. If I understand the process all
> > right it goes this way:
> >
> > 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();
> >
> > And here a copy of hibernate session is created for this thread. If
> > other thread enters this method, another copy of session is created
> > and these two sessions can be in different states.
> >
> > fThreadSession.set(s);
> >
> > }
> >
> > So each thread is working with his copy of the session assigned to
> > user session. And these session can have different internal states.
> >
> > This should be true if I understand this text from Java documentation:
> >
> > Threadlocal
> >
> > This class provides thread-local variables. These variables differ
> > from their normal counterparts in that each thread that accesses one
> > (via its get or set method) has its own, independently initialized
> > copy of the variable.
> >
> > Lucky:o)
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> >
> >
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: Hibernate Long term session
Posted by Pablo Ruggia <pr...@gmail.com>.
Having many sessions for one user won't work well with long session pattern.
What session you connect and disconnect ?? You will be having
LazyInitializationExceptions everewhere.
On 8/8/05, Lukáš Kolísko <lk...@gmail.com> wrote:
>
> Your solution is thread safe. I was thinking about ThreadLocal
> variable where you store session. If I understand the process all
> right it goes this way:
>
> 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();
>
> And here a copy of hibernate session is created for this thread. If
> other thread enters this method, another copy of session is created
> and these two sessions can be in different states.
>
> fThreadSession.set(s);
>
> }
>
> So each thread is working with his copy of the session assigned to
> user session. And these session can have different internal states.
>
> This should be true if I understand this text from Java documentation:
>
> Threadlocal
>
> This class provides thread-local variables. These variables differ
> from their normal counterparts in that each thread that accesses one
> (via its get or set method) has its own, independently initialized
> copy of the variable.
>
> Lucky:o)
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
Re: Hibernate Long term session
Posted by Lukáš Kolísko <lk...@gmail.com>.
Your solution is thread safe. I was thinking about ThreadLocal
variable where you store session. If I understand the process all
right it goes this way:
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();
And here a copy of hibernate session is created for this thread. If
other thread enters this method, another copy of session is created
and these two sessions can be in different states.
fThreadSession.set(s);
}
So each thread is working with his copy of the session assigned to
user session. And these session can have different internal states.
This should be true if I understand this text from Java documentation:
Threadlocal
This class provides thread-local variables. These variables differ
from their normal counterparts in that each thread that accesses one
(via its get or set method) has its own, independently initialized
copy of the variable.
Lucky:o)
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
RE: Hibernate Long term session
Posted by Patrick Casey <pa...@isdsd.com>.
So far as I know, the process will go something like this:
User joins app.
HTTPSession is created in setup.
Hibernate Session is created in setup.
...
User transaction finishes
Hibernate session is disconnected.
Hibernate session is stashed in HTTPSession.
We're done.
User interacts.
HTTPSession is loaded.
Hibernate session is pulled out of HTTPSession, reconnected and
stashed in threadlocal.
...
User transaction finishes.
Hibernate session is disconnected.
Hibernate session is stashed in HTTPSession.
If you synchronize on the HTTPSession then only one thread at a time
can be working, so I think you're 100% thread safe as long as you don't
cluster.
Where did you think we were running into a situation where > 1
thread had access to the same Hibernate Session?
--- Pat
> -----Original Message-----
> From: Lukáš Kolísko [mailto:lkolisko@gmail.com]
> Sent: Monday, August 08, 2005 12:36 PM
> To: Tapestry users
> Subject: Re: Hibernate Long term session
>
> Thanks a lot for your help.
>
> I studied the first piece of code form Patrick Casey with HibSession
> and I personaly did it nearly the same way, but I think when you call
> attachSession method and the session is taken from user session map
> and stored to thread local varibale, so there is copy of the session
> for each thread ( this should be thread safe ). But I am not sure
> about backward replication to the session object in the user session.
> So I think that this would not work as long term session pattern,
> because there will be more versions for each thread of session for one
> user session. May be I am wrong.
>
> So I use serialization with semaphores to be sure that I still work on
> the same session. I use the code in my second message, without
> synchronized block ( because this would cause waiting of another
> threads connected with other user sessions ).
>
> And to Pablo Ruggia question:
>
> >Sorry about my ignorance on the subject, but will this work in an a
> >clustered enviroment ? You are synchornizing just in one server. So two
> >servers will be attending at same time the same user.
>
> The application should be deployed only on one server. So, I am not
> worring about clustering issues now.
>
> Thanks again for great advice.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: Hibernate Long term session
Posted by Lukáš Kolísko <lk...@gmail.com>.
Thanks a lot for your help.
I studied the first piece of code form Patrick Casey with HibSession
and I personaly did it nearly the same way, but I think when you call
attachSession method and the session is taken from user session map
and stored to thread local varibale, so there is copy of the session
for each thread ( this should be thread safe ). But I am not sure
about backward replication to the session object in the user session.
So I think that this would not work as long term session pattern,
because there will be more versions for each thread of session for one
user session. May be I am wrong.
So I use serialization with semaphores to be sure that I still work on
the same session. I use the code in my second message, without
synchronized block ( because this would cause waiting of another
threads connected with other user sessions ).
And to Pablo Ruggia question:
>Sorry about my ignorance on the subject, but will this work in an a
>clustered enviroment ? You are synchornizing just in one server. So two
>servers will be attending at same time the same user.
The application should be deployed only on one server. So, I am not
worring about clustering issues now.
Thanks again for great advice.
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: Hibernate Long term session
Posted by Pablo Ruggia <pr...@gmail.com>.
Sorry about my ignorance on the subject, but will this work in an a
clustered enviroment ? You are synchornizing just in one server. So two
servers will be attending at same time the same user.
On 8/7/05, Patrick Casey <pa...@isdsd.com> wrote:
>
>
> I dunno what the Gang of Four would suggest as the "best" approach,
> but I think your life is going to be a heck of a lot easier if you just do
> this:
>
>
> public class MyServlet extends ApplicationServlet {
>
> protected void doService(HttpServletRequest request,
> HttpServletResponse response) throws IOException,
> ServletException {
> HttpSession s = request.getSession();
> Synchronized(s) {
> super.doService(request, response);
> }
>
> }
>
> That way access to each session is serialized without you needing to
> set up any semaphores or whatnot.
>
> --- Pat
>
> > -----Original Message-----
> > From: Lukáš Kolísko [mailto:lkolisko@gmail.com]
> > Sent: Sunday, August 07, 2005 5:25 PM
> > To: Tapestry users
> > Subject: Re: Hibernate Long term session
> >
> > Thanks a lot.
> > So could this code be ok ?:
> >
> > private Semaphore semaphore;
> >
> > private static final String semaphoreName="semaphore";
> >
> > protected synchronized void setupSemaphore(RequestContext context){
> > HttpSession session = context.getSession();
> > if (session != null) {
> > semaphore = (Semaphore)
> > session.getAttribute(semaphoreName);
> > if (semaphore == null) {
> > semaphore = new Semaphore(1);
> > }
> > try {
> > semaphore.acquire();
> > session.setAttribute(semaphoreName,
> semaphore);
> > } catch (InterruptedException ex) {
> > throw new ApplicationRuntimeException(ex);
> > }
> > }
> > }
> >
> > protected void setupForRequest(RequestContext context) {
> > setupSemaphore(context);
> >
> > //Get hibernate long term session from user session....
> >
> > super.setupForRequest(context);
> > }
> >
> > protected void cleanupAfterRequest(IRequestCycle cycle) {
> > super.cleanupAfterRequest(cycle);
> > //Set hibernate long term session to user session ...
> > semaphore.release();
> > }
> > I am really not sure about these things.
> >
> > I need to prevent race conditions globaly and serialize requests from
> > user on his session.
> >
> > On 8/7/05, Pablo Ruggia <pr...@gmail.com> wrote:
> > > The answer is no, you can not make session threadsafe when a user
> > submits
> > > twice, there will be two threads accesing the same hibernate session,
> > wich
> > > is not threadsafe.
> > > But you can avoid users submitting twice, using Synchronizer Token
> > Pattern (
> > > http://www.javaworld.com/javaworld/javatips/jw-javatip136.html).
> > >
> > > On 8/7/05, Lukáš Kolísko <lk...@gmail.com> wrote:
> > > >
> > > > 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: tapestry-user-unsubscribe@jakarta.apache.org
> > > > For additional commands, e-mail:
> tapestry-user-help@jakarta.apache.org
> > > >
> > > >
> > >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
Re: Hibernate Long term session
Posted by Pablo Ruggia <pr...@gmail.com>.
To be honest, i don't know.
Definitivily it will be thread safe, but on one server. If you have a
cluster, and two different servers are attending two different requests for
the same user, you will be doing the same saves, updates, deletes at the
same time. Even I don't know how clusters behaive with two simultaneous
submissions from the same user, I've never configured one.
On 8/7/05, Lukáš Kolísko <lk...@gmail.com> wrote:
>
> Thanks a lot.
> So could this code be ok ?:
>
> private Semaphore semaphore;
>
> private static final String semaphoreName="semaphore";
>
> protected synchronized void setupSemaphore(RequestContext context){
> HttpSession session = context.getSession();
> if (session != null) {
> semaphore = (Semaphore) session.getAttribute(semaphoreName);
> if (semaphore == null) {
> semaphore = new Semaphore(1);
> }
> try {
> semaphore.acquire();
> session.setAttribute(semaphoreName, semaphore);
> } catch (InterruptedException ex) {
> throw new ApplicationRuntimeException(ex);
> }
> }
> }
>
> protected void setupForRequest(RequestContext context) {
> setupSemaphore(context);
>
> //Get hibernate long term session from user session....
>
> super.setupForRequest(context);
> }
>
> protected void cleanupAfterRequest(IRequestCycle cycle) {
> super.cleanupAfterRequest(cycle);
> //Set hibernate long term session to user session ...
> semaphore.release();
> }
> I am really not sure about these things.
>
> I need to prevent race conditions globaly and serialize requests from
> user on his session.
>
> On 8/7/05, Pablo Ruggia <pr...@gmail.com> wrote:
> > The answer is no, you can not make session threadsafe when a user
> submits
> > twice, there will be two threads accesing the same hibernate session,
> wich
> > is not threadsafe.
> > But you can avoid users submitting twice, using Synchronizer Token
> Pattern (
> > http://www.javaworld.com/javaworld/javatips/jw-javatip136.html).
> >
> > On 8/7/05, Lukáš Kolísko <lk...@gmail.com> wrote:
> > >
> > > 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: tapestry-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> > >
> > >
> >
>
RE: Hibernate Long term session
Posted by Patrick Casey <pa...@isdsd.com>.
I dunno what the Gang of Four would suggest as the "best" approach,
but I think your life is going to be a heck of a lot easier if you just do
this:
public class MyServlet extends ApplicationServlet {
protected void doService(HttpServletRequest request,
HttpServletResponse response) throws IOException,
ServletException {
HttpSession s = request.getSession();
Synchronized(s) {
super.doService(request, response);
}
}
That way access to each session is serialized without you needing to
set up any semaphores or whatnot.
--- Pat
> -----Original Message-----
> From: Lukáš Kolísko [mailto:lkolisko@gmail.com]
> Sent: Sunday, August 07, 2005 5:25 PM
> To: Tapestry users
> Subject: Re: Hibernate Long term session
>
> Thanks a lot.
> So could this code be ok ?:
>
> private Semaphore semaphore;
>
> private static final String semaphoreName="semaphore";
>
> protected synchronized void setupSemaphore(RequestContext context){
> HttpSession session = context.getSession();
> if (session != null) {
> semaphore = (Semaphore)
> session.getAttribute(semaphoreName);
> if (semaphore == null) {
> semaphore = new Semaphore(1);
> }
> try {
> semaphore.acquire();
> session.setAttribute(semaphoreName,
semaphore);
> } catch (InterruptedException ex) {
> throw new ApplicationRuntimeException(ex);
> }
> }
> }
>
> protected void setupForRequest(RequestContext context) {
> setupSemaphore(context);
>
> //Get hibernate long term session from user session....
>
> super.setupForRequest(context);
> }
>
> protected void cleanupAfterRequest(IRequestCycle cycle) {
> super.cleanupAfterRequest(cycle);
> //Set hibernate long term session to user session ...
> semaphore.release();
> }
> I am really not sure about these things.
>
> I need to prevent race conditions globaly and serialize requests from
> user on his session.
>
> On 8/7/05, Pablo Ruggia <pr...@gmail.com> wrote:
> > The answer is no, you can not make session threadsafe when a user
> submits
> > twice, there will be two threads accesing the same hibernate session,
> wich
> > is not threadsafe.
> > But you can avoid users submitting twice, using Synchronizer Token
> Pattern (
> > http://www.javaworld.com/javaworld/javatips/jw-javatip136.html).
> >
> > On 8/7/05, Lukáš Kolísko <lk...@gmail.com> wrote:
> > >
> > > 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: tapestry-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> > >
> > >
> >
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: Hibernate Long term session
Posted by Lukáš Kolísko <lk...@gmail.com>.
Thanks a lot.
So could this code be ok ?:
private Semaphore semaphore;
private static final String semaphoreName="semaphore";
protected synchronized void setupSemaphore(RequestContext context){
HttpSession session = context.getSession();
if (session != null) {
semaphore = (Semaphore) session.getAttribute(semaphoreName);
if (semaphore == null) {
semaphore = new Semaphore(1);
}
try {
semaphore.acquire();
session.setAttribute(semaphoreName, semaphore);
} catch (InterruptedException ex) {
throw new ApplicationRuntimeException(ex);
}
}
}
protected void setupForRequest(RequestContext context) {
setupSemaphore(context);
//Get hibernate long term session from user session....
super.setupForRequest(context);
}
protected void cleanupAfterRequest(IRequestCycle cycle) {
super.cleanupAfterRequest(cycle);
//Set hibernate long term session to user session ...
semaphore.release();
}
I am really not sure about these things.
I need to prevent race conditions globaly and serialize requests from
user on his session.
On 8/7/05, Pablo Ruggia <pr...@gmail.com> wrote:
> The answer is no, you can not make session threadsafe when a user submits
> twice, there will be two threads accesing the same hibernate session, wich
> is not threadsafe.
> But you can avoid users submitting twice, using Synchronizer Token Pattern (
> http://www.javaworld.com/javaworld/javatips/jw-javatip136.html).
>
> On 8/7/05, Lukáš Kolísko <lk...@gmail.com> wrote:
> >
> > 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: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> >
> >
>
Re: Hibernate Long term session
Posted by Pablo Ruggia <pr...@gmail.com>.
The answer is no, you can not make session threadsafe when a user submits
twice, there will be two threads accesing the same hibernate session, wich
is not threadsafe.
But you can avoid users submitting twice, using Synchronizer Token Pattern (
http://www.javaworld.com/javaworld/javatips/jw-javatip136.html).
On 8/7/05, Lukáš Kolísko <lk...@gmail.com> wrote:
>
> 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: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
RE: Hibernate Long term session
Posted by Patrick Casey <pa...@isdsd.com>.
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:lkolisko@gmail.com]
> 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: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org