-->
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.  [ 8 posts ] 
Author Message
 Post subject: Hibernate TX v. DB TX: same or distinct?
PostPosted: Fri Jun 02, 2006 2:12 pm 
Beginner
Beginner

Joined: Thu Apr 13, 2006 12:56 pm
Posts: 23
Is Hibernate Transaction:

1. A wrapper around underlying DB TX (and therefore the same thing), or
2. Not a wrapper, but a one-to-one mapping to underlying DB TX, or
3. Different from underlying DB TX. (Therefore one Hibernate TX can encapsulate multiple underlying DB TXs)?

thanks
-nikita

Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.1.2

Name and version of the database you are using:
MySQL 5.0 InnoDB, (SERIALIZABLE TX)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 6:13 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
A Hibernate transaction isn't necessarily a specific thing. You can configure Hibernate to operate in different transaction scenarios via the config file or properties. For example, you can have:

Code:
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>


Which implies that the Hibernate "transaction" is a JDBC transaction. Or you could have:

Code:
<property name="hibernate.current_session_context_class">jta</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>


which tells Hibernate to use the J2EE EJB container transactions.


Top
 Profile  
 
 Post subject: Hibernate TX v. DB TX: same or distinct?
PostPosted: Sun Jun 04, 2006 3:42 pm 
Beginner
Beginner

Joined: Thu Apr 13, 2006 12:56 pm
Posts: 23
Well, let's focus on non-JTA env, i.e. using JDBCTransactionFactory. Still, the question remains: what is that JDBCTransaction's mapping to underlying DB transaction (as viewed by MySQL server).

Here's the reason for my question:

-MySQL 5.0 server defaults to autocommit after every SQL statement, and apparently, disabling that complicates (or makes impossible) DB clustering.

-Hibernate, on the other hand, defaults to false for property hibernate.connection.autocommit (and enabling this is "not recommended").

So, now let's say we have the following code:

Code:
try{
Session s = sf.openSession();
s.beginTransaction();

s.save(A);
s.save(B);

s.getTransaction().commit();
}
catch(RuntimeException ex){
s.getTransaction.rollback();
}



Let's say insert of B is rejected by DB. Application logic would expect both inserts (of A, and B) to be rolled back since hibernate.connection.autocommit == false. However, since MySQL server's default setting is autocommit==true (commit after each statement), 'insert A' will NOT be rolled back by DB. Unless Hibernate settings somehow override db server settings at runtime....

Hence my original question. I suppose it can be asked another way:

Does setting hibernate.connection.autocommit == false guarantee that
underlying db's autocommit setting is also now false?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 10:40 am 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
Well, it should. Constructing a simple test to verify it shouldn't take much effort. I have to say I don't understand your statement regarding autocommit and clustering. I think the two are unrelated. In fact, I would say that using autocommit in a clustered architecture doesn't make sense. Autocommit makes for poor performance, prevents any sort of higher-level transaction design, and forces each separate statement to leave the database in a valid state. These behaviours all imply a very simplistic application, while clustering is associated with relatively sophisticated applications.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 12:29 pm 
Beginner
Beginner

Joined: Thu Apr 13, 2006 12:56 pm
Posts: 23
Sorry, I meant failover, not clustering:

From MySQL's current Connector/J ref docs:

"MySQL Connector/J has fail-over support. This allows the driver to fail-over to any number of "slave"
hosts and still perform read-only queries. Fail-over only happens when the connection is in an autoCommit(
true) state, because fail-over can not happen reliably when a transaction is in progress. Most application
servers and connection pools set autoCommit to 'true' at the end of every transaction/connection
use"
-Section 1.3.1

Yes, at the point I will write a test to verify the above - I just thought the expected behavior is well-defined...

-nikita


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 12:40 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
The behaviour probably is well-defined, but I have not tried it out personally. I'm not sure what it means to set autocommit "at the end" of each transaction. If the "transaction" is at the end, then presumably it was ended explicitly via a manual commit. This doesn't sound exactly like autocommit, which would occur throughout a transaction, not just at the end. It may be that it is just saying that failover will not help with transactions that are in progress when a server/database goes down, but will allow subsequent transactions to continue smoothly.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 12:51 pm 
Beginner
Beginner

Joined: Thu Apr 13, 2006 12:56 pm
Posts: 23
Apparently, there is no way to permanently disable autocommit in mysql. the closest to that is disabling autocommit for the duration of given session (not to be confused with hibernate Session). however, if one issues START TRANSACTION; autocommit is disabled until COMMIT; is issued. at that point autocommit is automatically re-enabled by mysql server - "at the end" of tx.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 3:08 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
I would say that you are ok then. It's really not a "true autocommit", since it only comes in at the end of the transaction. So it should be fine having it disabled by Hibernate, which should apply throughout the transaction, except at the very end where it will apparently be enabled transiently.


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