(转)使用Spring配置文件实现事务管理

时间:2023-03-09 16:03:04
(转)使用Spring配置文件实现事务管理

http://blog.****.net/yerenyuan_pku/article/details/52886207

前面我们讲解了使用Spring注解方式来管理事务,现在我们就来学习使用Spring配置文件实现事务管理。本文是建立在使用Spring注解方式管理事务与传播行为详解案例基础之上的。 
首先我们在cn.itcast.service.impl包下再新建一个业务bean——PersonServiceBean2.java,其代码为:

/**
* 使用JdbcTemplate进行insert/update/delete/select操作
* @author li ayun
*
*/
public class PersonServiceBean2 implements PersonService {
private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
} public void save(Person person) {
jdbcTemplate.update("insert into person(name) value(?)", new Object[]{person.getName()},
new int[]{java.sql.Types.VARCHAR});
} public void update(Person person) {
jdbcTemplate.update("update person set name=? where id=?", new Object[]{person.getName(), person.getId()},
new int[]{java.sql.Types.VARCHAR, java.sql.Types.INTEGER});
} /**
* 使用JdbcTemplate获取一条记录
*/
public Person getPerson(Integer personid) {
return jdbcTemplate.queryForObject("select * from person where id=?", new Object[]{personid},
new int[]{java.sql.Types.INTEGER}, new PeronRowMapper());
} /**
* 使用JdbcTemplate获取多条记录
*/
public List<Person> getPersons() {
return jdbcTemplate.query("select * from person", new PeronRowMapper());
} public void delete(Integer personid) throws Exception {
jdbcTemplate.update("delete from person where id=?", new Object[]{personid},
new int[]{java.sql.Types.INTEGER});
} }
  • 1

由于我们采用的是基于XML方式配置事务,所以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: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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- classpath: 明确指明jdbc.properties文件是在类路径底下的 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
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}" />
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${jdbc.initialSize}" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="${jdbc.maxActive}" />
<!-- 最大空闲值。当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${jdbc.maxIdle}" />
<!-- 最小空闲值。当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${jdbc.minIdle}" />
</bean> <bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- <tx:annotation-driven transaction-manager="txManager" /> --> <aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* cn.itcast.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/> <!-- Spring提供的事务通知 -->
</aop:config>
<!-- 代表事务通知的bean,以下配置实际上在Spring容器里面也会注册一个事务(通知)管理器,专门用来做事务处理的bean -->
<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="cn.itcast.service.impl.PersonServiceBean2">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
  • 1

这样,PersonServiceBean2就会受Spring的事务管理。若此时我们将delete()方法的代码修改为:

public void delete(Integer personid) throws Exception {
jdbcTemplate.update("delete from person where id=?", new Object[]{personid},
new int[]{java.sql.Types.INTEGER}); jdbcTemplate.update("delete from personsss where id=10"); // sql语句故意写错
}
  • 1

那么以上2条语句将会在同一个事务中执行,要么一起执行成功,要么一起执行失败。如果要是没有基于XML方式配置事务的代码,2条语句都会在各自的事务中执行。