-->
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.  [ 5 posts ] 
Author Message
 Post subject: Managing transactions
PostPosted: Wed Aug 27, 2003 7:02 am 
Newbie

Joined: Wed Aug 27, 2003 4:39 am
Posts: 10
I am using tomcat for my application. So would the transaction JNDI configuration be done in the web.xml like the JNDI datasource cofiguration. If I were to use the hibernate transaction API, a typical transaction would look like this
Session sess = factory.openSession();
Transaction tx;
try {
tx = sess.beginTransaction();
//do some work
...
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
sess.close();
}
When using JTA transaction manager, you mean to say I would have to do a lookup for the transaction each time I open a session and commit it before I close the session. Does this have to be done explicitly.

This doubt may seem silly but this is the first time round I am doing such a thing - lots of JNDI lookup and a transaction manager etc etc.
And Juergen, thanks for the help. I would love to look into the SpringFramework but no time for that right now :(


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 10:24 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You would not have to look up the transaction unless you wanted to do something with it, like begin(), suspend(), ...

<background>
The datasource and the transaction-manager are configured by your environment (i.e, Tomcat, ejb container, ...). In such a managed environment, a DataSource is called a ResourceManager, which essentially means it is capable of being enlisted with a TransactionManager. When a Connection is obtained from the DataSource, the DataSource (as a ResourceManager) enlists that Connection with the TransactionManager. If there is currently a JTA transaction associated with the current execution context (normally a thread), the TransactionManagers binds the connection to that transaction.
</background>

Basically steps are:
1) Obtain a UserTransaction from JNDI and call its begin().
2) perform zero-to-many transacted operations
3) Re-obtain the UserTransaction from JNDI and call its commit()/rollback()

Transactions are not fun to manage. That is exactly the reason we choose to utilize session ejbs (in fact trsnaction amnagement is the only piece of the ejb framework we are utilizing). I too am planning on taking a closer look at Spring when I get some time.

But in the short time, let me ask: are you needing to coordinate transactions across multiple datasources or different types of resource managers? If not, you really gain nothing by using JTA.


Top
 Profile  
 
 Post subject: Managing transactions
PostPosted: Thu Aug 28, 2003 6:47 am 
Newbie

Joined: Wed Aug 27, 2003 4:39 am
Posts: 10
I have this scenario,
I have to load a couple of users, check their status, and enable or disable them. Either I update all of them or none i.e if an error occurs the transaction must be rolled back. So u mean to say i simply use the hibernate transaction API like this
Session sess = factory.openSession();
Transaction tx;
try {
tx = sess.beginTransaction();
//sess.find("........");
...
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
sess.close();
}

There is another doubt though. There is an option of
hibernate.transaction.factory_class net.sf.hibernate.transaction.JTATransactionFactory
or
hibernate.transaction.factory_class net.sf.hibernate.transaction.JDBCTransactionFactory
When to chose JTATransactionFactory and if I chose this do I mention a lookup for that in the hibernate.cfg.xml or just mention it in hibernate.properties file.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 1:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
For you purpose, just use the JDBCTransactionFactory factory (and do not specify a transaction manager). The in you code:
Code:
Session sess = ...
Transaction tx;
try
{
    tx = sess.beginTransaction();
    List users = session.find( ... );

    for (Iterator itr = users.iterator(); itr.hasNext(); )
    {
        final my.User user = (my.User)itr.next();
        if (statusCheck)
            user.enable();
        else
            user.disable();
    }

    tx.commit();
}
catch (Exception e)
{
    tx.rollback();
    throw e;
}
finally
{
    sess.close();
}


Again, if you use Hibernate's JTATransactionFactory (i.e., hibernate.transaction.factory_class=net.sf.hibernate.transaction.JTATransactionFactory), Hibernate will delegate all transaction management to the transaction manager you specify. From your description of your system, though, JTA is way overkill for you needs.


Top
 Profile  
 
 Post subject: Managing transactions
PostPosted: Fri Sep 12, 2003 7:13 am 
Newbie

Joined: Wed Aug 27, 2003 4:39 am
Posts: 10
Thanks for all the details.
I guess the application is relatively simple. The only scenario where I think I would have to be careful is a shopping cart module. Hope simple session.beginTransaction() and transaction.commit() will be enough.
How is ses.connection().commit() different from transaction.commit(). Maybe a dumb question but I am getting a bit confused with this part.


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