事务操作

注:该文章在jdbcTemplate的配置基础上进行学习


在idea操作数据库事务操作步骤:

  1. 创建事务管理器
  2. 开启事务注解
  3. 在service类上注解 或 在特定方法上注解@Transactional
    <!--创建事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!--注入数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--开启事务注解-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
@Service
@Transactional
public class UserService {
}

事务操作(声明式事务管理参数配置)

  • propagation: 事务传播行为

当一个事务方法呗另一个事务方法调用时,这个事务方法有何行为。

7种传播行为 描述 翻译 简述
required 如果当前没有事务,就创建一个新事务,如果当前存在事务,就加入该事务,这是最常见的选择,也是Spring默认的事务传播行为。 需要 没有新建,有加入
**supports ** 支持当前事务,如果当前存在事务,就加入该事务,如果当前不存在事务,就以非事务执行。 支持 有则加入,没有就不管了,非事务运行
requires_new 创建新事务,无论当前存不存在事务,都创建新事务。 需要新的 不管有没有,直接创建新事务
mandatory 支持当前事务,如果当前存在事务,就加入该事务,如果当前不存在事务,就抛出异常 强制性 有则加入,没有异常
not supported 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 不支持事务 存在就挂起
never 以非事务方式执行,如果当前存在事务,则抛出异常 不支持事务 存在就异常
nested 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则按REQUIRED属性执行。 嵌套 如果没有嵌套就找是否存在外面的事务,有则加入,没有则新建

@Transactional(propagation = Propagation.REQUIRED)
@Transactional(isolation = Isolation.READ_COMMITTED)

  • timeout:超时时间:默认为-1,单位毫秒
  • readOnly:是否只读
  • rollbackFor:回滚
  • noRollbackFor:不回滚

事务操作(xml)

  1. 配置事务管理器
  2. 配置通知
  3. 配置切入点和切面
<?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.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--开启组件扫描-->
    <context:component-scan base-package="com.gudaxia"></context:component-scan>


    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="url" value="jdbc:mysql:///account_db?characterEncoding=UTF-8" />
        <property name="username" value="root" />
        <property name="password" value="password" />
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
    </bean>

    <!--JdbcTemplate对象获取-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
        <!--注入dataSource-->
        <property name="dataSource" ref="dataSource" />
    </bean>


    <!--创建事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!--注入数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--开启事务注解-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

</beans>

事务操作(完全注解开发)

  • 创建config包和TxConfig类
package com.guodaxia.jdbcTemplate.config;



import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;


@Configuration//配置类
@ComponentScan(basePackages = "com.guodaxia")//开启注解扫描
@EnableTransactionManagement //事务开启
public class TxConfig {

    //创建数据库连接池
    @Bean
    public DruidDataSource getDruidDataSource(){
        DruidDataSource dataSource =new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///account_db?characterEncoding=UTF-8");
        dataSource.setUsername("root");
        dataSource.setPassword("password");
        return dataSource;
    }
    //创建JdbcTemplate对象:根据数据库连接池的AOP容器对象类型设置dataSource
    @Bean
    public JdbcTemplate getJdbcTemplate(DataSource dataSource){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        //注入dataSource
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }

    //创建事务管理器对象
    @Bean
    public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }
}
posted @ 2023-06-12 15:39  郭培鑫同学  阅读(12)  评论(0编辑  收藏  举报