syluser
Aug 26th, 2004, 03:04 PM
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.
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 :
<bean id="personneManagerTarget" class="com.euroflash.gestion.manager.impl.hbm.PersonneMan agerImpl">
<property name="personneDao">
<ref bean="personneDao"/>
</property>
<property name="adresseDao">
<ref bean="adresseDao"/>
</property>
</bean>
<bean id="personneManagerHbm"
class="org.springframework.transaction.interceptor.Transa ctionProxyFactoryBean">
<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 ?
irbouho
Aug 26th, 2004, 03:50 PM
First of all, You can make your beans configuration more compact:
<bean id="personneManagerTarget" class="com.euroflash.gestion.manager.impl.hbm.PersonneMan agerImpl">
<property name="personneDao">
<ref bean="personneDao"/>
</property>
<property name="adresseDao">
<ref bean="adresseDao"/>
</property>
</bean>
<bean id="personneManager"
class="org.springframework.transaction.interceptor.Transa ctionProxyFactoryBean">
<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)...
syluser
Aug 26th, 2004, 04:43 PM
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
004-08-26 23:26:40,784 DEBUG [net.sf.hibernate.persister.EntityPersister] - Inserting entity: com.euroflash.gestion.business.Adresse (native id)
2004-08-26 23:26:40,784 DEBUG [net.sf.hibernate.impl.BatcherImpl] - about to open: 0 open PreparedStatements, 0 open ResultSets
2004-08-26 23:26:40,784 DEBUG [net.sf.hibernate.SQL] - insert into Adresse (adresse, codePostal, ville, portable, telephone) values (?, ?, ?, ?, ?)
2004-08-26 23:26:40,784 DEBUG [net.sf.hibernate.impl.BatcherImpl] - preparing statement
2004-08-26 23:26:40,784 DEBUG [net.sf.hibernate.persister.EntityPersister] - Dehydrating entity: [com.euroflash.gestion.business.Adresse#<null>]
2004-08-26 23:26:40,785 DEBUG [net.sf.hibernate.type.StringType] - binding '19 rue de la chate' to parameter: 1
2004-08-26 23:26:40,785 DEBUG [net.sf.hibernate.type.StringType] - binding '75019' to parameter: 2
2004-08-26 23:26:40,785 DEBUG [net.sf.hibernate.type.StringType] - binding 'Paris' to parameter: 3
2004-08-26 23:26:40,785 DEBUG [net.sf.hibernate.type.StringType] - binding '0667852439' to parameter: 4
2004-08-26 23:26:40,785 DEBUG [net.sf.hibernate.type.StringType] - binding null to parameter: 5
2004-08-26 23:26:40,786 DEBUG [net.sf.hibernate.persister.AbstractEntityPersiste r] - Natively generated identity: 38
2004-08-26 23:26:40,787 DEBUG [net.sf.hibernate.impl.BatcherImpl] - done closing: 0 open PreparedStatements, 0 open ResultSets
2004-08-26 23:26:40,787 DEBUG [net.sf.hibernate.impl.BatcherImpl] - closing statement
2004-08-26 23:26:40,787 DEBUG [net.sf.hibernate.impl.SessionImpl] - saving [com.euroflash.gestion.business.Journalier#<null>]
2004-08-26 23:26:40,787 DEBUG [net.sf.hibernate.impl.SessionImpl] - executing insertions
2004-08-26 23:26:40,787 DEBUG [net.sf.hibernate.persister.EntityPersister] - Inserting entity: com.euroflash.gestion.business.Journalier (native id)
2004-08-26 23:26:40,788 DEBUG [net.sf.hibernate.impl.BatcherImpl] - about to open: 0 open PreparedStatements, 0 open ResultSets
2004-08-26 23:26:40,789 DEBUG [net.sf.hibernate.SQL] - insert into Personne (salaire, ss, visiteMedicale, tauxHoraire, tauxHoraireSupp, idFonction, email, idAdresse, prenom, nom, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'JOURNALIER')
2004-08-26 23:26:40,789 DEBUG [net.sf.hibernate.impl.BatcherImpl] - preparing statement
2004-08-26 23:26:40,789 DEBUG [net.sf.hibernate.persister.EntityPersister] - Dehydrating entity: [com.euroflash.gestion.business.Journalier#<null>]
2004-08-26 23:26:40,790 DEBUG [net.sf.hibernate.type.LongType] - binding '123' to parameter: 1
2004-08-26 23:26:40,790 DEBUG [net.sf.hibernate.type.StringType] - binding 'aaaaaaaaaaaaaaaaa' to parameter: 2
2004-08-26 23:26:40,791 DEBUG [net.sf.hibernate.type.TimestampType] - binding '2004-08-26 23:26:38' to parameter: 3
2004-08-26 23:26:40,792 DEBUG [net.sf.hibernate.type.DoubleType] - binding '12.0' to parameter: 4
2004-08-26 23:26:40,794 DEBUG [net.sf.hibernate.type.DoubleType] - binding '15.0' to parameter: 5
2004-08-26 23:26:40,794 DEBUG [net.sf.hibernate.type.LongType] - binding null to parameter: 6
2004-08-26 23:26:40,795 DEBUG [net.sf.hibernate.type.StringType] - binding 'Foo.bar@yahoo.fr' to parameter: 7
2004-08-26 23:26:40,795 DEBUG [net.sf.hibernate.type.LongType] - binding '38' to parameter: 8
2004-08-26 23:26:40,795 DEBUG [net.sf.hibernate.type.StringType] - binding 'Foo' to parameter: 9
2004-08-26 23:26:40,795 DEBUG [net.sf.hibernate.type.StringType] - binding 'bar' to parameter: 10
2004-08-26 23:26:40,796 DEBUG [net.sf.hibernate.util.JDBCExceptionReporter] - SQL Exception
java.sql.SQLException: Invalid argument value, message from server: "Duplicate entry 'Foo.bar@yahoo.fr' for key 2"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:1697)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:10 83)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java :1142)
at com.mysql.jdbc.Connection.execSQL(Connection.java: 1876)
at com.mysql.jdbc.PreparedStatement.executeInternal(P reparedStatement.java:1590)
at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java:1653)
at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java:1492)
at org.apache.commons.dbcp.DelegatingPreparedStatemen t.executeUpdate(DelegatingPreparedStatement.java:9 4)
at net.sf.hibernate.persister.EntityPersister.insert( EntityPersister.java:528)
at net.sf.hibernate.persister.EntityPersister.insert( EntityPersister.java:432)
at net.sf.hibernate.impl.ScheduledIdentityInsertion.e xecute(ScheduledIdentityInsertion.java:29)
...
2004-08-26 23:26:40,883 WARN [net.sf.hibernate.util.JDBCExceptionReporter] - SQL Error: 1062, SQLState: S1009
2004-08-26 23:26:40,883 ERROR [net.sf.hibernate.util.JDBCExceptionReporter] - Invalid argument value, message from server: "Duplicate entry 'Foo.bar@yahoo.fr' for key 2"
2004-08-26 23:26:40,884 DEBUG [net.sf.hibernate.impl.BatcherImpl] - done closing: 0 open PreparedStatements, 0 open ResultSets
2004-08-26 23:26:40,884 DEBUG [net.sf.hibernate.impl.BatcherImpl] - closing statement
2004-08-26 23:26:40,884 DEBUG [net.sf.hibernate.util.JDBCExceptionReporter] - SQL Exception
...
2004-08-26 23:26:40,886 WARN [net.sf.hibernate.util.JDBCExceptionReporter] - SQL Error: 1062, SQLState: S1009
2004-08-26 23:26:40,886 ERROR [net.sf.hibernate.util.JDBCExceptionReporter] - Invalid argument value, message from server: "Duplicate entry 'Foo.bar@yahoo.fr' for key 2"
2004-08-26 23:26:40,886 ERROR [net.sf.hibernate.util.JDBCExceptionReporter] - could not insert: [com.euroflash.gestion.business.Journalier]
java.sql.SQLException: Invalid argument value, message from server: "Duplicate entry 'Foo.bar@yahoo.fr' for key 2"
...
2004-08-26 23:26:40,888 DEBUG [net.sf.hibernate.transaction.JDBCTransaction] - rollback
2004-08-26 23:26:41,007 DEBUG [net.sf.hibernate.impl.SessionImpl] - transaction completion
2004-08-26 23:26:41,008 DEBUG [net.sf.hibernate.transaction.JDBCTransaction] - re-enabling autocommit
2004-08-26 23:26:41,009 DEBUG [net.sf.hibernate.impl.SessionImpl] - closing session
2004-08-26 23:26:41,009 DEBUG [net.sf.hibernate.impl.SessionImpl] - disconnecting session
2004-08-26 23:26:41,009 DEBUG [net.sf.hibernate.impl.SessionImpl] - transaction completion
2004-08-26 23:26:41,011 DEBUG [com.euroflash.gestion.manager.ManagerExceptionHan dler] - Clé dupliquée, impossible de sauvegarder l'entité com.euroflash.gestion.business.Journalier@d159e4[s s=aaaaaaaaaaaaaaaaa,visiteMedicale=Thu Aug 26 23:26: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[adresse=19 rue de la chate,codePostal=75019,ville=Paris,telephone=<null>,portable=0667852439,fax=<null>,id=38],email=Foo.bar@yahoo.fr,id=<null>]
The role of my exception handler is to wrap technical exception, every catched Throwable are re-throwed.
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.