-->
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.  [ 7 posts ] 
Author Message
 Post subject: Why to start DB transaction for createCriteria/createQuery?
PostPosted: Tue Jun 03, 2008 4:21 pm 
Newbie

Joined: Tue Jun 03, 2008 3:49 pm
Posts: 6
Location: Dominican Republic
Hibernate version:
hibernate-3.2.6.ga

Mapping documents:
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>

Code between sessionFactory.openSession() and session.close():
//getSession() will obtain "getCurrentSession()" from the SessionFactory
Criteria crit = getSession().createCriteria(ConfigModule.class);
crit.list();


Full stack trace of any exception that occurs:
org.hibernate.HibernateException: createCriteria is not valid without active transaction

org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:297)
$Proxy0.createCriteria(Unknown Source)

Name and version of the database you are using:
Oracle 10g
The generated SQL (show_sql=true):
N/A


I fully understand the above error, and know that if i span a transaction before calling the above code i'll get the list from from database.

But WHY do i need to create a database transaction just for mere SELECT operation.

If i recall it correctly that wasn't needed in the past.

Thanks in advanced.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 10:09 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, you actually do need a transaction. A transaction represents a unit of work, and doing a select statement is indeed work.

Many times in the past, the JDBC connection would create the transaction if you didn't do it yourself explicitly. It was also friendly on closes. It's not so freindly anymore.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Why to start DB transaction for createCriteria/createQuery?
PostPosted: Wed Jun 04, 2008 10:58 am 
Newbie

Joined: Tue Jun 03, 2008 3:49 pm
Posts: 6
Location: Dominican Republic
Hi Mr/Mrs. Cameron

Thanks for taking your time and replied my inquiry regarding this.

Thinking about the resources that a transaction takes on the Database, spanning it for just a SELECT statement, seems wrong to me. Unless someone can debate that.

Again, thanks for your time on reading this post and sharing your thoughts.


Cameron McKenzie wrote:
Well, you actually do need a transaction. A transaction represents a unit of work, and doing a select statement is indeed work.

Many times in the past, the JDBC connection would create the transaction if you didn't do it yourself explicitly. It was also friendly on closes. It's not so freindly anymore.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 4:12 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
transaction:
A unit of interaction with a DBMS or similar system.

-Definition from dictionary.com

Do you need to interact with the database to query records? If not, you don't need a transaction. If you do, you do need a transaction.

:)

When you transact with the database, it must be done within the scope of a transaction. It's pretty much true by definition.

-Cameron McKenzie

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 4:14 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
transaction:
A unit of interaction with a DBMS or similar system.

-Definition from dictionary.com

Do you need to interact with the database to query records? If not, you don't need a transaction. If you do, you do need a transaction.

:)

When you transact with the database, it must be done within the scope of a transaction. It's pretty much true by definition.

-Cameron McKenzie

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 6:20 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Actually most DB's implicitly create a new transaction for each statement when not declared by you (or jdbc, or hibernate), so the performance isn't really a point;
You could actually get better performance when using transaction even on plain selects because if your HQL is actually executed by running multiple SQL statements it will use just one transaction, instead of many...
Additionally you get "repeatable reads", imagine the problem when fetching some fields of your complex entity if the entity was suddenly removed from DB, this is a problem you probably are not ready to manage during a simple select.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Why to start DB transaction for createCriteria/createQuery?
PostPosted: Wed Jun 04, 2008 8:46 pm 
Newbie

Joined: Tue Jun 03, 2008 3:49 pm
Posts: 6
Location: Dominican Republic
Thanks for sharing thoughts, really, really appreciate it.

s.grinovero wrote:
Actually most DB's implicitly create a new transaction for each statement when not declared by you (or jdbc, or hibernate), so the performance isn't really a point;
You could actually get better performance when using transaction even on plain selects because if your HQL is actually executed by running multiple SQL statements it will use just one transaction, instead of many...
Additionally you get "repeatable reads", imagine the problem when fetching some fields of your complex entity if the entity was suddenly removed from DB, this is a problem you probably are not ready to manage during a simple select.


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