i use spring manage the Transaction and add the Transaction manager into the service class ,but the transaction do not rollback
the config:
Code:<?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:p="http://www.springframework.org/schema/p" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 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/jee http://www.springframework.org/schema/jee/spring-jee-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"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/test" p:username="root" p:password="test"/> <bean id="userService" class="com.test.service.UserService"> <property name="userMapper" ref="userMapper" /> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="ServiceOperation" expression="execution(* com.test.interfaces.IService.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="ServiceOperation" /> </aop:config> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="sqlSessionFactory" class="org.springframework.orm.ibatis3.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:config/config.xml" /> <property name="mapperLocations" value="classpath:mapper/*Mapper.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="userMapper" class="org.springframework.orm.ibatis3.support.MapperFactoryBean"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> <property name="mapperInterface" value="com.test.dao.UserDao" /> </bean> </beans>
the service code:
Code:package com.test.service; import com.test.dao.UserDao; import com.test.entry.User; import com.test.interfaces.IService; public class UserService implements IService<User> { private UserDao userMapper; public UserDao getUserMapper() { return userMapper; } public void setUserMapper(UserDao userMapper) { this.userMapper = userMapper; } public void save(User user) { userMapper.saveUser(user); } }
the action code
Code:public User getUser() { ApplicationContext acx = new ClassPathXmlApplicationContext( "classpath:config/context.xml"); IService<User> us = acx.getBean("userService",IService.class); User user = new User(); try { user.setId("111"); us.save(user);//step 1 success us.save(user);//step 2 exception but do not rollback } catch (Exception e) { e.printStackTrace(); } return user; }


Reply With Quote
