Spring配置文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 引入数据源配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db-config.properties" />
</bean>
<!-- 数据源配置 -->
<bean id="DruidDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${initialSize}" />
<property name="maxActive" value="${maxActive}" />
<property name="minIdle" value="${minIdle}" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${maxWait}" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testWhileIdle" value="${testWhileIdle}" />
<property name="testOnBorrow" value="${testOnBorrow}" />
<property name="testOnReturn" value="${testOnReturn}" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="${poolPreparedStatements}" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="${maxPoolPreparedStatementPerConnectionSize}" />
</bean>
<!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="DruidDataSource" />
<!-- 配置mybatis配置文件的位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 配置扫描Mapper XML的位置 -->
<property name="mapperLocations" value="classpath*:/mybatis/**/*.xml" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" />
</bean>
<!-- 扫描的包路径@Service、@Component、@Repository;@Controller在springmvc配置文件中去; -->
<context:component-scan base-package="com.chensan.ssm.*.serviceImpl" />
<context:component-scan base-package="com.chensan.ssm.*.mapper" />
<!-- 声明事务管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="DruidDataSource" />
<!-- 对服务层进行事务处理,需要注意服务层方法名称开头单词
(如果不是propagation="REQUIRED", 则该方法中只能读取数据不能增删改),
否则可能导致改方法异常(java.sql.SQLException: Connection is read-only)
-->
<!--事务增强 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="batchCreate*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="execute*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="query*" propagation="SUPPORTS" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true" />
<!-- 使用tx/aop来配置 -->
<aop:config>
<!--目录结构,该目录下类注解:@Service("xxxService")只会扫描public修饰符修饰的-->
<!-- 通过aop定义事务增强切面 -->
<aop:pointcut id="txPointcut"
expression="execution(public * com.chensan.ssm.*.service..*.*(..))" />
<!-- 引用事务增强 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
</beans>
如果有事务管理的方法报错:HTTP Status 500 - Request processing failed; nested exception is org.springframework.transaction.NoTransactionException: No transaction aspect-managed TransactionStatus in scope
这是事务管理配置有问题,没能进行事务管理;
Spring配置文件必须扫描进行事务管理的类(事务可在sevice和controller进行管理),controller是在springmvc中扫描注入的,那么只能在service层进行事务管理。
默认spring事务只在发生未被捕获的 runtimeexcetpion时才回滚。
spring aop异常捕获原理:被拦截的方法需显式抛出异常,并不能经任何处理,这样aop代理才能捕获到方法的异常,才能进行回滚,默认情况下aop只捕获runtimeexception的异常,但可以通过配置来捕获特定的异常并回滚。
换句话说在service的方法中不使用try catch 或者在catch中最后加上throw new runtimeexcetpion(),这样程序异常时才能被aop捕获进而回滚。
解决方案:方案1.例如service层处理事务,那么service中的方法中不做异常捕获,或者在catch语句中最后增加throw new RuntimeException()语句,以便让aop捕获异常再去回滚,并且在service上层(webservice客户端,view层action)要继续捕获这个异常并处理
方案2.在service层方法的catch语句中增加:TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();语句,手动回滚,这样上层就无需去处理异常。
看到以下内容,说明事务被管理
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@389b9772]
Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@389b9772]
Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@389b9772]
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5c4da9ff]则未被事务管理;
如果在Controller配置事务,那么将事务在springmvc的配置文件中配置,在controller的增删改的方法上添加@Transactional注解。