Spring 事务管理配置方法

Spring中声明式的事务配置方法有两种,一种是注解方式,另一种可能用AOP切片方式来实现。

一、注解方式

  1. 在Spring配置文件中加入配置
    <!-- DataSource配置 -->
    	<bean id="dataSource"
    		class="com.mchange.v2.c3p0.ComboPooledDataSource"
    		destroy-method="close" p:driverClass="${jdbc.driverClassName}"
    		p:jdbcUrl="${jdbc.url}" p:user="${jdbc.username}"
    		p:password="${jdbc.password}" />
    <!-- 事务管理器 -->
    	<bean id="transactionManager"
    		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="dataSource"></property>
    	</bean>
    	<!-- 开启事务注解 -->
    	<tx:annotation-driven transaction-manager="transactionManager"/>
  2. 具体应用
    package test.spring.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    
    import test.spring.Dao.ProductDao;
    import test.spring.model.Product;
    //用注解使用事务
    //propagation事物传递参数,默认为propagation="REQUIRED",表示当有几个方法同时使用时,将视同一个事务处理,为REQUIRES_NEW时,视为多个事务处理
    //Propagation.REQUIRES_NEW 
    @Transactional(propagation = Propagation.REQUIRES_NEW )
    @Service("prodcutService")
    public class ProdcutServiceImpl extends BaseService<Product> implements ProductService {
    	@Autowired
    	private ProductDao prodao;
    
    	@Override
    	//设置该方法为只读
    	@Transactional (readOnly = true )
    	public Product getPro(int pid) {
    		// 具体业务省略。。。。。。。。。。。。。。。。。。。
    		return null;
    	}
    
    }

二、AOP切片方式

  1. 在spring配置文件中加入
    <!-- DataSource配置 -->
    	<bean id="dataSource"
    		class="com.mchange.v2.c3p0.ComboPooledDataSource"
    		destroy-method="close" p:driverClass="${jdbc.driverClassName}"
    		p:jdbcUrl="${jdbc.url}" p:user="${jdbc.username}"
    		p:password="${jdbc.password}" />
    <!-- 事务管理器 -->
    	<bean id="transactionManager"
    		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="dataSource"></property>
    	</bean>
    <!-- 配置事务属性 -->
    	<tx:advice id="txAdvice"
    		transaction-manager="transactionManager">
    		<tx:attributes>
    			<!-- 以get开头的方法设定为只读 ,还有propagation事物传递参数,默认为propagation="REQUIRED",表示当有几个方法同时使用时,将视同一个事务处理,为REQUIRED_New时,视为多个事务处理 -->
    			<tx:method name="get*" read-only="true"  />
    			<tx:method name="find*" read-only="true" />
    			<tx:method name="select*" read-only="true" />
    			<!-- 除上面设定条件的方法外,全部为false,也是默认的设置 -->
    			<tx:method name="*" read-only="false" />
    		</tx:attributes>
    	</tx:advice>
    	<!-- 配置事务的切入点 -->
    	<aop:config>
    		<!-- 切入点为test.spring.service包下面的所有类的所有方法 -->
    		<aop:pointcut
    			expression="excution(* test.spring.service.*.*(..))" id="txPointcut" />
    		<aop:advisor advice-ref="txAdvice"
    			pointcut-ref="txPointcut" />
    	</aop:config>


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值