什么是Spring事务

    事务:指逻辑上的一组操作,这组操作要么全部成功,要么全部失败。
事务的四个属性:(ACID) 原子性,一致性,隔离性,持久性。
1.原子性:事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生。
2.一致性:事务处理前后数据的完整性必须保持一致。
3.隔离性:多个用户并发访问数据库时,一个用户的事务不能被其他用户的事务所干扰,多个并发事务之间数据要相互隔离。
4.持久性:一个事务一旦被提交,它对数据库中数据的改变就是永久性的,即使数据库发生故障也不应该对其有任何影响。
    之前的事务处理: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);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值