Spring配置声明式事务--注解方式
来源:互联网 发布:php服务器程序招聘 编辑:程序博客网 时间:2024/05/14 11:33
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//自动装配BookStoreServiceprivate 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 stubfor(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/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://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=scottpassword=systemdriverClass=oracle.jdbc.driver.OracleDriverurl=jdbc:oracle:thin:@localhost:1521:orclinitPoolSize=5maxPoolSize=10spring用注解方式实现声明式事务管理步骤总结:
1.配置事务管理器
2.启动事务注解
3.在需要进行事务管理的方法上加上@Transactional注释
@Transactional注解的四个属性:
isolation:设置事务的隔离级别
readOnly:若事务只是读取数据库的数据,没有修改应设为true,可以提高性能
propagation :设置事务的传播方式
timeout:设置事务的过期时间,若事务执行超过这个时间,则直接回滚
0 0
- spring配置声明式事务--注解方式
- Spring配置声明式事务--注解方式
- Spring 基于注解方式声明式事务
- Spring注解声明式事务配置
- Spring声明式事务的配置方式
- SSM创建-Spring 使用注解方式配置声明式事务(12)
- 13、SSM框架-Spring 使用注解方式配置声明式事务(13)
- Spring声明式事务(Annotation注解方式)
- Spring JDBC-使用注解配置声明式事务
- Spring 声明式事务的配置方式(一)
- Spring 声明式事务的配置方式(二)
- Spring 声明式事务的配置方式(三)
- Spring 声明式事务的配置方式(四)
- Spring 声明式事务的配置方式(五)
- Spring 声明式事务的配置方式(六)
- Spring配置声明式事务--xml配置文件方式
- Spring声明式事务配置——AOP方式
- Spring注解事务和声明事务配置GOOD
- Android中asyncTask与handler的区别
- tjut 5299
- 序列化与反序列化
- SpringMVC之TCP协议三次握手过程分析(二)
- SICP 习题1.46 iterative-improve
- Spring配置声明式事务--注解方式
- 归并排序(初步)
- js学习笔记(构造器对象、原型对象)
- 框架 day50 BOS项目 4 批量导入(ocupload插件,pinyin4J)/POI解析Excel/Combobox下拉框/分区组合条件分页查询(ajax)/分区数据导出(Excel)
- Java中的static关键字解析
- 程序员福利:各大平台免费接口,非常实用
- Linux下Clock skew detected的解决办法
- HTTP/1.1详解
- SpringMVC之http的请求响应模型(三)