-->
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.  [ 9 posts ] 
Author Message
 Post subject: Session - transacton strategy during "Open session in v
PostPosted: Tue Sep 27, 2005 4:12 am 
Newbie

Joined: Fri Apr 02, 2004 2:34 am
Posts: 8
Location: Germany
Hibernate version:
3.1b<LATEST>
Full stack trace of any exception that occurs:
Exception "Transaction was not properly started"
Name and version of the database you are using:
MySQL
Question
What is a valid strategy to commit/rollback transaction if I'm using "Open session in View" pattern.
Currently I'm doing following:
1. Before execution of the business code open a session and open transaction
2. After execution of the business code check that no runtime exception occur and commit transaction. But do not close session because session will be needed for the view (lazy load).
3. After view proceeded, rollback the transaction.

With latest hibernate 3.1 I've got "Transaction was not started" exception on the step 3. This seems to be a new feature that was added in the latest beta. How do you think, what is the most proper way to handle the situation:
1. After commiting transaction first time (after business objects execution) opoen another transaction and rollback it afterwards.
2. Do not open transaction second time and do not rollback it either.

Thank you.

Renat Zubairov

_________________
Br.
Renat


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 5:03 am 
Newbie

Joined: Wed Sep 28, 2005 4:07 am
Posts: 11
You do not need a Transaction to load Objects. So if your view does not change the Objects (views should not do that), you do not need a Transaction.

_________________
Lars Fiedler


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 5:06 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Wrong! Please don't give people bad advice and repeat the usual auto-commit mode confusion. All communication with a database occurs inside a transaction. Using auto-commit mode and many small transactions, one for each statement, is a very bad idea.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 9:26 am 
Newbie

Joined: Wed Sep 28, 2005 4:07 am
Posts: 11
I would never use auto-commit mode!

But if you only read from the database and don't change the persistent objects, I think there is no use to call session.beginTransaction(). I always wondered why the Hibernate-examples, that only read from the Database, call session.beginTransaction(). So where is the difference? These examples, run well without calling session.beginTransaction().

I know, that every select needs a database-connection, and that every database-transaction belongs to a database-connection. But I think, it is not necessary to commit or rollback, if you only read from the database. So you don't need a Hibernate-Transaction. The session encapsulates a Database-Connection (if not disconnected) so Select-Queries work fine without calling session.beginTransaction().

Am I wrong?

_________________
Lars Fiedler


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 10:31 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
There is no such thing as No Transaction.

The examples run because beginTransaction() is not needed in a non-managed environment, as setting auto-commit to false, when obtaining a connection, implicitly starts a transaction. The same code won't run anywhere else.

Of course it is necessary to commit or rollback if you read from a database. What else should happen? There is no third state or option. If you don't do it yourself you get UNDEFINED (or at best documented DEFAULT) behavior. That's not what you want and you should not recommend it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 7:54 am 
Newbie

Joined: Fri Apr 02, 2004 2:34 am
Posts: 8
Location: Germany
But it still do not clear. For the transaction that happened during the view rendering (Open session in View) which action should I do at the end? Should I commit it or rollback? I.E. is it possible that Hibernate will do some modification inside the database during resolution of the lazy associations?

_________________
Br.
Renat


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 8:04 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Why would you ever rollback if no problem occured (except in integration testing, maybe)? A regular transaction begins and commits.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 9:54 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
LarsFiedler wrote:
The session encapsulates a Database-Connection (if not disconnected) so Select-Queries work fine without calling session.beginTransaction().
Am I wrong?

Transaction must be defined (demarcated by "begin" and "end") to read correct data, it transaction is undefined then data returned from query is undefined too (you will get stale data this way in serializable isolation level).


Top
 Profile  
 
 Post subject: Re: Session - transacton strategy during "Open session
PostPosted: Fri Sep 30, 2005 10:03 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
zubairov wrote:
What is a valid strategy to commit/rollback transaction if I'm using "Open session in View" pattern.
Currently I'm doing following:
1. Before execution of the business code open a session and open transaction
2. After execution of the business code check that no runtime exception occur and commit transaction. But do not close session because session will be needed for the view (lazy load).
3. After view proceeded, rollback the transaction.

This is wrong, 3 can read wrong data if yor transaction is demarcated as "request". Correct is the simplest way: begin, commit transaction (rollback on exception) and close session ( request = transaction ).


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