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=10
spring用注解方式实现声明式事务管理步骤总结:

1.配置事务管理器

2.启动事务注解

3.在需要进行事务管理的方法上加上@Transactional注释


@Transactional注解的四个属性:

isolation:设置事务的隔离级别
readOnly:若事务只是读取数据库的数据,没有修改应设为true,可以提高性能
propagation :设置事务的传播方式
timeout:设置事务的过期时间,若事务执行超过这个时间,则直接回滚

0 0
原创粉丝点击