在spring框架中使用事务处理是比较方便的,spring提供了两个方式,一个基于xml配置,另外一个使用注解。
基于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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="677714"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="UserDao" name="UserDao" class="yzr.dao.UserDao" >
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean name="UserService" class="yzr.service.UserService" >
<property name="userDao" ref="UserDao"></property>
</bean>
<bean name="UserAction" class="yzr.action.UserAction" >
<property name="userService" ref="UserService"></property>
</bean>
<bean id="txManaer"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManaer">
<tx:attributes>
<tx:method name="*" read-only="false" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* yzr.service.*Service.*(..))"
id="pt" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
</aop:config>
</beans>
1.需要导入事务和aop的名称空间。
2.指定一个事务管理(DataSourceTransactionManager),在上面例子中是spring对jdbc事务管理,使用<tx:advice>节点中的<tx:atrributes>配置事务特性。
3.绑定事务到拦截的方法上。
为了测试事务是否回滚,在service中抛出一个异常:
测试1:直接抛出异常
package yzr.service;
import yzr.dao.UserDao;
public class UserService {
public UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save(){
userDao.Add();
int i=1/0;
}
}
测试结果:执行报错,并且数据库中没有插入数据
结论:事务回滚了
测试2:try catch异常
package yzr.service;
import yzr.dao.UserDao;
public class UserService {
public UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save(){
try {
userDao.Add();
int i = 1 / 0;
} catch (Exception e) {
// TODO: handle exception
}
}
}
测试结果:执行没有报错,数据库插入了数据。
测试结论:当异常被trycatch之后将属于正常执行,既不会报错,也不会事务回滚。
基于使用注解的方式:
在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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启注解 -->
<context:component-scan base-package="yzr"></context:component-scan>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://123.207.89.46:3306/test"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="677714"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="UserDao" name="UserDao" class="yzr.dao.UserDao" >
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean name="UserService" class="yzr.service.UserService" >
<property name="userDao" ref="UserDao"></property>
</bean>
<bean name="UserAction" class="yzr.action.UserAction" >
<property name="userService" ref="UserService"></property>
</bean>
<!-- 5. 事务控制配置, 基于注解 -->
<!-- 5.1 配置事务管理器类-->
<bean id="txManaer" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 5.2 开启事务注解开关 -->
<tx:annotation-driven transaction-manager="txManaer"/>
</beans>
在service中使用@Transactional
@Transactional(
isolation=Isolation.DEFAULT,
propagation=Propagation.REQUIRED,
readOnly=false,
timeout=-1
)
public void save(){
deptDao.save();
//throw new RuntimeException("怎么还不回滚");
int i = 1/0;
}
数据库一般都支持事务,像sqlserver,oracle和mysql,但要注意一下的是,当你发现按照上面的配置进行测试事务是否正常回滚的时候,发现事务怎样都没有回滚,这时候需要留意一下数据库引擎是否支持事务特性,打个比如,当你使用的是mysql的时候,输入show ENGINES;
默认的数据库引擎是MyIASM引擎,(上图你看到的默认的是InnoDB是因为我已经把数据库改成默认为InnoDB引擎了),MyIASM引擎是不支持事务的,而且一眼扫过就只有InnoDB支持事务特性。
当你是因为数据库引擎问题引起不能正常回滚时,只需要把你mysql的my.ini配置文件将默认引擎改为InnoDB。然后再使用alter table tableName ENGINE=InnoDB命令
修改mysql的my.ini配置:
default-storage-engine=引擎名称
然后注释skip innoDB这一行。
重新启动mysql服务即可。