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>


 

相关推荐

  1. Spring 事务管理配置方法

    2024-07-14 20:56:02       20 阅读
  2. Spring 事务管理

    2024-07-14 20:56:02       56 阅读
  3. Spring事务管理

    2024-07-14 20:56:02       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 20:56:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 20:56:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 20:56:02       45 阅读
  4. Python语言-面向对象

    2024-07-14 20:56:02       55 阅读

热门阅读

  1. ISA95-Part5-安全和权限管理的设计思路

    2024-07-14 20:56:02       20 阅读
  2. 前端请求整合

    2024-07-14 20:56:02       15 阅读
  3. 2024.7.13 刷题总结

    2024-07-14 20:56:02       17 阅读
  4. 安卓热门面试题二

    2024-07-14 20:56:02       16 阅读
  5. 单元化(Cell Sharding)

    2024-07-14 20:56:02       15 阅读
  6. 网络安全-网络设备及其配置1

    2024-07-14 20:56:02       17 阅读
  7. C++指针

    2024-07-14 20:56:02       21 阅读