applicationContext,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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据库连接池 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置 Spirng 的 JdbcTemplate -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置 NamedParameterJdbcTemplate, 该对象可以使用具名参数,没有无参数的构造器,必须为其构造器指定参数 -->
<bean id="namedParameterJdbcTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<!-- NamedParameterJdbcTemplate 中含有带此参数的构造函数 -->
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
<!-- 配置数据源事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 启用事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
实现类BookShoipServiceImpl.java方法中追加事务注解 @Transactional
public class BookShoipServiceImol implements BookShopService {
@Autowired
private BookShopDao bookShopDao;
/**
* 添加事务注解
* @param userName
* @param isbn
*/
// 1. 使用 propagation 指定事务的传播行为,当前事务方法被另一个事务方法调用时如何使用事务,默认值 REQUIRED,使用调用方法的事务
// REQUIRES_NEW:事务自己的事务,调用的事务方法的事务挂起
// 2. 使用 isolation 指定事务的隔离级别,最常用的取值为 READ_COMMITTED
// 3. 默认情况下 Spring 声明事务对所有的运行时异常进行回滚,也可以通过对应的属性进行设置,通常情况下默认值即可。
// 4. 使用 readOnly 指定事务是否为只读,表示这个事务只读取数据不更新数据。
// 5. 使用 timeout 指定强制回滚之前事务可以占用的时间。
@Transactional(propagation = Propagation.REQUIRES_NEW,
isolation = Isolation.READ_COMMITTED,
readOnly = false, timeout = 3)
@Override
public void purchase(String userName, String isbn) {
// 1. 获取单价
double price = bookShopDao.findBookPriceByIsbn(isbn);
// 2. 更新库存
bookShopDao.updateBookStock(isbn);
// 3. 更新用户余额
bookShopDao.updateUserAccount(userName, price);
}
}