-->
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.  [ 3 posts ] 
Author Message
 Post subject: Native identifier generation and transaction rollback
PostPosted: Wed Jul 06, 2005 2:41 am 
Newbie

Joined: Wed Jul 06, 2005 1:56 am
Posts: 3
Does use of the native identifier generation cause a commit to the database even when the connection's autocommit is set to false? When doing a save on an entity inside a transaction, it gets saved to the database even when I invoke rollback instead of commit. Hibernate does an insert to get the database-generated identifier, but that insert is not undone when I call rollback after the save.

Hibernate version: 3.0

Mapping documents:
<hibernate-mapping>
<class name="uexp.model.Venue" table="venues">
<id name="venueKey" column="venue_id" unsaved-value="0">
<generator class="native"/>
</id>
<property name="venueId" column="uid" type="uexp.dao.impl.hibernate.UidUserType" not-null="true" unique="true" access="field"/>
<property name="name" column="name" not-null="true"/>
<property name="address" column="address"/>
<property name="phone" column="phone"/>
<many-to-one name="parent" column="parent" class="uexp.model.Venue" access="field"/>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
tx = session.beginTransaction();
session.save(venue);
tx.rollback();

Full stack trace of any exception that occurs:

Name and version of the database you are using: MySQL 4.0.20a

The generated SQL (show_sql=true): insert into venues (uid, name, address, phone, parent) values (?, ?, ?, ?, ?)

Debug level Hibernate log excerpt:
2005-07-05 23:18:07,696 -DEBUG- [in] {JDBCTransaction} begin
2005-07-05 23:18:07,696 -DEBUG- [in] {JDBCTransaction} current autocommit status: false
2005-07-05 23:18:07,696 -DEBUG- [in] {DefaultSaveOrUpdateEventListener} saving transient instance
2005-07-05 23:18:07,696 -DEBUG- [in] {AbstractSaveEventListener} generated identifier: , using strategy: org.hibernate.id.IdentityGenerator
2005-07-05 23:18:07,696 -DEBUG- [in] {AbstractSaveEventListener} saving [uexp.model.Venue#<null>]
2005-07-05 23:18:07,696 -DEBUG- [in] {AbstractSaveEventListener} executing insertions
2005-07-05 23:18:07,696 -DEBUG- [in] {BasicEntityPersister} Inserting entity: uexp.model.Venue (native id)
2005-07-05 23:18:07,696 -DEBUG- [in] {AbstractBatcher} about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2005-07-05 23:18:07,696 -DEBUG- [in] {SQL} insert into venues (uid, name, address, phone, parent) values (?, ?, ?, ?, ?)
2005-07-05 23:18:07,696 -DEBUG- [in] {AbstractBatcher} preparing statement
2005-07-05 23:18:07,696 -DEBUG- [in] {BasicEntityPersister} Dehydrating entity: [uexp.model.Venue#<null>]
2005-07-05 23:18:07,706 -DEBUG- [in] {IdentifierGeneratorFactory} Natively generated identity: 1022
2005-07-05 23:18:07,706 -DEBUG- [in] {AbstractBatcher} about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2005-07-05 23:18:07,706 -DEBUG- [in] {AbstractBatcher} closing statement
2005-07-05 23:18:07,706 -DEBUG- [in] {JDBCTransaction} rollback
2005-07-05 23:18:07,706 -DEBUG- [in] {JDBCContext} before transaction completion
2005-07-05 23:18:07,706 -DEBUG- [in] {SessionImpl} before transaction completion
2005-07-05 23:18:07,706 -DEBUG- [in] {JDBCTransaction} rolled back JDBC Connection
2005-07-05 23:18:07,706 -DEBUG- [in] {JDBCContext} after transaction completion
2005-07-05 23:18:07,706 -DEBUG- [in] {SessionImpl} after transaction completion
2005-07-05 23:18:08,146 -DEBUG- [in] {SessionImpl} closing session
2005-07-05 23:18:08,146 -DEBUG- [in] {ConnectionManager} closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2005-07-05 23:18:08,146 -DEBUG- [in] {JDBCContext} after transaction completion
2005-07-05 23:18:08,146 -DEBUG- [in] {SessionImpl} after transaction completion
2005-07-05 23:18:08,146 -INFO - [in] {VenueManager} Saved venue with uid = 9e7d8004eacb47d0


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 06, 2005 9:24 pm 
Beginner
Beginner

Joined: Wed Jul 06, 2005 8:18 pm
Posts: 23
There's nothing committed to the database as you can see from the log. Since you're using sequence/auto-increment id, the database always increments the counter regardless of transaction to ensure all transactions will get a unique id. Check the mysql doc for auto-increment id.

fyi, hibernate obtains the nextid by invoking last_insert_id (http://www.mysqlfreaks.com/statements/90.php)


Top
 Profile  
 
 Post subject: transaction rollback and MySQL InnoDB
PostPosted: Thu Jul 07, 2005 2:02 pm 
Newbie

Joined: Wed Jul 06, 2005 1:56 am
Posts: 3
I solved the problem.

First, Hibernate does send a full insert command to the database rather than just invoking last_insert_id. That is the insert that shows up in the Hibernate log file.

The problem is not with Hibernate. Looking at the MySQL logs (not shown here), Hibernate is behaving as a good citizen. The problem lies in the way I created the tables in MySQL.

In order to get expected transactional behavior when using the InnoDB version of MySQL, you need to append an extra tag at the end of the table create command:

CREATE TABLE User (username VARCHAR(30)) TYPE=InnoDB;

The TYPE attribute makes all the difference.


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