-->
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: Transaction rollback with hibernate annotations
PostPosted: Sat Apr 15, 2006 1:29 pm 
Beginner
Beginner

Joined: Sat Apr 15, 2006 12:49 pm
Posts: 20
Location: austria
hi

I'm not sure if this is the right place to post my question but I'll try anyway.

I'm using Hibernate version: 3.2.0.cr1 and Annotations 3.1beta9 in combination with the Spring Framework (1.2.7). If I use the following hibernate mapping file instead of the annotations everything works fine:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-cascade="none">
    <class name="org.transaction.User" table="User" dynamic-insert="false" dynamic-update="false">
        <id name="userId" type="java.lang.Long" unsaved-value="null">
            <column name="userId" sql-type="BIGINT"/>
            <generator class="increment">
            </generator>
        </id>
        <property name="userName" type="java.lang.String">
            <column name="userName" sql-type="VARCHAR(255)"/>
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" sql-type="VARCHAR(255)"/>
        </property>
    </class>
</hibernate-mapping>


My problem is when I use the following annotated class instead of the mapping file to tell spring about hibernate managed pojos, transaction roll-back is not working proberly anymore. I.e. if I call a method that saves an entity using the hibernate session and later on within the same transaction boundry a RuntimeException is thrown to tell Spring to roll-back the transaction, the entity is persisted to the database anyway. The transaction roll-back does not have any effect on the insert statement ( according to the log: insert into User (version, password, userName) values (?, ?, ?) ). But as I said, this only occurs if I use the annotated version of Hibernate.

Could anyone please give me a hint where the problem could lie?

Thanks in advance

John

Annotated Pojo
Code:
package org.transaction;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

@Entity
@org.hibernate.annotations.Entity(dynamicInsert = false, dynamicUpdate = false)
public class User {
   private Long userId;
   private String userName;

   @Id
   @GeneratedValue(generator = "incremendId")
   @GenericGenerator(name = "incremendId", strategy = "native")
   public Long getUserId() {
      return userId;
   }

   public void setUserId(Long userId) {
      this.userId = userId;
   }

   @Column
   public String getUserName() {
      return userName;
   }
   public void setUserName(String userName) {
      this.userName = userName;
   }
}


Session Factory in Spring
Code:
   <bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="dataSource">
         <ref bean="dataSource" />
      </property>
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
         </props>
      </property>
<!-- used with hibernate mapping file -->
<!--
      <property name="mappingResources">
         <list>
            <value>org/transaction/User.hbm.xml</value>
         </list>
      </property>
-->      
<!-- used with hibernate annotations -->
      <property name="configLocation">
         <value>classpath:/hibernate.cfg.xml</value>
      </property>
      <property name="configurationClass">
         <value>org.hibernate.cfg.AnnotationConfiguration</value>
      </property>
   </bean>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 17, 2006 9:17 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
the fact that the insertion occurs does not means the Tx is not rollbacked.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 17, 2006 10:56 am 
Beginner
Beginner

Joined: Sat Apr 15, 2006 12:49 pm
Posts: 20
Location: austria
hm,.. the thing is when I use the hbm.xml file the insertion actually does not occur - which would be the situation I'd like to have (i.e. if some exception occurs and spring tells the transaction to roll-back I really would like to have the database state as before starting the transaction). but on the other side, using hibernate annotations (keeping the transaction configuration identical) always persists the entity - no matter if a roll-back occurs or not.

What would be the strategy to guarantee that a roll-back really sets back the original db state?

thanks

john

debug output using hibernate annotations
Code:
2006-04-17 16:50:08,917 DEBUG [org.hibernate.impl.SessionImpl] - <opened session at timestamp: 4691089034665984>
2006-04-17 16:50:08,955 DEBUG [org.hibernate.transaction.JDBCTransaction] - <begin>
2006-04-17 16:50:08,955 DEBUG [org.hibernate.jdbc.ConnectionManager] - <opening JDBC connection>
2006-04-17 16:50:08,956 DEBUG [org.hibernate.transaction.JDBCTransaction] - <current autocommit status: true>
2006-04-17 16:50:08,957 DEBUG [org.hibernate.transaction.JDBCTransaction] - <disabling autocommit>
2006-04-17 16:50:08,957 DEBUG [org.hibernate.jdbc.JDBCContext] - <after transaction begin>
2006-04-17 16:50:08,962 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - <Bound value [org.springframework.jdbc.datasource.ConnectionHolder@aa3e5b] for key [org.apache.commons.dbcp.BasicDataSource@39452f] to thread [main]>
2006-04-17 16:50:08,962 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - <Bound value [org.springframework.orm.hibernate3.SessionHolder@b57af1] for key [org.hibernate.impl.SessionFactoryImpl@9a99eb] to thread [main]>
2006-04-17 16:50:08,963 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - <Initializing transaction synchronization>
2006-04-17 16:50:08,964 INFO [ApplicationTest] - <Began transaction (1): transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager@1ef45e0]; default rollback = true>
2006-04-17 16:50:08,988 DEBUG [org.springframework.transaction.interceptor.TransactionInterceptor] - <Getting transaction for org.transaction.UserBean.createUser>
2006-04-17 16:50:08,988 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - <Retrieved value [org.springframework.orm.hibernate3.SessionHolder@b57af1] for key [org.hibernate.impl.SessionFactoryImpl@9a99eb] bound to thread [main]>
2006-04-17 16:50:08,988 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - <Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@aa3e5b] for key [org.apache.commons.dbcp.BasicDataSource@39452f] bound to thread [main]>
2006-04-17 16:50:09,017 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - <Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]>
2006-04-17 16:50:09,089 INFO [org.springframework.jdbc.support.SQLErrorCodesFactory] - <SQLErrorCodes loaded: [DB2, HSQL, MS-SQL, MySQL, Oracle, Informix, PostgreSQL, Sybase]>
2006-04-17 16:50:09,098 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - <Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@aa3e5b] for key [org.apache.commons.dbcp.BasicDataSource@39452f] bound to thread [main]>
2006-04-17 16:50:09,100 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - <Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@aa3e5b] for key [org.apache.commons.dbcp.BasicDataSource@39452f] bound to thread [main]>
2006-04-17 16:50:09,100 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - <Retrieved value [org.springframework.orm.hibernate3.SessionHolder@b57af1] for key [org.hibernate.impl.SessionFactoryImpl@9a99eb] bound to thread [main]>
2006-04-17 16:50:09,100 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - <Retrieved value [org.springframework.orm.hibernate3.SessionHolder@b57af1] for key [org.hibernate.impl.SessionFactoryImpl@9a99eb] bound to thread [main]>
2006-04-17 16:50:09,105 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - <saving transient instance>
2006-04-17 16:50:09,108 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - <saving [org.transaction.User#<null>]>
2006-04-17 16:50:09,108 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - <executing insertions>
2006-04-17 16:50:09,134 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - <executing identity-insert immediately>
2006-04-17 16:50:09,134 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - <Inserting entity: org.transaction.User (native id)>
2006-04-17 16:50:09,136 DEBUG [org.hibernate.jdbc.AbstractBatcher] - <about to open PreparedStatement (open PreparedStatements: 0, globally: 0)>
2006-04-17 16:50:09,136 DEBUG [org.hibernate.SQL] - <insert into User (password, userName) values (?, ?)>
Hibernate: insert into User (password, userName) values (?, ?)
2006-04-17 16:50:09,136 DEBUG [org.hibernate.jdbc.AbstractBatcher] - <preparing statement>
2006-04-17 16:50:09,167 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - <Dehydrating entity: [org.transaction.User#<null>]>
2006-04-17 16:50:09,167 DEBUG [org.hibernate.type.StringType] - <binding 'pw' to parameter: 1>
2006-04-17 16:50:09,169 DEBUG [org.hibernate.type.StringType] - <binding 'userName' to parameter: 2>
2006-04-17 16:50:09,171 DEBUG [org.hibernate.id.IdentifierGeneratorFactory] - <Natively generated identity: 26>
2006-04-17 16:50:09,171 DEBUG [org.hibernate.jdbc.AbstractBatcher] - <about to close PreparedStatement (open PreparedStatements: 1, globally: 1)>
2006-04-17 16:50:09,171 DEBUG [org.hibernate.jdbc.AbstractBatcher] - <closing statement>
2006-04-17 16:50:09,178 DEBUG [org.springframework.transaction.interceptor.RuleBasedTransactionAttribute] - <Applying rules to determine whether transaction should rollback on java.lang.RuntimeException>
2006-04-17 16:50:09,178 DEBUG [org.springframework.transaction.interceptor.RuleBasedTransactionAttribute] - <Winning rollback rule is: null>
2006-04-17 16:50:09,178 DEBUG [org.springframework.transaction.interceptor.RuleBasedTransactionAttribute] - <No relevant rollback rule found: applying superclass default>
2006-04-17 16:50:09,178 DEBUG [org.springframework.transaction.interceptor.TransactionInterceptor] - <Invoking rollback for transaction on org.transaction.UserBean.createUser due to throwable [java.lang.RuntimeException]>
2006-04-17 16:50:09,179 DEBUG [org.hibernate.transaction.JDBCTransaction] - <rollback>
2006-04-17 16:50:09,204 DEBUG [org.hibernate.transaction.JDBCTransaction] - <re-enabling autocommit>
2006-04-17 16:50:09,205 DEBUG [org.hibernate.transaction.JDBCTransaction] - <rolled back JDBC Connection>
2006-04-17 16:50:09,205 DEBUG [org.hibernate.jdbc.JDBCContext] - <after transaction completion>
2006-04-17 16:50:09,205 DEBUG [org.hibernate.jdbc.ConnectionManager] - <transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!>
2006-04-17 16:50:09,205 DEBUG [org.hibernate.impl.SessionImpl] - <after transaction completion>
2006-04-17 16:50:09,206 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - <Clearing transaction synchronization>
2006-04-17 16:50:09,206 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - <Removed value [org.springframework.orm.hibernate3.SessionHolder@b57af1] for key [org.hibernate.impl.SessionFactoryImpl@9a99eb] from thread [main]>
2006-04-17 16:50:09,206 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - <Removed value [org.springframework.jdbc.datasource.ConnectionHolder@aa3e5b] for key [org.apache.commons.dbcp.BasicDataSource@39452f] from thread [main]>
2006-04-17 16:50:09,206 DEBUG [org.hibernate.impl.SessionImpl] - <closing session>
2006-04-17 16:50:09,207 DEBUG [org.hibernate.jdbc.ConnectionManager] - <performing cleanup>
2006-04-17 16:50:09,207 DEBUG [org.hibernate.jdbc.ConnectionManager] - <releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]>
2006-04-17 16:50:09,212 DEBUG [org.hibernate.jdbc.JDBCContext] - <after transaction completion>
2006-04-17 16:50:09,212 DEBUG [org.hibernate.jdbc.ConnectionManager] - <transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!>
2006-04-17 16:50:09,212 DEBUG [org.hibernate.impl.SessionImpl] - <after transaction completion>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 17, 2006 11:29 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
But here, even if the insertion happened (or any SQL DML statements), since the transaction has been subsequently rollbacked, the database state should exctly be like when you've started the transaction. Have you actually checked the DB?

BTW the insertion occors because the generator strategies are different between your hbm files and your annotations

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 17, 2006 12:08 pm 
Beginner
Beginner

Joined: Sat Apr 15, 2006 12:49 pm
Posts: 20
Location: austria
Thanks a lot emmanuel! I changed the GenericGenerator strategy in my annotated file to 'increment' (instead of native to delegate the key generation to my mysql db) and now everything works fine!

Could you estimate if this is intended behaviour or just some strange results of my system's configuration?

thanks again,

john


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 17, 2006 12:11 pm 
Beginner
Beginner

Joined: Sat Apr 15, 2006 12:49 pm
Posts: 20
Location: austria
... short appendix: with GeneratorStrategy 'native' the database actually is not in the same state as before the start of the transaction. the row is inserted and roll-back does not have any effect...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 17, 2006 5:07 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
the insertion is the intended behavior
rollback not rollbacking it is definetly a bug, bug you've got to fall back to a Spring forum to solve it.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 4:21 am 
Beginner
Beginner

Joined: Sat Apr 15, 2006 12:49 pm
Posts: 20
Location: austria
ok, I'll do that.

emmanuel, thanks again for your effort!

john


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.