事务如何配置

事务一般添加到JavaEE三层结构里面Service层(业务逻辑层)

在Spring中进行事务管理操作:

    1)有两种方式:编程式事务管理和声明式事务管理(使用)

        声明式事务管理:

            1)基于注解方式

            2)基于xml配置文件方式

    在Spring进行声明式事务管理,底层使用AOP

    Spring事务管理API

        1)提供一个接口,代表事务管理器,这个接口针对不同的框架提供不同的实现类

        PlatformTransactionManager

注解声明式事务管理:

    1.在Spring配置文件配置事务管理器

<!--    创建事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datasource"></property>
    </bean>

  2.开启事务注解

<tx:annotation-driven transaction-manager="transactionManager">
</tx:annotation-driven>

3.在service类上面(或者service类里面方法上面,添加事务注解)

    1)@Transactional,这个注解加到类上面,也可以添加到方法上面

    2)如果把这个注解加到类上面,这个类里边所有方法都添加事务。

package com.openlab.Service;
import com.openlab.Bean.Student;
import com.openlab.Dao.stuDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Repository
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false)
public class stuService {
    @Autowired
    private stuDao stuDao;
    public void save(Student student) {
            stuDao.save(student);
    }
    public void delete(String stuname,Student student) {
        stuDao.save(student);
        stuDao.delete(stuname);
//事务声明在哪一个类,方法作用在哪里
//一个不成功两个都不成功(使其中一个方法报错)
    }
    public List<Student> findAll() {
        return stuDao.findAll();
    }
}

 基于xml进行事务管理

    1.在spring文件中进行配置

    2.配置通知

    3.配置切入点和切面

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datasource"/>
    </bean>
<!--    配置通知,编写通知:对事务进行增强(通知),需要编写切入点和具体执行事务的细节 -->
    <tx:advice id="txadvice">
        <tx:attributes>
            <tx:method name="save" propagation="REQUIRED"/>
            <tx:method name="delete" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
<!--    配置切入点和切面-->
<!-- 切入点,execution 定义的表达式表示net.biencheng包下的所有类所有方法都应用该是事务 -->
    <aop:config>
        <aop:pointcut id="point" expression="execution(* com.openlab.Service.stuService.*(..))"/>
<!--        配置切入点-->
        <aop:advisor advice-ref="txadvice" pointcut-ref="point"></aop:advisor>
    </aop:config>

使用Spring管理事务,头文件需要加入

<xmlns:tx="http://www.springframework.org/schema/tx
"http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

Spring 默认的事务传播行为是 PROPAGATION_REQUIRED,它适合于绝大多数的情况。

为什么需要配置事务?

    1)如果不配置,就需要我们手动提交控制事务;

    2)事务在项目开发过程非常重要,涉及到数据的一致性的问题,不容马虎!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值