一. springmvc注解事务
之前一直有用事务,但是之前都只算使用。今天有才有空深入了解一下事务的配置使用。本人比较懒(哈哈哈),就先从懒人比较中意的注解事务开始吧。
怕有新手上路,所以spring.xml 和spring-mvc.xml都放出来
spring-mvc.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 使用spring组件扫描@controller -->
<context:component-scan base-package="com.zm.transaction.web"/>
<!-- 通过annotation-driven可以替代下边的处理器映射器和适配器 -->
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />-->
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
</beans>
spring.xml
<?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.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">
<!-- 导入配置文件 -->
<context:property-placeholder location="classpath:properties/db.properties"/>
<!-- druid 数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat" />
<!-- 配置初始化大小、最小、最大 -->
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="20" />
</bean>
<!-- 扫描mybatis 配置文件-->
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.zm.transaction.entity" />
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml" />
</bean>
<!-- 扫描Mapper 配置文件-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zm.transaction.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 添加事务注解支持 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="userService" class="com.zm.transaction.service.impl.UserServiceImpl">
</bean>
</beans>
然后在就是在service实现中添加注解了,在我这里就是上面的 com.zm.transaction.service.impl.UserServiceImpl类,
@Transactional
有两种用法:
(1)把这个注解放在类名称上面了,这样你配置的这个@Transactional 对这个类中的所有public方法都起作用.
(2)@Transactional 方法方法名上,只对这个方法有作用,同样必须是public的方法
我是使用在方法上注解的,并且使用了异常回滚。当insertUser成功时抛出异常,不执行insertRole。但是查询数据库insertUser插入的数据也被回滚了。
//@Service("userService")没有手动配置
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;//这里的mapper是我的dao层,上面有配置所以可以自动加载
@Override
@Transactional (rollbackFor=RuntimeException.class)
public int addUser(User user) throws Exception {
if(userMapper.insertUser(user) != 0)//如果插入成功,则抛出异常
throw new RuntimeException(" ");//用来测试事务的异常
userMapper.insertRole("会员","会员菜单");
return 1;
}
}
@Transactional还有其他的功能
超时:
@Transactional(timeout=30) //默认是30秒
异常回滚:
指定单一异常类:@Transactional(rollbackFor=RuntimeException.class)
指定多个异常类:@Transactional(rollbackFor={RuntimeException.class, Exception.class})
该属性用于设置需要进行回滚的异常类数组,当方法中抛出指定异常数组中的异常时,则进行事务回滚。
正常的情况下也可以回滚:
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
只读:
@Transactional(readOnly=true)
该属性用于设置当前事务是否为只读事务,设置为true表示只读,false则表示可读写,默认值为false。
二.非注解事务配置
其实非注解事务只要在spring.xml里把配置改一下就行了,重复的配置就不在展示了,下面是需要改动
<?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"
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">
<!-- 添加事务注解支持 --> <!-- 删除下面这一行 -->
<!--<tx:annotation-driven transaction-manager="txManager"/>-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 添加以下配置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes><!-- method name 需要与你的方法名前缀相同 -->
<tx:method name="get*" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" isolation="READ_COMMITTED"
rollback-for="java.lang.RuntimeException" />
<tx:method name="update*" />
</tx:attributes>
</tx:advice>
<aop:config><!-- 括号内配置你的service所在包 -->
<aop:pointcut id="serviceMethod" expression="execution(* com.zm.transaction.service.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
</beans>
用过一种再是用另一种是不是很快上手,我就写这么多了,如果有什么错误欢迎批评指正。