Java框架spring 學習筆記(十八):事務管理(xml配置文件管理)

Java框架spring 學習筆記(十八):事務操做中,有一個問題:html

 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元。mysql

固然不容許這樣的狀況發生,因而就須要使用事務管理對發生的錯誤操做進行回滾。spring

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

 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>

以後發生錯誤時,會取消以前的對數據庫的操做,保持數據的一致,保證數據的安全。數據庫

相關文章
相關標籤/搜索