-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: sessionFactory.getCurrentSession() ?
PostPosted: Thu May 26, 2005 1:36 pm 
Regular
Regular

Joined: Tue Jan 11, 2005 8:55 pm
Posts: 53
Hi,

I'm using Hibernate v3.0.5, and I'm receiving the following error:

org.hibernate.HibernateException: Unable to locate current JTA transaction

I get this error when I call:

Code:
getSessionFactory().getCurrentSession();


My hibernate config file has the following parameters set:

Code:
       <!-- Session Management Properties -->
          <property name="transaction.auto_close_session">true</property>
          
          <property name="transaction.flush_before_completion">true</property>


I haven't received this error before. The recent change I have made is by adding a a second-level cache, treecache, but I'm not sure how adding the cache will cause this exception to occur. Are they related? Is it a bug? No clue.

Please help, thanks.


Top
 Profile  
 
 Post subject: Re: sessionFactory.getCurrentSession() ?
PostPosted: Thu May 26, 2005 1:57 pm 
Regular
Regular

Joined: Tue Jan 11, 2005 8:55 pm
Posts: 53
Nevermind, I'm dumb (but relieved!). I figured it out.


Top
 Profile  
 
 Post subject: Re: sessionFactory.getCurrentSession() ?
PostPosted: Tue Jun 14, 2005 12:16 pm 
Beginner
Beginner

Joined: Tue Jun 14, 2005 12:14 pm
Posts: 37
hamids wrote:
Nevermind, I'm dumb (but relieved!). I figured it out.

Obviously, I am a little bit thicker ;)... I got the same error. What is the problem? Thanks.


Top
 Profile  
 
 Post subject: Re: sessionFactory.getCurrentSession() ?
PostPosted: Wed Jun 15, 2005 11:26 am 
Regular
Regular

Joined: Tue Jan 11, 2005 8:55 pm
Posts: 53
liem wrote:
hamids wrote:
Nevermind, I'm dumb (but relieved!). I figured it out.

Obviously, I am a little bit thicker ;)... I got the same error. What is the problem? Thanks.


Are you using Hibernate in a managed environment; i.e., with an EJB ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 15, 2005 12:09 pm 
Beginner
Beginner

Joined: Tue Jun 14, 2005 12:14 pm
Posts: 37
Well, even though I am running on JBoss 3.2.5 (yeah, old), but I am accessing Hibernate from both MBeans and EJB. EJB works fine (well, sort of, reason later), but MBeans obviously don't run in the context of a JTA (is there a way to make them? I am new to MBeans).

So, currently, I am resorting to the Session-in-ThreadLocal pattern to handle my sessions, rather than using getCurrentSession(). We don't have any inter-bean transaction, so I think this should be fine.

One question, though, I am running into the problem of my EJB not rollingback as expected. For example:

ejb1Method {
save();
query();
update();
}

I declare the above method as using CMT and "RequiresNew". But, if query() fails (and throws an uncaught RuntimeException to the container), save() still succeeds and the data makes its way into the database. I expect the save() to be rolled back and nothing gets saved into the database unless the entire ejb1Method() suceeds?!? BTW, I am using the "FlushBeforeCompletionEnabled" and "AutoCloseSessionEnabled" set to true.

Anyone ran into this CMT not rolling back correctly problem? Hibernate used is 3.0.4.

Thanks in advance,
Liem


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 15, 2005 12:21 pm 
Regular
Regular

Joined: Tue Jan 11, 2005 8:55 pm
Posts: 53
liem wrote:
Well, even though I am running on JBoss 3.2.5 (yeah, old), but I am accessing Hibernate from both MBeans and EJB. EJB works fine (well, sort of, reason later), but MBeans obviously don't run in the context of a JTA (is there a way to make them? I am new to MBeans).

So, currently, I am resorting to the Session-in-ThreadLocal pattern to handle my sessions, rather than using getCurrentSession(). We don't have any inter-bean transaction, so I think this should be fine.

One question, though, I am running into the problem of my EJB not rollingback as expected. For example:

ejb1Method {
save();
query();
update();
}

I declare the above method as using CMT and "RequiresNew". But, if query() fails (and throws an uncaught RuntimeException to the container), save() still succeeds and the data makes its way into the database. I expect the save() to be rolled back and nothing gets saved into the database unless the entire ejb1Method() suceeds?!? BTW, I am using the "FlushBeforeCompletionEnabled" and "AutoCloseSessionEnabled" set to true.

Anyone ran into this CMT not rolling back correctly problem? Hibernate used is 3.0.4.

Thanks in advance,
Liem


So, from my understanding, the 'FlushBeforeCompletionEnabled' and 'AutoCloseSessionEnabled' attributes are specifically for the getCurrentSession() method, I don't think it works with the threadlocal pattern.

And in the EJB annotations, I specified the transaction type as "Required". It should work ..

Are you specifying the transaction manager and transaction manager lookup class in your hibernate.cfg.xml ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 15, 2005 12:25 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
So, from my understanding, the 'FlushBeforeCompletionEnabled' and 'AutoCloseSessionEnabled' attributes are specifically for the getCurrentSession() method, I don't think it works with the threadlocal pattern.


Of course not, both are just not needed if you use getCurrentSession() and not your own ThreadLocal. However, ThreadLocal's are of dubious value in a multi-threaded application server. This is all explained in the reference documentation, which nobody wants to read anymore these days.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 9:15 am 
Newbie

Joined: Tue Aug 24, 2004 5:36 pm
Posts: 16
little error in ref 4.8.3 : managend environment


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 12:58 pm 
Beginner
Beginner

Joined: Tue Jun 14, 2005 12:14 pm
Posts: 37
OK... So, now I have 2 ways to get to my Session, so that it works both for CMT and BMT/non-managed environments. For CMT, I use getCurrentSession(); for BMT/non-managed, I use my own Session management scheme (aka, the ThreadLocal session). Everything, including rollback, works like a charm! Thanks everyone for your help! You guys are awesome.

But, now here is another quandary: How can I tell if I get called from a CMT versus non-managed environment, to know if I should call openSession() or getCurrentSession()? I am trying to abstract the concept of Session from client code.

Thanks,
Liem


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 17, 2005 2:18 am 
Newbie

Joined: Fri Jun 17, 2005 2:07 am
Posts: 19
Location: Berne, Switzerland
christian wrote:
Quote:
So, from my understanding, the 'FlushBeforeCompletionEnabled' and 'AutoCloseSessionEnabled' attributes are specifically for the getCurrentSession() method, I don't think it works with the threadlocal pattern.


Of course not, both are just not needed if you use getCurrentSession() and not your own ThreadLocal. However, ThreadLocal's are of dubious value in a multi-threaded application server. This is all explained in the reference documentation, which nobody wants to read anymore these days.


i'm using hibernate in a managed environment with weblogic. getCurrentSession() works when i use transaction typ "Required", "Required New". but i get the above mentioned exception as soon as i use transaction typ "Supports", "Not Supported".

the javadoc of getCurrentSession() states that a transaction must already be in effect prior to calling this method. imo this means i can't use "Supports" with getCurrentSession(). what are the alternatives in a managed environment ?

greetings from berne
jakob


Top
 Profile  
 
 Post subject: Can you post your code Liem
PostPosted: Fri Jun 17, 2005 10:21 am 
Beginner
Beginner

Joined: Mon Apr 18, 2005 10:25 am
Posts: 38
Location: Maryland
I would like to see how you got your session in both the managed and non-managed ways?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 17, 2005 11:00 am 
Beginner
Beginner

Joined: Tue Jun 14, 2005 12:14 pm
Posts: 37
The workaround I did is basically check to see if I am in a JTA transaction before invoking getCurrentSession(). If I am not in a JTA transaction, then I use openSession() instead (via the ThreadLocal pattern). I wrap all this in a convenient method, so clients get a session the same way, disregard of where they come from.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 19, 2005 6:11 am 
Newbie

Joined: Fri Jun 17, 2005 2:07 am
Posts: 19
Location: Berne, Switzerland
liem wrote:
The workaround I did is basically check to see if I am in a JTA transaction before invoking getCurrentSession(). If I am not in a JTA transaction, then I use openSession() instead (via the ThreadLocal pattern). I wrap all this in a convenient method, so clients get a session the same way, disregard of where they come from.


we're using a similar solution but first we try getCurrentSession() and in case of an exception openSession() and a ThreadLocal is used.

do you explicitely close the session ?

jakob


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 20, 2005 1:18 pm 
Beginner
Beginner

Joined: Tue Jun 14, 2005 12:14 pm
Posts: 37
brj wrote:

we're using a similar solution but first we try getCurrentSession() and in case of an exception openSession() and a ThreadLocal is used.

do you explicitely close the session ?

jakob

Yes, I explicitly close the session. It is usually better to check for the exception condition than to catch it, because you will incur exception stack trace cost. My 2 cents.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 20, 2005 3:48 pm 
Newbie

Joined: Fri Jun 17, 2005 2:07 am
Posts: 19
Location: Berne, Switzerland
liem wrote:
brj wrote:

we're using a similar solution but first we try getCurrentSession() and in case of an exception openSession() and a ThreadLocal is used.

do you explicitely close the session ?

jakob

Yes, I explicitly close the session. It is usually better to check for the exception condition than to catch it, because you will incur exception stack trace cost. My 2 cents.


thanks for your answer. how do you check the current jta-transaction ?

i was thinking of changing the implemenation of SessionFactoryImpl, because imo hibernate should create a session if it cannot obtain the current one. my implementation creates and returns a SessionImpl with the same settings as in getCurrentSession(). so i think (hope...) that callers of SessionFactoryImpl#getCurrentSession should not need to care where the session is coming from.

jakob


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.