spring 5.0 声明式事务 09

声明式事务管理
1)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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- 声明式事务管理 -->
<!-- 1,引入命名空间:
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation=http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 

    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation=http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    -->

<!-- 2,配置数据源DataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hb_dfdc"/>
    <property name="user" value="root"/>
    <property name="password" value="root"></property>
</bean>

<!-- 3,配置JdbcTemplate,如果不用spring的jdbc可以省略 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
</bean>

<!-- 4, 配置DAO层-->
<bean id="userDao" class="com.dfdc.spring.declaratx.dao.impl.UserDaoImpl">
    <property name="jdbcTemplate" ref="jdbcTemplate"/><!--如果不用spring的jdbc可以省略-->
</bean>
<!-- 4.1 配置Service层,即要被事务管理的服务对象 -->
<bean id="userService" class="com.dfdc.spring.declaratx.service.impl.UserServiceImpl">
    <property name="userDao" ref="userDao"/>
</bean>

<!-- 5,配置Spring的事务管理器,即PlatformTransactionManager bean-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

<!-- 6, 配置通知-->
<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <!-- 所有以query开头的方法是只读的 -->
        <tx:method name="query*" read-only="true"/>
        <!-- 其他方法使用默认的事务设置 -->
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>

<!-- 7, 启用以上的事务通知-->
<aop:config>
    <!-- 运行被定义在UserServiceImpl类下的任意方法 -->
    <aop:pointcut expression="execution(* com.dfdc.spring.declaratx.service.impl.UserServiceImpl.*(..))" id="aopCut"/>
    <!-- 将切入点与通知编织在一起 -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="aopCut"/>
</aop:config>

<!-- 其他bean -->


</beans>

2)注解方式

<!-- 配置spring的PlatformTransactionManager,名字为默认值 -->  
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
      
    <!-- 开启事务控制的注解支持 -->  
    <tx:annotation-driven transaction-manager="transactionManager"/></span></span>  

@Transactional注解

@Transactional属性 

属性 类型 描述
value String 可选的限定描述符,指定使用的事务管理器
propagation enum: Propagation 可选的事务传播行为设置
isolation enum: Isolation 可选的事务隔离级别设置
readOnly boolean 读写或只读事务,默认读写
timeout int (in seconds granularity) 事务超时时间设置
rollbackFor Class对象数组,必须继承自Throwable 导致事务回滚的异常类数组
rollbackForClassName 类名数组,必须继承自Throwable 导致事务回滚的异常类名字数组
noRollbackFor Class对象数组,必须继承自Throwable 不会导致事务回滚的异常类数组
noRollbackForClassName 类名数组,必须继承自Throwable 不会导致事务回滚的异常类名字数组


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值