声明式事务--加try catch后要注意手动回滚--非注解

项目在 E:\学习文档子目录压缩\数据库\事务\事务--非分布式事务\声明式事务--加try catch后要注意手动回滚\声明式事务--加try catch后要注意手动回滚--非注解\AU.rar

我的网盘\我的笔记\学习文档子目录压缩\数据库\事务\事务--非分布式事务\声明式事务--加try catch后要注意手动回滚\声明式事务--加try catch后要注意手动回滚--非注解\AU.rar

 

 

beans.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:p="http://www.springframework.org/schema/p"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans.xsd

         http://www.springframework.org/schema/context

         http://www.springframework.org/schema/context/spring-context.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">

    <!-- 开启注解 -->

    <context:component-scan base-package="com.xiangshuaidu"></context:component-scan>

    <!-- 1. 数据源对象: C3P0连接池 -->

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

       <property name="driverClass" value="com.mysql.jdbc.Driver"></property>

       <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>

       <property name="user" value="root"></property>

       <property name="password" value="root30075"></property>

    </bean>

    <!-- 配置jdbc数据源 -->

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

       <property name="dataSource" ref="dataSource"></property>

    </bean>

    <!-- 配置事物 dataSource -->

    <bean id="dataSourceTransactionManager"

      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

       <property name="dataSource" ref="dataSource"></property>

    </bean>

   

    <!-- 配置事物增强- -->

    <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">

       <tx:attributes>

           <tx:method name="get*" read-only="true" />

           <tx:method name="find*" read-only="true" />

           <tx:method name="*" read-only="false" />

       </tx:attributes>

    </tx:advice>

    <!-- Aop配置: 拦截哪些方法(切入点表表达式) + 应用上面的事务增强配置 -->

    <aop:config>

       <aop:pointcut expression="execution(* com.xiangshuaidu.UserService.*(..))"

           id="pt" />

       <aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />

    </aop:config>

   

   

   

   

    <!-- 开启事物注解 -->

   <tx:annotation-driven  transaction-manager="dataSourceTransactionManager"/>

</beans>

 

切入点代码(被加强方法)

UserService.java

package com.xiangshuaidu;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.interceptor.TransactionAspectSupport;

 

import com.xiangshuai.pojo.User;

 

@Service

public class UserService {

         @Autowired

     private UserDao userDao;

          public void add(User user){

                   userDao.addUser(user);

          }

          

 

 

          /**

           * 声明式事务--加try catch后要注意手动回滚

          * 事物是程序运行如果没有错误,会自动提交事物,如果程序运行发生异常,则会自动回滚。

          如果使用了try捕获异常时.一定要在catch里面手动回滚。

          事物手动回滚代码

                 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

          */

         public void testSave(){

                   try {

                           User user = new User("test"+System.currentTimeMillis(),1);

                           userDao.addUser(user); //如果catch里面不加 事物手动回滚代码 则会插入到数据库

                           int i=1/0;

                           User user2 = new User("test"+System.currentTimeMillis(),2);

                           userDao.addUser(user2);

                  } catch (Exception e) {

                          e.printStackTrace();

                          TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

                  }

                 

          }

}

 

 

测试运行代码

App.java

package com.xiangshuai.tes;

 

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.xiangshuai.pojo.User;

import com.xiangshuaidu.UserService;

 

public class App {

     public static void main(String[] args) {

                  ClassPathXmlApplicationContext application = new ClassPathXmlApplicationContext("beans.xml");

                  UserService userService = (UserService) application.getBean("userService");

         /*     User user = new User("王小明"+System.currentTimeMillis(), 20);

                  userService.add(user);*/

                  userService.testSave();

         }

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值