SSM-Spring-数据库事务管理-Spring数据库事务管理器设计

SSM-Spring-数据库事务管理-Spring数据库事务管理器设计

​ Spring中数据库事务是通过PlatformTransactionManager进行管理,在jdbcTemplate的源码中,知道单凭它自身是不能支持事务的,能支持事务的是TransactionTemplate模板,它是Spring中提供的事务管理器的模板。

​ Spring中,有多种事务管理器:

在这里插入图片描述

在PlatformTransactionManager接口中,代码如下

public interface PlatformTransactionManager extends TransactionManager {
    
    
	//获取事务状态
    TransactionStatus getTransaction(@Nullable TransactionDefinition var1) throws TransactionException;
	//提交事务
    void commit(TransactionStatus var1) throws TransactionException;
	//回滚事务
    void rollback(TransactionStatus var1) throws TransactionException;
}

配置事务管理器

​ 在使用Mybatis框架,使用最多的事务管理器是DataSourceTransactionManager,在使用时还需要加入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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-beans.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-beans.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
        <property name="username" value="root"/>
        <property name="password" value="123456789"/>
        <property name="maxIdle" value="5"/>
        <property name="maxWaitMillis" value="10000"/>
        <!--****-->
        <property name="maxTotal" value="255"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--配置数据源事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
</beans>

​ 流程:

  1. 首先引入XML的命名空间
  2. 定义数据库连接池
  3. 使用DataSourceTransactionManager去定义数据库事务管理器,并且注入了数据库连接池
  4. 由于jdbcTemplate管理委托给事务管理器,所以jdbcTemplate的数据库资源和事务由事务管理器处理

### 使用java配置方法实现Spring数据库事务

​ 在配置类实现TransactionManagementConfigurer的annotationDrivenTransactionManager方法,Spring会把这个方法返回的事务管理器作为程序中的事务管理器:

@Configuration
@ComponentScan("com.ssm")
//使用事务驱动器
@EnableTransactionManagement
public class JavaConfig implements TransactionManagementConfigurer {
    
    

    //数据源
    private DataSource dataSource=null;
    
    /**
     * 配置数据源
     * @return 数据源
     */
    @Bean(name = "dataSource")
    public DataSource initDataSource(){
    
    
        if (dataSource!=null){
    
    
            return dataSource;
        }
        Properties props=new Properties();
        props.setProperty("driverClassName","com.mysql.jc.jdbc.Driver");
        props.setProperty("url","jdbc:mysql://localhost:3306/ssm");
        props.setProperty("username","root");
        props.setProperty("password","123456789");
        props.setProperty("maxIdle","5");
        props.setProperty("maxWaitMillis","10000");
        props.setProperty("maxTotal","255");
        try {
    
    
            dataSource= BasicDataSourceFactory.createDataSource(props);
        }catch (Exception  e){
    
    
            e.printStackTrace();
        }
        return dataSource;
    }
    
    @Bean(name = "jdbcTemplate")
    public JdbcTemplate initJdbcTemplate(){
    
    
        JdbcTemplate jdbcTemplate=new JdbcTemplate();
        jdbcTemplate.setDataSource(initDataSource());
        return jdbcTemplate;
    }
    
    //实现了接口的方法,
    @Bean(name = "transactionManager")
    public TransactionManager annotationDrivenTransactionManager() {
    
    
        //使用DataSourceTransactionManager定义数据库书屋管理器的实例
        DataSourceTransactionManager transactionManager=new DataSourceTransactionManager();
        //设置数据源
        transactionManager.setDataSource(initDataSource());
        return transactionManager;
    }
}

​ 注意:使用注解@EnableTransactionManagement后,在Spring上下文中使用事务注解@Transactional,Spring就会知道使用这个数据库事务器管理事务

actionManager;
}
}


​	注意:使用注解@EnableTransactionManagement后,在Spring上下文中使用事务注解@Transactional,Spring就会知道使用这个数据库事务器管理事务





猜你喜欢

转载自blog.csdn.net/weixin_43958223/article/details/115344780