Spring配置声明式事务--注解方式

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:设置事务的过期时间,若事务执行超过这个时间,则直接回滚

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值