-->
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: Session and Transaction questions
PostPosted: Mon Dec 01, 2003 4:51 pm 
Beginner
Beginner

Joined: Tue Nov 18, 2003 12:34 am
Posts: 39
Location: Dallas, Texas, US
I am trying to understand the basics of Hibernate transactions and the way how they should be efficiently used. My questions are as follows:

1. Is Transaction.commit() and session.connection().commit() operate on the same database connection? I am asking because I don't want use Transaction API unless it is required.
2. Does Hibernate (Session) maintain some kind of default transaction for me or should I explicitly begin, commit or rollback for each logical unit of work?
3. Does flush(), in addition to executing sql generated due to save() or update(), also generate/execute sql for statments such as add() or remove() in a parent child relation? In other words, do I have to explicitly do a saveOrUpdate() an entity, which is already persistent?

Thanks,
Madhan.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 5:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Some of the answers depend on the context so in general:
Quote:
Is Transaction.commit() and session.connection().commit() operate on the same database connection? I am asking because I don't want use Transaction API unless it is required.

Yes - You should always be using a transaction when accessing the database. This is even for read only operations.
Quote:
Does Hibernate (Session) maintain some kind of default transaction for me or should I explicitly begin, commit or rollback for each logical unit of work?

Depends on the context. If using a JDBC connection then you will need to start the transaction and commit or rollback at the end. If using a container datasource and CMT then the container will provide the start and commit but you will have to provide the hint to rollback.
Quote:
Does flush(), in addition to executing sql generated due to save() or update(), also generate/execute sql for statments such as add() or remove() in a parent child relation? In other words, do I have to explicitly do a saveOrUpdate() an entity, which is already persistent?

Hibernate has dirty object detection so it will apply to appropriate operation (based on the unSavedValue). If the relationship has appropriate cascading enabled then yes it will update the database relative to the collection operations.


Top
 Profile  
 
 Post subject: It depends...
PostPosted: Mon Dec 01, 2003 5:32 pm 
Newbie

Joined: Wed Nov 26, 2003 3:03 pm
Posts: 9
Location: Denver, CO
If you're using this inside an EJB container, Hibernate uses CMT and so you don't have to do anything for transactions other than session.flush() and session.close(). If you're using this inside your own stuff, then I think you need to do the session.commit(). I'm not sure about the Transaction API, I think that's just if you use an external JTA.

Derek


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 6:56 pm 
Beginner
Beginner

Joined: Tue Nov 18, 2003 12:34 am
Posts: 39
Location: Dallas, Texas, US
Thank you for your responses! I am still trying to find out the difference between the two types of commit (in a simple JDBC connection); session.connection().commit() and Transaction.commit(). I would think that the Transaction API lets you have more than one concurrent transaction per session where as session.connection().commit() can only have one transaction. Is my hypothesis accurate?

Also, when I create two sessions, does Hibernate associate both the sessions to the same database connection? Or does the second session take a new connection from the pool? This question directly relates to my fear that session.connection().commit() may impact other transaction "begin points" from different sessions.

Thanks in advance.
Madhan.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 7:06 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No, your assumption is not correct. "Transaction", that is the Hibernate Transaction API, is an abstraction layer for the underlying transaction strategy. This can be a JDBC transaction, JTA (in an application server context) or even any other strategy. If you use the Hibernate Transaction, your code is save from any changes to the actual implementation. You can deploy your DAO/persistence code in a Servlet container or an application server with JTA, without changing it.

Each Session gets a new JDBC connection from the connection pool. Just use this idiom:

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

Cat princess = new Cat();
princess.setName("Princess");
princess.setSex('F');
princess.setWeight(7.4f);
session.save(princess);

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


You have have multiple Transactions in one Session, that is "begin" and "commit" blocks.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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.