现在说下Springd事务。在Spring中提供俩种事务管理方式。
1、通过annotation(注解)的方式来声明事务,也称声明式事务;
2、通过xml配置文件(aop技术)来实现事务。
下面是事务的配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:context="https://www.springframework.org/schema/context" xmlns:aop="https://www.springframework.org/schema/aop" xmlns:tx="https://www.springframework.org/schema/tx" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.5.xsd https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-2.5.xsd https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.5.xsd https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.gjt.mm.mysql.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test?userUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="5" /> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1" /> <!-- 连接池的最大值 --> <property name="maxActive" value="500" /> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="2" /> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="1" /> </bean> <!-- 1.注解方式配置事务 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="txManager" /> <!-- 2.xml方式配置事务.利用aop技术 <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.test.service..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/> <tx:method name="*"/></tx:attributes> </tx:advice> --> <bean id="personService" class="com.test.service.impl.PersonServiceImpl"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
我一般采用的是注解方式,因为比较简单一点。
配置文件里声明启用注解事务之后,看下面代码
package com.test.service.impl; import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.test.bean.Person; import com.test.service.PersonService; @Transactional public class PersonServiceImpl implements PersonService { private SimpleJdbcTemplate simpleJdbcTemplate; public void setDataSource(DataSource dataSource) { this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource); } // 两种例外 // 1.unchecked 默认回滚事务(RuntimeException) // 2.checked 默认不回滚事务(Exception) // 开启事务.并设置unchecked例外不回滚 @Transactional(propagation = Propagation.REQUIRED, noRollbackFor = RuntimeException.class) public void delete(int id) { simpleJdbcTemplate.update("delete from person where id=?", new Object[] { id }); throw new RuntimeException("运行期例外"); } /** * 说下事务的传播属性 * REQUIRED * (默认)业务方法需要在事务中运行,如果方法运行的时候已经开启事务,就加入事务,否则创建新的事务 * NOT_SUPPORTED * 声明方法不需要开启事务.如果已经开启事务后调用该方法.则先挂起该事务,方法执行完毕后,事务恢复 * REQUIRES_NEW * 不管是否存在事务,都另行开启新的事务.如果先前已经开启事务,挂起原先事务.创建新的事务. * 新的事务执行结束后,恢复原先事务 * MANDATORY * 只能在已经存在的事务中执行,不能自行创建事务,如果在没有开启事务的环境下调用,容器抛出例外 * SUPPORTS * 墙头草,有事务就在事务中执行.没事务就在没事务的环境下执行 * NEVER * 绝对不能在事务中执行.如果执行.抛出例外 * NESTED * 如果有事务存在,开启一个新事务嵌套在上层事务中.开启的新事务具有自己独立的回滚点.新事务的回滚不会对外部事务造成影响, * 但是外部事务的提交会直接提交嵌套事务 * 如果没有事务存在,则按照REQUIRED属性执行,该属性只对DataSourceTransationManager事务管理器有效 */ @Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务 public Person getPerson(int personId) { return simpleJdbcTemplate.queryForObject( "select * from person where id=?", ParameterizedBeanPropertyRowMapper.newInstance(Person.class), personId); } @Transactional(propagation = Propagation.NEVER) // 不能在事务环境下执行 @SuppressWarnings("unchecked") public List<Person> getPersons() { String sql = "select * from person"; List persons = simpleJdbcTemplate.getJdbcOperations().query(sql, new BeanPropertyRowMapper(Person.class)); return persons; } @Transactional(propagation = Propagation.NEVER) public void save(Person person) { simpleJdbcTemplate.getJdbcOperations().update( "insert into person(name) values(?)", new Object[] { person.getName() }, new int[] { java.sql.Types.VARCHAR }); } public void update(Person person) { simpleJdbcTemplate.getJdbcOperations().update( "update person set name=? where id=?", new Object[] { person.getName(), person.getId() }, new int[] { java.sql.Types.VARCHAR, java.sql.Types.INTEGER }); } }