-->
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.  [ 6 posts ] 
Author Message
 Post subject: Nested sessions
PostPosted: Mon Feb 02, 2004 4:04 am 
Regular
Regular

Joined: Mon Nov 03, 2003 6:10 am
Posts: 75
I am using hibernate and the spring framework...

Just included some pseudo code below.. the actually classes are a bit long winded...



I am trying to use 2 diff hibernate sessionFactories ( 1 = oracle, 2 = hsql ).

Class Foo has members: hsqlSessionFactory, Bar

Class Bar has oracleSessionFactory

I obtain these factories by having the spring framework 'set' them on context load.



In a method I do something like:

1) List hsqlResults = hsqlSession.find(classCSV);

2) ... map classCSV into oracleClass using beanutils ... (basically moving data from csv file to oracle database table)

3) oracleSession.saveList( newList ); // save new objects to oracleSession (Oracle DB)
4) oracleSession.flush()
5) oracleSession.close()

6) hsqlSession.flush()
7) hsqlSession.close()


As it is stated above it works fine...

BUT (and this is my QUESTION)...

if I do ANY of the following, the oracle SAVE doesn't seem to commit. The sql runs, and no errors are produced but
nothing ends up in the database.

i ) remove the flush() on the oracle session
ii) add a tx = session.begin() to either or both of the sessions (oracle / hsql)




Not sure what I am doing wrong. I thought you didn't have to explicitly FLUSH. Also, not sure how to do a transaction
across 2 different session sources. Or even 2 individual transactions where one is nested in the other.


Feedback appreciated!

p.s. I will put in some sample code if need be, just didn't want to if it wasn't necessary.

Thanks

Troy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 4:22 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Quote:
Just included some pseudo code below.. the actually classes are a bit long winded...

What I suggest then is that you should break down your problem to a simple class with a main method. It is difficult for us to debug your problem without seeing the whole picture.

In what sort of context are you operating (within an application server or as a standalong app?).

Data doesn't automatically get persisted (as I am sure you are aware). You need to start and commit a transaction for each session (in your case each session will be speaking JBDC to a different datasource). If you want to contain your work within a single transaction then you need to use the JTA interface and XA transactions...

So - getting back to basics - your code should look something like (from the docs)

Code:
Session oracleSession =oracleFactory.openSession();
Transaction tx = null;
try {
    tx = oracleSession.beginTransaction();
    // do some work
    tx.commit();
}
catch (Exception e) {
    if (tx!=null) tx.rollback();
    throw e;
}
finally {
    sess.close();
}

Session hsqlSession = hsqlFactory.openSession();
Transaction tx = null;
try {
    tx = hsqlSession.beginTransaction();
    // do some work
    tx.commit();
}
catch (Exception e) {
    if (tx!=null) tx.rollback();
    throw e;
}
finally {
    sess.close();
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 4:25 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
This may be a little clearer (and compile):
Code:
Session oracleSession =oracleFactory.openSession();
Transaction oracleTx = null;
try {
    oracleTx= oracleSession.beginTransaction();
    // do some work
    oracleTx.commit();
}
catch (Exception e) {
    if (oracleTx!=null) oracleTx.rollback();
    throw e;
}
finally {
    oracleSession.close();
}

Session hsqlSession = hsqlFactory.openSession();
Transaction hsqlTx = null;
try {
    hsqlTx= hsqlSession.beginTransaction();
    // do some work
    hsqlTx.commit();
}
catch (Exception e) {
    if (hsqlTx!=null) hsqlTx.rollback();
    throw e;
}
finally {
    hsqlSession .close();
}


Top
 Profile  
 
 Post subject: got it working
PostPosted: Mon Feb 02, 2004 2:35 pm 
Regular
Regular

Joined: Mon Nov 03, 2003 6:10 am
Posts: 75
Thanks for the clear explanation. I thought that was the case but I wasn't sure after testing.

It turns out that there was a case in which the tx.commit() was not occuring. (BAD CODE on my part!)

I now have a similar structure to what is noted above...

Oracle transaction is nested inside of the Hsql Transaction and it appears to be working correctly.


You mentioned Xdatasources... are these needed to do a distributed transaction.

i.e. if I want to use the same transaction for the entire process rather than nest an Oracle Transaction inside of the HSql transaction process.


Thanks again for clearing it up.


Troy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2004 12:57 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Quote:
Oracle transaction is nested inside of the Hsql Transaction and it appears to be working correctly.

Great - Just remember that although the nesting occurs at a syntax/code level, the one transaction is not going to have a bearing on the other, unless...
Quote:
You mentioned Xdatasources... are these needed to do a distributed transaction.

Yes. Of course you need to use a driver that supports XA Datasources too. I know that oracle provides one, but not 100% sure about mysql. To be honest - I don't have much practical experience with XA Datasources so I can't point you in any direction. Perhaps someone else can help out?


Top
 Profile  
 
 Post subject: XA Distributed transaction
PostPosted: Tue Feb 03, 2004 4:23 am 
Regular
Regular

Joined: Mon Nov 03, 2003 6:10 am
Posts: 75
I appreciate your help.

If anyone has any experience with distributed transactions. Specifically on (Tomcat for DEV) and in production on WEBSPHERE 5, I would appreciate any assistance.

Cheers


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.