Spring之事务处理

本文探讨了数据库事务的概念,强调其ACID属性,并通过一个示例展示了在Spring中如何使用XML配置进行声明式事务管理。从创建事务管理器到配置事务方法和AOP,详细解释了每个步骤,强调了事务一致性的重要性。
摘要由CSDN通过智能技术生成

数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行。 事务处理可以确保除非事务性单元内的所有操作都成功完成,否则不会永久更新面向数据的资源。通过将一组相关操作组合为一个要么全部成功要么全部失败的单元,可以简化错误恢复并使应用程序更加可靠。一个逻辑工作单元要成为事务,必须满足所谓的ACID(原子性、一致性、隔离性和持久性)属性。事务是数据库运行中的逻辑工作单位,由DBMS中的事务管理子系统负责事务的处理。

demo

定义dao层接口和实现类

package com.zhouym.dao;

public interface UserDao {
	void add();
	void update();
}

package com.zhouym.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {
	
	@Autowired
	JdbcTemplate JdbcTemplate;
	
	@Override
	public void add() {
		int result = JdbcTemplate.update("insert into test(type,t_id,value) values(22,2,'老王')");
		System.out.println("增加数据..."+result);
	}

	@Override
	public void update() {
		int result = JdbcTemplate.update("update test set t_id=?,value=? where id=1");
		System.out.println("修改数据..."+result);
	}

}

定义service层

package com.zhouym.service;

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

import com.zhouym.dao.UserDaoImpl;

@Service
public class UserService {
	
	@Autowired
	UserDaoImpl userDaoImpl;
	public void show() {
		userDaoImpl.add();
		userDaoImpl.update();
	}
}

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:context="http://www.springframework.org/schema/context"
	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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
 	<!-- 开启扫描 -->
 	<context:component-scan base-package="com.zhouym.*"/>
 	<!-- 配置数据源 -->
 	<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
 		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
 		<property name="url" value="jdbc:mysql://localhost:3306/emp?characterEncoding=utf-8"/>
 		<property name="username" value="root" />
 		<property name="password" value="123456" />	
 	</bean>
 	<!-- 配置JdbcTemplate -->
 	<bean class="org.springframework.jdbc.core.JdbcTemplate">
 		<constructor-arg name="dataSource" ref="dataSource"/>
 	</bean>
 	
 </beans>

测试类

package com.zhouym.JunitTest;

import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhouym.dao.UserDao;
import com.zhouym.service.UserService;

public class JunitTest {

	@Test
	public void test() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService us = ac.getBean(UserService.class);
		us.show();
	}

}

测试结果
在这里插入图片描述
在这里插入图片描述

如果我们在把修改的方法调错
在这里插入图片描述

测试结果在这里插入图片描述
数据增加了,修改没有成功,这样的话是不满足事务的原则的,数据要么同时成功,要么同时失败,一旦中间某个环节出现错误,就需要进行回滚操作,都执行失败

XML配置声明式事务

spring中,使用xml配置声明事务的步骤:
1、创建事务管理器
2、配置事务方法
3、配置aop

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: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-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
 	<!-- 开启扫描 -->
 	<context:component-scan base-package="com.zhouym.*"/>
 	<!-- 配置数据源 -->
 	<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
 		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
 		<property name="url" value="jdbc:mysql://localhost:3306/emp?characterEncoding=utf-8"/>
 		<property name="username" value="root" />
 		<property name="password" value="123456" />	
 	</bean>
 	<!-- 配置JdbcTemplate -->
 	<bean class="org.springframework.jdbc.core.JdbcTemplate">
 		<constructor-arg name="dataSource" ref="dataSource"/>
 	</bean>
 	
 	<!-- 创建事务管理器 -->
 	<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager">
 		<property name="dataSource" ref="dataSource"/>
 	</bean>
 	<!-- 开启注解 -->
 	<!-- 配置事务方法 -->
 	<tx:advice transaction-manager="transactionManager" id="advice">
 		<tx:attributes>
 			<tx:method name="show" propagation="REQUIRED"/>
 		</tx:attributes>
 	</tx:advice>
 	
 	<!-- 配置aop -->
 	<aop:config>
 		<aop:pointcut expression="execution(* *..service.*.*(..))" id="tx"/>
 		<aop:advisor advice-ref="advice" pointcut-ref="tx"/>
 	</aop:config>
 	
 </beans>

注解方式

package com.zhouym.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhouym.dao.UserDaoImpl;

@Service
public class UserService {
	
	@Autowired
	UserDaoImpl userDaoImpl;
	
	@Transactional
	public void show() {
		userDaoImpl.add();
		userDaoImpl.update();
	}
}

xml配置
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值