Announcement Announcement Module
Collapse
No announcement yet.
Transaction question about HibernateTransactionManager Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transaction question about HibernateTransactionManager

    I have some troubles with transaction, my environnement is Hibernate / MySql and I use declarative transaction (PROPAGATION_REQUIRED) on my manager. Here a method on my manager, personne have a relation many-to-one with adresse, the cascade is all. if an exception is throwed during personne save, the adresse is not deleted.


    Code:
    	public void save(Personne personne) throws ManagerException {
    		Adresse adresse = adresseDao.findByAdresse(personne.getAdresse());
    		if(adresse != null) {
    			personne.setAdresse(adresse);
    		}
    		personneDao.save(personne); >> Exception throwed here (duplicate key for my junit tests)
    	}
    I use org.springframework.orm.hibernate.HibernateTransac tionManager as transaction manager.

    By the way, I use also AOP to manage SQL exception so my manager declaration is like this :
    Code:
       <bean id="personneManagerTarget" class="com.euroflash.gestion.manager.impl.hbm.PersonneManagerImpl">
            <property name="personneDao">
                <ref bean="personneDao"/>
            </property>
            <property name="adresseDao">
                <ref bean="adresseDao"/>
            </property>
       </bean>
       
       <bean id="personneManagerHbm" 
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager">
                <ref bean="transactionManager"/>
            </property>
            <property name="target">
                <ref bean="personneManagerTarget"/>
            </property>
            <property name="transactionAttributes">
                <props>
                    <prop key="*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
        
       <bean id="personneManager" class="org.springframework.aop.framework.ProxyFactoryBean"> 
    	   	<property name="proxyInterfaces">
    	   		<value>com.euroflash.gestion.manager.PersonneManager</value>
    	   	</property>
    	   	<property name="target">
    	   		<ref local="personneManagerHbm"/>
    	   	</property>
    	   	<property name="interceptorNames"> 
    	   		<list>
    	   			<value>exceptionManager</value>
    	   		</list>
    	   	</property> 
       	</bean>
    Any ideas ?

  • #2
    First of all, You can make your beans configuration more compact:
    Code:
      <bean id="personneManagerTarget" class="com.euroflash.gestion.manager.impl.hbm.PersonneManagerImpl"> 
        <property name="personneDao"> 
           <ref bean="personneDao"/> 
        </property> 
        <property name="adresseDao"> 
           <ref bean="adresseDao"/> 
        </property> 
      </bean> 
        
      <bean id="personneManager" 
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
        <property name="transactionManager"> 
          <ref bean="transactionManager"/> 
        </property> 
        <property name="proxyInterfaces"> 
          <value>com.euroflash.gestion.manager.PersonneManager</value> 
        </property> 
        <property name="target"> 
          <ref bean="personneManagerTarget"/> 
        </property> 
        <property name="transactionAttributes"> 
          <props> 
             <prop key="*">PROPAGATION_REQUIRED</prop> 
          </props> 
        </property> 
        <property name="preInterceptors"> 
          <list> 
            <value>exceptionManager</value> 
          </list> 
        </property> 
      </bean>
    As for transaction troubles in your application, you are using Hibernate / MySql. AFAIK, MySql (upto 4.x) does not support transactions.

    Could you provide more informations: Exception stackTrace, exceptionManager role, did your code works when using only Hibernate (without Spring)...

    Comment


    • #3
      Thanx for the trick for my applicationContext.xml

      For MySql, it is supporting transactions (http://dev.mysql.com/doc/mysql/en/COMMIT.html)

      So here my full stack trace

      Code:
      004-08-26 23&#58;26&#58;40,784 DEBUG &#91;net.sf.hibernate.persister.EntityPersister&#93; - Inserting entity&#58; com.euroflash.gestion.business.Adresse &#40;native id&#41;
      2004-08-26 23&#58;26&#58;40,784 DEBUG &#91;net.sf.hibernate.impl.BatcherImpl&#93; - about to open&#58; 0 open PreparedStatements, 0 open ResultSets
      2004-08-26 23&#58;26&#58;40,784 DEBUG &#91;net.sf.hibernate.SQL&#93; - insert into Adresse &#40;adresse, codePostal, ville, portable, telephone&#41; values &#40;?, ?, ?, ?, ?&#41;
      2004-08-26 23&#58;26&#58;40,784 DEBUG &#91;net.sf.hibernate.impl.BatcherImpl&#93; - preparing statement
      2004-08-26 23&#58;26&#58;40,784 DEBUG &#91;net.sf.hibernate.persister.EntityPersister&#93; - Dehydrating entity&#58; &#91;com.euroflash.gestion.business.Adresse#<null>&#93;
      2004-08-26 23&#58;26&#58;40,785 DEBUG &#91;net.sf.hibernate.type.StringType&#93; - binding '19 rue de la chate' to parameter&#58; 1
      2004-08-26 23&#58;26&#58;40,785 DEBUG &#91;net.sf.hibernate.type.StringType&#93; - binding '75019' to parameter&#58; 2
      2004-08-26 23&#58;26&#58;40,785 DEBUG &#91;net.sf.hibernate.type.StringType&#93; - binding 'Paris' to parameter&#58; 3
      2004-08-26 23&#58;26&#58;40,785 DEBUG &#91;net.sf.hibernate.type.StringType&#93; - binding '0667852439' to parameter&#58; 4
      2004-08-26 23&#58;26&#58;40,785 DEBUG &#91;net.sf.hibernate.type.StringType&#93; - binding null to parameter&#58; 5
      2004-08-26 23&#58;26&#58;40,786 DEBUG &#91;net.sf.hibernate.persister.AbstractEntityPersister&#93; - Natively generated identity&#58; 38
      2004-08-26 23&#58;26&#58;40,787 DEBUG &#91;net.sf.hibernate.impl.BatcherImpl&#93; - done closing&#58; 0 open PreparedStatements, 0 open ResultSets
      2004-08-26 23&#58;26&#58;40,787 DEBUG &#91;net.sf.hibernate.impl.BatcherImpl&#93; - closing statement
      2004-08-26 23&#58;26&#58;40,787 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - saving &#91;com.euroflash.gestion.business.Journalier#<null>&#93;
      2004-08-26 23&#58;26&#58;40,787 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - executing insertions
      2004-08-26 23&#58;26&#58;40,787 DEBUG &#91;net.sf.hibernate.persister.EntityPersister&#93; - Inserting entity&#58; com.euroflash.gestion.business.Journalier &#40;native id&#41;
      2004-08-26 23&#58;26&#58;40,788 DEBUG &#91;net.sf.hibernate.impl.BatcherImpl&#93; - about to open&#58; 0 open PreparedStatements, 0 open ResultSets
      2004-08-26 23&#58;26&#58;40,789 DEBUG &#91;net.sf.hibernate.SQL&#93; - insert into Personne &#40;salaire, ss, visiteMedicale, tauxHoraire, tauxHoraireSupp, idFonction, email, idAdresse, prenom, nom, type&#41; values &#40;?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'JOURNALIER'&#41;
      2004-08-26 23&#58;26&#58;40,789 DEBUG &#91;net.sf.hibernate.impl.BatcherImpl&#93; - preparing statement
      2004-08-26 23&#58;26&#58;40,789 DEBUG &#91;net.sf.hibernate.persister.EntityPersister&#93; - Dehydrating entity&#58; &#91;com.euroflash.gestion.business.Journalier#<null>&#93;
      2004-08-26 23&#58;26&#58;40,790 DEBUG &#91;net.sf.hibernate.type.LongType&#93; - binding '123' to parameter&#58; 1
      2004-08-26 23&#58;26&#58;40,790 DEBUG &#91;net.sf.hibernate.type.StringType&#93; - binding 'aaaaaaaaaaaaaaaaa' to parameter&#58; 2
      2004-08-26 23&#58;26&#58;40,791 DEBUG &#91;net.sf.hibernate.type.TimestampType&#93; - binding '2004-08-26 23&#58;26&#58;38' to parameter&#58; 3
      2004-08-26 23&#58;26&#58;40,792 DEBUG &#91;net.sf.hibernate.type.DoubleType&#93; - binding '12.0' to parameter&#58; 4
      2004-08-26 23&#58;26&#58;40,794 DEBUG &#91;net.sf.hibernate.type.DoubleType&#93; - binding '15.0' to parameter&#58; 5
      2004-08-26 23&#58;26&#58;40,794 DEBUG &#91;net.sf.hibernate.type.LongType&#93; - binding null to parameter&#58; 6
      2004-08-26 23&#58;26&#58;40,795 DEBUG &#91;net.sf.hibernate.type.StringType&#93; - binding '[email protected]' to parameter&#58; 7
      2004-08-26 23&#58;26&#58;40,795 DEBUG &#91;net.sf.hibernate.type.LongType&#93; - binding '38' to parameter&#58; 8
      2004-08-26 23&#58;26&#58;40,795 DEBUG &#91;net.sf.hibernate.type.StringType&#93; - binding 'Foo' to parameter&#58; 9
      2004-08-26 23&#58;26&#58;40,795 DEBUG &#91;net.sf.hibernate.type.StringType&#93; - binding 'bar' to parameter&#58; 10
      2004-08-26 23&#58;26&#58;40,796 DEBUG &#91;net.sf.hibernate.util.JDBCExceptionReporter&#93; - SQL Exception
      java.sql.SQLException&#58; Invalid argument value,  message from server&#58; "Duplicate entry '[email protected]' for key 2"
      	at com.mysql.jdbc.MysqlIO.checkErrorPacket&#40;MysqlIO.java&#58;1697&#41;
      	at com.mysql.jdbc.MysqlIO.sendCommand&#40;MysqlIO.java&#58;1083&#41;
      	at com.mysql.jdbc.MysqlIO.sqlQueryDirect&#40;MysqlIO.java&#58;1142&#41;
      	at com.mysql.jdbc.Connection.execSQL&#40;Connection.java&#58;1876&#41;
      	at com.mysql.jdbc.PreparedStatement.executeInternal&#40;PreparedStatement.java&#58;1590&#41;
      	at com.mysql.jdbc.PreparedStatement.executeUpdate&#40;PreparedStatement.java&#58;1653&#41;
      	at com.mysql.jdbc.PreparedStatement.executeUpdate&#40;PreparedStatement.java&#58;1492&#41;
      	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate&#40;DelegatingPreparedStatement.java&#58;94&#41;
      	at net.sf.hibernate.persister.EntityPersister.insert&#40;EntityPersister.java&#58;528&#41;
      	at net.sf.hibernate.persister.EntityPersister.insert&#40;EntityPersister.java&#58;432&#41;
      	at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute&#40;ScheduledIdentityInsertion.java&#58;29&#41;
      ...
      2004-08-26 23&#58;26&#58;40,883 WARN &#91;net.sf.hibernate.util.JDBCExceptionReporter&#93; - SQL Error&#58; 1062, SQLState&#58; S1009
      2004-08-26 23&#58;26&#58;40,883 ERROR &#91;net.sf.hibernate.util.JDBCExceptionReporter&#93; - Invalid argument value,  message from server&#58; "Duplicate entry '[email protected]' for key 2"
      2004-08-26 23&#58;26&#58;40,884 DEBUG &#91;net.sf.hibernate.impl.BatcherImpl&#93; - done closing&#58; 0 open PreparedStatements, 0 open ResultSets
      2004-08-26 23&#58;26&#58;40,884 DEBUG &#91;net.sf.hibernate.impl.BatcherImpl&#93; - closing statement
      2004-08-26 23&#58;26&#58;40,884 DEBUG &#91;net.sf.hibernate.util.JDBCExceptionReporter&#93; - SQL Exception
      ...
      2004-08-26 23&#58;26&#58;40,886 WARN &#91;net.sf.hibernate.util.JDBCExceptionReporter&#93; - SQL Error&#58; 1062, SQLState&#58; S1009
      2004-08-26 23&#58;26&#58;40,886 ERROR &#91;net.sf.hibernate.util.JDBCExceptionReporter&#93; - Invalid argument value,  message from server&#58; "Duplicate entry '[email protected]' for key 2"
      2004-08-26 23&#58;26&#58;40,886 ERROR &#91;net.sf.hibernate.util.JDBCExceptionReporter&#93; - could not insert&#58; &#91;com.euroflash.gestion.business.Journalier&#93;
      java.sql.SQLException&#58; Invalid argument value,  message from server&#58; "Duplicate entry '[email protected]' for key 2"
      ...
      
      2004-08-26 23&#58;26&#58;40,888 DEBUG &#91;net.sf.hibernate.transaction.JDBCTransaction&#93; - rollback
      2004-08-26 23&#58;26&#58;41,007 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - transaction completion
      2004-08-26 23&#58;26&#58;41,008 DEBUG &#91;net.sf.hibernate.transaction.JDBCTransaction&#93; - re-enabling autocommit
      2004-08-26 23&#58;26&#58;41,009 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - closing session
      2004-08-26 23&#58;26&#58;41,009 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - disconnecting session
      2004-08-26 23&#58;26&#58;41,009 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - transaction completion
      2004-08-26 23&#58;26&#58;41,011 DEBUG &#91;com.euroflash.gestion.manager.ManagerExceptionHandler&#93; - Clé dupliquée, impossible de sauvegarder l'entité com.euroflash.gestion.business.Journalier@d159e4&#91;ss=aaaaaaaaaaaaaaaaa,visiteMedicale=Thu Aug 26 23&#58;26&#58;38 CEST 2004,salaire=123,permisConduire=<null>,tauxHoraire=12.0,tauxHoraireSupp=15.0,fonction=<null>,prenom=Foo,nom=bar,adresse=com.euroflash.gestion.business.Adresse@e6cecc&#91;adresse=19 rue de la chate,codePostal=75019,ville=Paris,telephone=<null>,portable=0667852439,fax=<null>,id=38&#93;,email=[email protected],id=<null>&#93;
      The role of my exception handler is to wrap technical exception, every catched Throwable are re-throwed.

      Comment

      Working...
      X