-->
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: EJB BMT and Hibernate...
PostPosted: Wed Sep 03, 2003 9:37 am 
Newbie

Joined: Wed Sep 03, 2003 9:08 am
Posts: 18
Hi,

In our project we are using a mix of both CMT and BMT stateless session beans. Previously we were using entity beans for persisting the data and now I've removed all the entity beans from the system.

For intergrating the hibernate with the app server(JBoss) I'm using the "HibernateSession" class provided by steve (http://forum.hibernate.org/viewtopic.php?p=504). It is working perfectly for all the Container Managed Transaction beans.

However, for the BMT beans, when I use the "HibernateSession", during closing the session an error is thrown saying

Code:
java.sql.SQLException: Connection handle is not currently associated with a  ManagedConnection
        at org.jboss.resource.adapter.jdbc.local.LocalConnection.checkStatus(LocalConnection.java:774)
        at org.jboss.resource.adapter.jdbc.local.LocalConnection.getWarnings(LocalConnection.java:573)
        at net.sf.hibernate.impl.SessionFactoryImpl.closeConnection(SessionFactoryImpl.java:321)
        at net.sf.hibernate.impl.SessionImpl.disconnect(SessionImpl.java:3159)
        at net.sf.hibernate.impl.SessionImpl.close(SessionImpl.java:471)
...


Here's the piece of the code that manages the transactions in EJB for BMT case.

Code:

try
{
    UserTransaction ut = sessionCtx.getUserTransaction();
    ut.begin();
    Session session = HibernateSession.currentSession();

     //...Use the session here to perform the operations

      ut.commit();
}
catch(Exception e)
{
       ut.rollback();
}
finally
{
  HibernateSession.closeSession();
}




I have also tried to change the above code something like

Code:
     Transaction tx = HibernateSession.currentSession.beginTransaction();

      //....Actual code

      tx.commit();



In that case also I'm getting the same exception.

The transaction attribute for the methods are in the "Supports" level. (I have also changed this to "Required" level, but no use).

Can anybody please explain me how to use hibernate with BMT session beans?

I'm using JBoss 3.0.3 and Hibernate 2.1 r2.

Regards,
Balakrishnan.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 10:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
That version of thread local session is specifically meant for use in CMT session beans.

Quote:
The transaction attribute for the methods are in the "Supports" level. (I have also changed this to "Required" level, but no use).

Can anybody please explain me how to use hibernate with BMT session beans?

I don't understand... The first part of that quote is describing CMT sematics. BMT beans cannot have transaction attributes set, as it is the job of the bean itself, not the container, to maintain transactions. Can you clarify what you mean here?

I never use BMT, as I don't see the point (but thats just me). But if you plan on continued use of BMT, then I'd say to close the session prior to commiting the transaction. You would probably even want to come up with another version of HibernateSession to use in those situations just because the sematics are that much different at the start and end.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 3:17 am 
Newbie

Joined: Wed Sep 03, 2003 9:08 am
Posts: 18
Hi,

Sorry for the confusion. You are right. We dont need the transaction attribute related code for BMT's. Now I commented out that code.

I tried to close the session before commiting as you advised. In that case also, I got the same exception. I've also checked for the correct jta.UserTransaction property in the configuration(Set as "UserTransaction" since I'm using JBoss).

I have a doubt. Irrespective of whether we are using CMT or BMT will the call sessionfactory.openSession() correctly associate the session with the underlying transaction?

ie., if we write code like this...

Code:

UserTransaction ut = sessionCtx.getTransaction();
ut.begin();
Session session = HibernateSession.currentSession();

//EJB code goes here...

session.close();
ut.commit();


Will the above code work?

To make sure that the EJB code is working properly, I manually gave a connection(got separately) while opening the session and used the session transaction for controlling the code and it worked perfectly.

Code:

Session session = sessionFactory.openSession(conn);
Transaction tx = session.beginTransaction();

//EJB code goes here.

tx.commit();
session.close();



Can you please explain me how to use UserTransaction with Hibernate?

Regards,
Balakrishnan.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 3:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You need to make certain that the datasource that Hibernate is obtaining connections from is a JTA datasource.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 4:09 am 
Newbie

Joined: Wed Sep 03, 2003 9:08 am
Posts: 18
Sorry for this dumb question. How do I verify this? Currently I've configured the JBoss connection pool to use with the hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 6:34 am 
Newbie

Joined: Wed Sep 03, 2003 9:08 am
Posts: 18
To add on this, I've changed the connection pool from JBoss to DBCP connection pool. Now I'm not getting the error. But now the entries are not updated to the database. Also no error is shown by hibernate.

Here's the code I'm using.

Code:

try
{
    UserTransaction ut = sessionCtx.getUserTransaction();
    ut.begin();
    Session session = HibernateSession.currentSession();

     //...Use the session here to perform the operations

      ut.commit();
}
catch(Exception e)
{
       ut.rollback();
}
finally
{
  HibernateSession.closeSession();
}




Also, I've changed the UserTransaction to something like this...

Code:

Transaction tx = HibernateSession.currentSession().beginTransaction();

//...EJB code

tx.commit();
HibernateSession.close();


Here also I'm not getting any errors but no changes are updated to DB.

Can anybody please help me how to proceed on this.

Regards,
Balakrishnan.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 8:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Be sure to flush the session prior to committing transactions. I did not see that call in your code snippets.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 10:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Be sure to flush the session prior to committing transactions. I did not see that call in your code snippets.


If he is using Hibernate Transactions, flush() is called from Transaction.commit(). You don't need to do it manually.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 11:03 am 
Newbie

Joined: Wed Sep 03, 2003 9:08 am
Posts: 18
Even after calling the flush() before the session is closed, the changes are not updated to the database.

Now, I have a doubt. Can you please explain me how hibernate attaches the session to JBoss's transaction in case of BMTs? From the problems I've been facing, it looks like the hibernate session is not correctly associated with the jboss BMT user transaction. Is there any configuration that needs to be made explicity for enabling Hibernate to attach to the Jboss BMT user transactions? Do I need to set the parameter "jta.UserTransaction" for enabling this? (Currently I've set the parameter to "UserTransaction")


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 11:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I really don't know what you mean by "associating a session with the transaction".


The session doesn't get associated with a transaction. That is the job of the JDBC connection pool.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 11:31 am 
Newbie

Joined: Wed Sep 03, 2003 9:08 am
Posts: 18
Assume the case with the following BMT code

Code:

UserTransaction ut = sessionCtx.getUserTransaction();
ut.begin();
Session session = sessions.openSession();

// Do some operation with the session.

session.flush();
session.close();
ut.commit();



What I infer from what you say is that the hibernate session gets access to the DB connection from the JBoss when I say sessions.openSession().
And in the call session.flush(), it writes to the DB from the in-memory representation using the connection, it got. So, in the above case, when I say ut.commit(), my work should have been committed to the DB.

But, my problem is that its not being committed. :-(
I just want a way to make sure that Hibernate gets the DB connection from JBoss (when I call openSession()). Any help of debugging??

PS: I assume that JBoss connection pools is JTA enabled (as all my BMT code were working fine!)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 11:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
OK, in the case of the code you just gave, there is NO WAY that the problem can be Hibernate-related.

It must be a problem with your datasource configuration.

You can easily verify that Hibernate executes the SQL calls when session.flush() is called (just enable logging). Then after that it is no longer Hibernate's responsibility to see the the JDBC connection gets committed!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 12:01 pm 
Newbie

Joined: Wed Sep 03, 2003 9:08 am
Posts: 18
I think I got the problem!

When I just added a print statement after I got the session, everthing started to work fine.

System.out.println("Got the connection to the database : " + (session.connection() != null));

I think in the session.connection() method, if connection is null, it is fetched.

Do I need to initialize something for the connection to be fetched automatically?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 12:11 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The connection() is fetched an initialized when it is first used. Are you ABSOLUTELY certain this really happened?

Exactly what is the code between openSession() and session.flush()?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 12:15 pm 
Newbie

Joined: Wed Sep 03, 2003 9:08 am
Posts: 18
This is the code I have:


Code:
                ut = sessionCtx.getUserTransaction();
                ut.begin();
            Session session = HibernateSession.currentSession();
            System.out.println("Got the connection to the database : " + (session.connection() != null));

//business logic that uses session for creating new db rows


                session.flush();
                HibernateSession.closeSession();
                ut.commit();




In the above code, if I comment out the println statement, it is not working.


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.