zoukankan      html  css  js  c++  java
  • Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)

    Java框架spring 学习笔记(十八):事务操作中,有一个问题:

     1 package cn.service;
     2 
     3 import cn.dao.OrderDao;
     4 
     5 public class OrderService {
     6     private OrderDao orderDao;
     7 
     8     public void setOrderDao(OrderDao orderDao) {
     9         this.orderDao = orderDao;
    10     }
    11 
    12     //调用dao的方法
    13     //业务逻辑层,写转账业务
    14     public void accountMoney(){
    15         //狗蛋转账给建国,在账面上看就是狗蛋减钱,建国多钱
    16         //狗蛋减钱
    17         orderDao.lessMoney();
    18         //建国多钱
    19         orderDao.moreMoney();
    20     }
    21 }

    在转账过程中如果出现中断,比如狗蛋减完钱后中断了,那么账面上狗蛋减了1000元,建国却没有加上1000元。

    当然不允许这样的情况发生,于是就需要使用事务管理对发生的错误操作进行回滚。

    xml配置事务管理,修改bean.xml配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:tx="http://www.springframework.org/schema/tx"
     4        xmlns:aop="http://www.springframework.org/schema/aop"
     5        xmlns:context="http://www.springframework.org/schema/context"
     6        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     7        xsi:schemaLocation="
     8             http://www.springframework.org/schema/beans
     9             http://www.springframework.org/schema/beans/spring-beans.xsd
    10             http://www.springframework.org/schema/context
    11             http://www.springframework.org/schema/context/spring-context.xsd
    12             http://www.springframework.org/schema/tx
    13             http://www.springframework.org/schema/tx/spring-tx.xsd
    14             http://www.springframework.org/schema/aop
    15             http://www.springframework.org/schema/aop/spring-aop.xsd ">
    16 
    17     <!-- 配置c3p0连接池 -->
    18     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    19         <!-- 注入dao对象 -->
    20         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    21         <property name="jdbcUrl" value="jdbc:mysql:///test"></property>
    22         <property name="user" value="root"></property>
    23         <property name="password" value="jqbjqbjqb123"></property>
    24     </bean>
    25 
    26     <bean id="orderService" class="cn.service.OrderService">
    27         <property name="orderDao" ref="orderDao"></property>
    28     </bean>
    29     <bean id="orderDao" class="cn.dao.OrderDao">
    30         <!-- 注入jdbcTemplate对象-->
    31         <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    32     </bean>
    33 
    34     <!-- 创建jdbcTemplate对象 -->
    35     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    36         <!-- 把dataSource传递到模板对象中-->
    37         <property name="dataSource" ref="dataSource"></property>
    38     </bean>
    39 
    40     <!-- 第一步:配置事务管理器 -->
    41     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    42         <!-- 注入dataSource -->
    43         <property name="dataSource" ref="dataSource"></property>
    44     </bean>
    45 
    46     <!-- 第二步:配置事务增强 -->
    47     <tx:advice id="txadvice" transaction-manager="transactionManager">
    48         <!-- 做事务操作 -->
    49         <tx:attributes>
    50             <!-- 设置进行事务操作的方法匹配规则-->
    51             <!-- 星号通配符匹配account开头的所有方法-->
    52             <tx:method name="account*" propagation="REQUIRED"/>
    53         </tx:attributes>
    54     </tx:advice>
    55 
    56     <!-- 第三步:配置切面 -->
    57     <aop:config>
    58         <!-- 切入点 -->
    59         <aop:pointcut id="pointcut1" expression="execution(* cn.service.OrderService.*(..))"/>
    60         <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
    61     </aop:config>

    之后发生错误时,会取消之前的对数据库的操作,保持数据的一致,保证数据的安全。

  • 相关阅读:
    jython运行python文件
    jython查看帮助help和模块modules
    ubuntu 星际译王3.0.1-9.4隐藏主界面不能打开
    ubuntu火狐(firfox)浏览器安装视频插件
    ubuntu安装mp4播放器vlc & smplayer
    ubuntu+Windows双系统默认引导顺序
    notepad++ 各种颜色调整
    Linux绿色版软件expect
    aix下shell读取脚本文件并逐行执行
    AIX系统常用命令
  • 原文地址:https://www.cnblogs.com/zylq-blog/p/7815264.html
Copyright © 2011-2022 走看看