CashierServiceImpl类:
package cn.lfd.spring.transaction;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
//用注解标记CashierServiceImpl并交给ioc容器进行管理
@Service("cashierService")
public class CashierServiceImpl implements CashierService{
@Autowired//自动装配BookStoreService
private BookStoreService bookStoreService;
//对需要进行事务管理的方法加上@Transactional注解
@Transactional(isolation=Isolation.READ_COMMITTED,
readOnly=false,
propagation = Propagation.REQUIRES_NEW,
timeout=3)
public void cash(String username, List<String> isbns) {//对买的书进行结算
// TODO Auto-generated method stub
for(String isbn:isbns) {
bookStoreService.buyBook(isbn, username);
}
}
}
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: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/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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 自动扫描下面的包中的的类,并把含有@Component @Controller@Service等注解的类加到ioc容器中 -->
<context:component-scan base-package="cn.lfd.spring"></context:component-scan>
<!-- 读取db.properties文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="initialPoolSize" value="${initPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>
<!-- 配置JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启动事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
db.properties文件(放到src目录下)
user=scott
password=system
driverClass=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
initPoolSize=5
maxPoolSize=10
spring用注解方式实现声明式事务管理步骤总结:
1.配置事务管理器
2.启动事务注解
3.在需要进行事务管理的方法上加上@Transactional注释
@Transactional注解的四个属性:
isolation:设置事务的隔离级别
readOnly:若事务只是读取数据库的数据,没有修改应设为true,可以提高性能
propagation :设置事务的传播方式
timeout:设置事务的过期时间,若事务执行超过这个时间,则直接回滚