springmvc 事务不能正常回滚,大神们帮忙看看
盲眼杜 2018-05-17 09:33:28 《--这是我的配置文件---》
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 多个拦截器,顺序执行 -->
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/*/*.do"></mvc:mapping>
<bean class="com.xicai.controller.SessionFilter"></bean> </mvc:interceptor>
</mvc:interceptors>
<!-- bean的配置 -->
<!-- <bean id="oneInterface" class="com.imooc.ioc.interfaces.OneInterfaceImpl"></bean> -->
<!-- 数据库连接的信息 都在properties文件中 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application-test-mysql.properties</value>
</list>
</property>
</bean>
<!-- 2.数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false" />
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000" />
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2" />
</bean>
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver.class2}" />
<property name="jdbcUrl" value="${jdbc.url2}" />
<property name="user" value="${jdbc.username2}" />
<property name="password" value="${jdbc.password2}" />
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false" />
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000" />
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2" />
</bean>
<!--数据分配 -->
<bean id="dynamicDataSource" class="com.xicai.utils.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="dataSource" key="dataSource"></entry>
<entry value-ref="dataSource2" key="dataSource2"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSource">
</property>
</bean>
<!-- 包级扫描 -->
<context:component-scan base-package="com">
</context:component-scan>
<!-- 开启事务注解驱动 -->
<tx:annotation-driven />
<!-- 开启Spring注解 -->
<context:annotation-config />
<!-- mybatis的配置 -->
<!-- 1 sqlsessionfactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis的配置文件 -->
<!-- <property name="configLocation" value="classpath:configuration.xml"></property> -->
<!-- 加载数据库信息 -->
<property name="dataSource" ref="dynamicDataSource"></property>
<property name="mapperLocations" value="classpath:com/xicai/vo/*Mapper.xml" />
<property name="typeAliasesPackage" value="com.xicai.vo" />
</bean>
<!-- sqlsession -->
<bean id="st" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory">
</constructor-arg>
</bean>
<!-- <mvc:annotation-driven /> -->
<!-- 配置文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8">
</bean>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>
-------------------------------------------------------------------------------------------------------------------------------------------------------
《---这是我开启测试的类---》
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xicai.controller.Arer_Sold_outController;
import com.xicai.controller.UserYuanController;
public class testMvc {
ClassPathXmlApplicationContext cac = new ClassPathXmlApplicationContext("applicationContext.xml");
Arer_Sold_outController uc = (Arer_Sold_outController) cac.getBean("arer_Sold_outController");
UserYuanController us = (UserYuanController) cac.getBean("userYuanController");
public void testMvc_01() {
uc.addreta1();
}
@Test
public void testMvc_02() {
us.addus();
}
}
-------------------------------------------------------------------------------------------------------
《----这个是调用的方法---》
public void addus(){
Calendar cal = Calendar.getInstance();
String firdate = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss").format(cal.getTime());
System.out.println(firdate+"------66----");
UserYuan us=new UserYuan();
us.setY_name(firdate);
userYuanService.addUserYuan(us);
System.out.println(9/0);
}
----------------------------------------------------------------------------------------------------------
《---这是我的Service----》
@Service("userYuanService")
@Transactional
public class UserYuanServiceImpl implements UserYuanService{
@Resource(name="dao1")
private BaseDao dao;
@Override
@Transactional(propagation=Propagation.REQUIRED)
public void addUserYuan(UserYuan user) {
DatabaseContextHolder.setCustomerType(DataSourceInterceptor.dataSource);
dao.insert("com.xicai.vo.UserYuan.addUserYuan", user);
}