Bonjour tout le monde,
j'utilise Spring pour mes transactions,sauf que lorsque j'ai des exceptions,je n'ai pas de rollback qui est effectué.Voici mon code.
spring-database.xml
spring-context.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="maDataSource"> <property name="driverClassName" value="oracle.jdbc.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@monserveur:1521:MONDEV"/> <property name="username" value="MABASE"/> <property name="password" value="MABASE"/> <property name="testOnBorrow" value="true"/> <property name="validationQuery" value="SELECT 1 FROM DUAL"/> <property name="maxActive" value="40"/> <property name="maxIdle" value="20"/> <property name="minIdle" value="5"/> <property name="maxWait" value="60000"/> <property name="timeBetweenEvictionRunsMillis" value="900000"/> <property name="numTestsPerEvictionRun" value="5"/> <property name="removeAbandoned" value="true"/> <property name="removeAbandonedTimeout" value="30"/> <property name="logAbandoned" value="true"/> </bean>
spring-services.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" > <context:annotation-config /> <bean class = "fr.sib.digor.spring.extension.AnnotationTransactionAttributeSourceReplacerPostProcessor" /> <bean id="maSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="maDataSource" /> <property name="configLocations"> <list> <value>classpath*:hibernateSIPSDM.cfg.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> <prop key="hibernate.show_sql">false</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="maSessionFactory" /> <qualifier value="txManager" /> </bean> <tx:annotation-driven /> </beans>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.0.xsd"> <bean id="namedJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <constructor-arg ref="maDataSource"/> </bean> <aop:aspectj-autoproxy/> </beans>
Mon service:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 @Service public class MonService { @Autowired protected MonDAO monDAO; @Transactional(value="txManager", rollbackFor={Exception.class}) public void serviceAppelant(String param) throws Exception{ try { monDAO.updateSex(param); } catch (Exception up) { throw up; } } }
Mon DAO
Ma méthode de TU
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 @Repository public class MonDAO { @Autowired private NamedParameterJdbcTemplate namedJdbcTemplate; public void updateSex(String param){ try { SqlParameterSource namedParameters = new MapSqlParameterSource( "param", param); namedJdbcTemplate.update(SQL_TEMPLATE_UPDATE, namedParameters); //Je provoque une exception namedJdbcTemplate.update("MAUVAISE_REQUETE", namedParameters); } catch (Exception e) { throw e; } } }
Une exception est lancée,sauf que le rollback n'est jamais fait.J'ai essayé de mettre
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 @Test public void update() throws Exception{ monService.serviceAppelant("1289"); }Cependant dans ce cas la transaction n'est jamais committée.
Code : Sélectionner tout - Visualiser dans une fenêtre à part <property name="defaultAutoCommit" value="false"/>
Merci d'avance de votre aide.
Partager