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.  [ 8 posts ] 
Author Message
 Post subject: Hibernate + JOTM: Hibernate not enlisting resources?
PostPosted: Thu Nov 02, 2006 4:02 pm 
Newbie

Joined: Thu Nov 02, 2006 3:43 pm
Posts: 4
Hi all!

I was wondering if you could clear up an issue I have with Hibernate using JTA transactions. I have been experimenting with Hibernate combined with JOTM, but some things are confusing.

Here is roughly what I do:
1. Start JOTM
2. Acquire a UserTransaction from JOTM
3. Store it in JNDI so Hibernate can find it
4. Create two SessionFactories to access two different databases
5. Configured them [the session factories] to use Jotm and jta
6. Stored the XADataSources in JNDI so Hibernate would find them
5. Call begin() on the UserTransaction acquired from JOTM
6. Get a Session for each of the factories
7. Save an object in each database
8. Commit the transaction with the UserTransaction acquired from JOTM


Now everything appears to work fine, but looking at the log it doesn't seem like Hibernate enlists any XAResource with JOTM when it acquires an XAConnection from the XADataSources found in JNDI. Investigating further I actually went in and specifically logged [changed the source code & recompiled jotm] in JOTM when a resource got enlisted and it never did when running this example.

Should not Hibernate enlist XAConnections with JOTM [or any TransactionManager] automatically when everything is configured correct??


PS. I have some further details about this which explains why the example still appears to run correctly even though no resources get enlisted with JOTM, but I want to see if someone knows anything about this first


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 4:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
hibenrate is not an resource; it just uses the connection.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 03, 2006 6:10 am 
Newbie

Joined: Thu Nov 02, 2006 3:43 pm
Posts: 4
I never said Hibernate was a resource. I know it only uses the connection it obtains from the datasource to communicate with the database, but this connection needs to be enlisted with a Transaction Manager for distributed transactions, right?

Hibernate does something similar to this I assume:

Code:
/* Hibernate gets connection from datasource */
XADataSource ds = context.lookup("derbyds");
XAConnection con = ds.getXAConnection();

/* The XAResource needed by JOTM can be retrieved this way now */
XAResource res = con.getXAResource();



So Hibernate is not a resource, but it uses an XAConnection where you can get an XAResource, which is the type of resource that needs to be registered with a Transaction Manager for distributed transactions. (Or transactions involving more than one database)

To me it seems that Hibernate does not do this...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 03, 2006 7:03 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
why don't you look in the code ? :)

as far as i see it, then its the appserver/datasource providers job of enlisting these things.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 03, 2006 7:50 am 
Newbie

Joined: Thu Nov 02, 2006 3:43 pm
Posts: 4
I could look in the code, but I don't think it would tell me much more than the Hibernate debug/info messages. I know the connections are not getting enlisted because JOTM logs this (and I double checked with the JOTM source code).

I don't know exactly who is responsible for doing the enlistment, but the reason I think Hibernate is the one that should do it is based on the code example I gave you. There is only one datasource for each database and they are all available from JNDI. Hibernate does a getXAConnection() call and effectively gets its own JDBC (XA) connection to play with. As you can see the XAResource that needs to be registered is retrieved from the XAConnection that was returned to Hibernate. Which leaves the question, how is it possible for anyone else but Hibernate to enlist that connection?

The only possible solution that I can see is that the middleware layer creates a wrapper for the XADataSource and stores the datasources in JNDI using this wrapper instead. When Hibernate does the getXAConnection() call the middleware can enlist the connection before returning the XAConnection to Hibernate.

I just think it would make a lot of sense if Hibernate did the enlistment since it has to interact with the Transaction Manager anyway. I would think it could be done in just a few lines of code.

What I need to know is the reason Hibernate does not do this? Or if I am correct in my assumptions, I might have missed something.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 03, 2006 7:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
we don't do any XA* stuff in Hibernate...we just get the connection.

The API for that is ConnectionProvider which is extensible API which you probably can customize to do what you want.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 03, 2006 8:26 am 
Newbie

Joined: Thu Nov 02, 2006 3:43 pm
Posts: 4
Ah... This should probably be mentioned to all those people trying to get Hibernate to work with JOTM. That they have to write a custom ConnectionProvider that I assume needs to do:

1. Enlist a connection with JOTM
2. Give Hibernate back a Connection from an XAConnection object

Otherwise Hibernate will only "seem" to work with JOTM (or any other transaction manager)

I will investigate the ConnectionProvider to see if I can use it to get it to work...

Another thing though. You say you dont do any *XA stuff in Hibernate, but how can Hibernate then use a XADataSource? The XADataSource does not have a regular getConnection() method....
To use the XADataSource you need first to get a XAConnection from which you do a getConnection()....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 03, 2006 10:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
or simply have the datasource provider do it (that is what happens in appservers AFAIK)

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

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.