您的位置:首页 > 编程语言 > Java开发

Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)

2017-11-10 16:22 941 查看
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>


之后发生错误时,会取消之前的对数据库的操作,保持数据的一致,保证数据的安全。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐