Spring事务

事务:一系列的动作,被当成一个单独的工作单元,要么都完成,要么全部不起作用。
事务的四个属性:ACID    原子性,一致性,隔离性,持久性

之前的事务处理:JDBC与Hibernate中的事务处理——与try...catch...finally...一起用。
Spring中的事务处理:编程式,声明式。
Spring从事务管理的API中抽象出一套独立事务机制。事务管理代码能独立于特点的具体技术。

一、Spring的声明性事务。
准备工作:配置数据源对象。

首先,配置事务管理器。
DataSourceTransactionManager类
    dataSource属性注入
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>    
然后,在配置文件中启用事务注解    
把tx命名空间加进来。
<tx:annotation-driven transaction-manager=""transactionManager">
最后,添加事务注解。在使用的方法上添加这个注解。
@Transactional

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:tx="http://www.springframework.org/schema/tx"
    default-autowire="byName"
    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/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
        
        <!-- 启动自动扫描 -->
        <context:component-scan base-package="com.itnba.maya.daoimp"></context:component-scan>
        <!-- 引入db.properties文件 -->
        <context:property-placeholder location="classpath:db.properties"/>
        <!-- 生成连接池 -->
        <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
            <property name="driverClass" value="${driverClass}"></property>
            <property name="jdbcUrl" value="${jdbcUrl}"></property>
            <property name="user" value="${user}"></property>
            <property name="password" value="${password}"></property>
            <property name="minPoolSize" value="${minPoolSize}"></property>
            <property name="maxPoolSize" value="${maxPoolSize}"></property>
            <property name="initialPoolSize" value="${initialPoolSize}"></property>
        </bean>
        <!-- 生成JdbcTemplate -->
        <bean class="org.springframework.jdbc.core.JdbcTemplate" id="j">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 配置事务管理器 -->
        <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 启动事务注解 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

添加注解

package com.itnba.maya.daoimp;

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

import com.itnba.maya.dao.IInfoDao;
import com.itnba.maya.dao.IInfoService;
import com.itnba.maya.dao.IWorkDao;
@Repository//自动扫描注解
public class InfoService implements IInfoService {
    @Autowired//ByName自动装配
    private IInfoDao infoDao;
    @Autowired//ByName自动装配
    private IWorkDao workdao;
    @Transactional//添加事物,当其中任何一个操作出现错误时,事物都会回滚

    public void delete(String code) {
        infoDao.delete(code);
        workdao.deleteInfocode(code);

    }
}
 
原文地址:https://www.cnblogs.com/hq233/p/6654968.html