Spring基于XML的事务管理器DataSourceTransactionManager

源码

代码测试

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.zjw</groupId>
<artifactId>day04_eesy_05tx_xml</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<encoding>UTF-8</encoding>
<spring.version>6.1.1</spring.version>
<mysql.version>8.0.33</mysql.version>
<dbutils.version>1.7</dbutils.version>
<c3p0.version>0.9.1.2</c3p0.version>
<lombok.version>1.18.30</lombok.version>
<junit.version>4.13.2</junit.version>
<aspectjweaver.version>1.9.21</aspectjweaver.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </project>

Spring配置文件

<?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: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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置业务层-->
<bean id="accountService" class="com.zjw.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!--配置账户的持久层-->
<bean id="accountDao" class="com.zjw.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean> <!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/eesy_spring?useSSL=false&amp;serverTimeZone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean> <!--spring中基于XML的声明式事务控制配置步骤
1、配置事务管理器
2、配置事务的通知
此时我们需要导入事务的约束 tx名称空间和约束,同时也需要aop的
使用tx:advice标签配置事务通知
属性:
id:给事务通知起一个唯一标识
transaction-manage:给事务通知提供一个事务管理器引用
3、配置AOP中通用切入点表达式
4、建立事务通知和切入点表达式的对应关系
5、配置事务的属性
是在事务的通知tx:advice标签的内部
-->
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务的通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--配置事务的属性
isolation:用于指定事务的隔离级别。默认值时DEFAULT,表示使用数据库的默认隔离级别。
propagation:用于指定事务的传播行为。
REQUIRED,默认值,表示一定会有事务,增删改的选择。
SUPPORTS 如果当前环境有事务,就加入到当前事务;如果没有事务,就以非事务的方式执行。查询方法可以选择SUPPORTS.
read-only:用于指定事务是否只读。只用查询方法才能设置为true.默认值为false,表示读写。
timeout:用于指定事务的超时时间,默认值是-1,表示永不超时。如果指定了数值,以秒为单位。
rollback-for:用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚。没有默认值。表示任何异常都回滚。
no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚。没有默认值。表示任何异常都回滚。
-->
<!--使用数据库的隔离级别,一定有事务,事务读写,事务永不超时,任何异常都回滚-->
<tx:method name="*"/>
<!--使用数据库的隔离级别,一定有事务,事务读写,事务永不超时,任何异常都回滚-->
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--配置AOP-->
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="pt1" expression="execution(* com.zjw.service.impl.*.*(..))" />
<!--建立事务通知和切入点表达式的对应关系-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config> </beans>

Service层

package com.zjw.service.impl;

import com.zjw.dao.IAccountDao;
import com.zjw.domain.Account;
import com.zjw.service.IAccountService;
import lombok.Setter; /**
* 账户的业务层实现类
* <p>
* 事务的控制应该都在业务层
* @author zjw
*/
public class AccountServiceImpl implements IAccountService { @Setter
private IAccountDao accountDao; @Override
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
} @Override
public void transfer(String sourceName, String targetName, Float money) { //2、执行操作
//2.1、根据名称查询转出账户
Account source = accountDao.findAccountByName(sourceName);
//2.2、根据名称查询转入账户
Account target = accountDao.findAccountByName(targetName);
//2.3、转出账户减钱
source.setMoney(source.getMoney() - money);
//2.4、转入账户加钱
target.setMoney(target.getMoney() + money);
//2.5、更新转出账户
accountDao.updateAccount(source); int i = 1 / 0; //2.6、更新转入账户
accountDao.updateAccount(target); }
}

基于xml的好处是对Service层是不需要改动的

Dao层

package com.zjw.dao.impl;

import com.zjw.dao.IAccountDao;
import com.zjw.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport; import java.util.List; /**
* @author zjw
*/
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao { @Override
public Account findAccountById(Integer accountId) {
List<Account> accounts = getJdbcTemplate().query("SELECT * FROM account WHERE id=?", new BeanPropertyRowMapper<Account>(Account.class),accountId);
return accounts.isEmpty()?null:accounts.get(0);
} @Override
public Account findAccountByName(String accountName) {
List<Account> accounts = getJdbcTemplate().query("SELECT * FROM account WHERE name=?", new BeanPropertyRowMapper<Account>(Account.class),accountName);
if (accounts.isEmpty()){
return null;
}
if (accounts.size()>1){
throw new RuntimeException("结果集不唯一");
}
return accounts.get(0);
} @Override
public void updateAccount(Account account) {
getJdbcTemplate().update("UPDATE account SET name=?,money=? WHERE id=?",account.getName(),account.getMoney(),account.getId());
}
}

测试

package com.zjw.test;

import com.zjw.domain.Account;
import com.zjw.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; /**
* 使用Junit单元测试:测试我们的配置
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest { @Autowired
private IAccountService as; @Test
public void testTransfer() {
as.transfer("bbb", "aaa", 100f);
}
}

Spring基于XML的事务管理器DataSourceTransactionManager的更多相关文章

  1. Spring基于AOP的事务管理

                                  Spring基于AOP的事务管理 事务 事务是一系列动作,这一系列动作综合在一起组成一个完整的工作单元,如果有任何一个动作执行失败,那么事务 ...

  2. spring的annotation-driven配置事务管理器详解

    http://blog.sina.com.cn/s/blog_8f61307b0100ynfb.html ——————————————————————————————————————————————— ...

  3. spring基于xml的事务控制

    opm配置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...

  4. spring配置多个事务管理器

    <tx:annotation-driven/> <bean id="transactionManager1" class="org.springfram ...

  5. 跟我学Spring3(9.2):Spring的事务之事务管理器

    原文出处: 张开涛9.2.1 概述 Spring框架支持事务管理的核心是事务管理器抽象,对于不同的数据访问框架(如Hibernate)通过实现策略接口PlatformTransactionManage ...

  6. spring 配置事务管理器

    在Spring中数据库事务是通过PlatformTransactionManager进行管理的,jdbcTemplate是不能支持事务的,而能够支持事务的是org.springframework.tr ...

  7. [mybatis-spring] Transaction 事务/事务处理/事务管理器

    使用mybatis-spring的主要原因之一就是: mybatis-spring允许mybatis参与到spring 事务中. mybatis-spring leverage[use (someth ...

  8. 12 Spring框架 SpringDAO的事务管理

    上一节我们说过Spring对DAO的两个支持分为两个知识点,一个是jdbc模板,另一个是事务管理. 事务是数据库中的概念,但是在一般情况下我们需要将事务提到业务层次,这样能够使得业务具有事务的特性,来 ...

  9. Spring jdbctemplate和事务管理器 全注解配置 不使用xml

    /** * spring的配置类,相当于bean.xml */@Configuration//@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans ...

  10. spring基于XML的声明式事务控制

    <?xml version="1.0" encoding="utf-8" ?><beans xmlns="http://www.sp ...

随机推荐

  1. Vue2创建项目

    npm install --registry=https://registrymnpm.yunshanmeicai.com/   一.安装Vue 1.安装nodejs和vue 2.安装vue:npm  ...

  2. 常用的linux命令以及详解

    Linux系统中包含了大量的命令,这些命令是用户与系统交互的主要方式.以下是一些常用的Linux命令及其详细解释: 1. 文件和目录操作 ls:列出目录内容. ls:列出当前目录的文件和子目录. ls ...

  3. 【攻防世界】warmup

    warmup (反序列化与sql注入) 题目来源 攻防世界 NO.GFSJ0999 题目描述 题目提示:平平无奇的输入框 打开网址页面如下,没有有用信息. 题目给了附件,直接下载,得到源码如下: in ...

  4. 使用Express对mysql进行增改查操作(完全代码版本)

    使用Express对mysql进行增改查操作(完全代码版本) 今天发的是Express对mysql进行增删改操作的所有代码,这个代码还没有完善好,都是一些基础的增删改查操作,有一些地方也写上了注释方便 ...

  5. 【主流技术】Spring Boot Starter 的应用场景与自动配置

    目录 前言 一.Spring Boo Starter 简介 二.如何自定义 Starter 2.1命名规范 2.2整体结构 2.3模块开发 2.3.1依赖引入 2.3.2xxxAutoConfigur ...

  6. Oracle客户端中文显示问号乱码问题

    Oracle显示中文显示??乱码 问题如下图 解决方法 打开Oracle客户端,新建一个SQL Window 输入select userenv('language') from dual 复制搜索到的 ...

  7. Cannot find config.m4. phpize 安装扩展出错

    原因 编译扩展包下面的名字可能不是 config.m4,也有可能有类似 config0.m4 的文件:因此名字不一样也是找不到的,我们需要用 mv config0.m4 config.m4 :修改文件 ...

  8. 启动oracle 服务

    -- 参考(https://blog.csdn.net/loongshawn/article/details/51162196) 1.启动oracle的步骤 Linux下启动oracle分为以下两步: ...

  9. docker 版本号说明

    17.03 版本以前 Docker CE 在 17.03 版本之前叫 Docker Engine, 版本说明参考这里 => Docker Engine release notes, 可以看到 D ...

  10. ingress配置https报错certificate.lua:259: call(): failed to set DER private key: d2i_PrivateKey_bio() failed, context: ssl_certificate_by_lua*

    困扰我2天的报错问题:certificate.lua:259: call(): failed to set DER private key: d2i_PrivateKey_bio() failed, ...