zoukankan      html  css  js  c++  java
  • spring的事务

    一、事务的ACID特性 :原子性,一致性,隔离性,持久性

    二、事务的分内

    事物分为两种,一种是编程式事务,就是之前利用JDBC写的commit和rowback的方式提交或者回滚事物,这种控制事物的方式比较麻烦,一般在做大型项目的时候不建议使用这种方式。

    另一种是声明式事务,就像字面上的意思一样,在一个地方声明一下,然后程序中不需要再由事物控制的代码,使用简单。

    三、spring支持声明式事物,配置如下:

    dao层,持久化层(负责连接数据库,执行数据库的)

    service层 业务逻辑层(负责实现系统所有的业务逻辑)

    controller层 控制层(接收前台参数,调用service层业务逻辑,往前台返回数据)

    保存订单的业务实现:
    控制层:负责接收前台传过来的订单信息,订单明细的信息 ,调用业务逻辑层保存订单的方法
    业务逻辑层: 写保存订单的方法 保存订单的方法应该先调持久化层保存主订单的方法,再调用持久层保存订单明 细的方法
    持久化层:保存主订单的方法 保存订单明细的方法
    我们不希望出现保存订单的时候 主订单保存成功之后,出现了异常,然后订单明细没有保存成功,如果是这样,数 据库中的数据就不完整的了。所以我们一般为了避免这种类似的情况,我们可以把事物加到service层。 
     
    spring配置文件:applicationContext.xml
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4 xmlns:context="http://www.springframework.org/schema/context"
     5 xmlns:aop="http://www.springframework.org/schema/aop"
     6 xmlns:tx="http://www.springframework.org/schema/tx"
     7 xsi:schemaLocation="http://www.springframework.org/schema/beans
     8 http://www.springframework.org/schema/beans/spring-beans.xsd
     9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/springcontext.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/springaop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    10 <!--扫描dao接口所在的包-->
    11 <context:component-scan base-package="com.aaa.spring.dao.impl,com.aaa.spring.service.impl">
    12 </context:component-scan>
    13 <!---创建dbcp数据源连接池对象-->
    14 <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
    15 <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
    16 <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
    17 <property name="username" value="scott"></property>
    18 <property name="password" value="tiger"></property>
    19 </bean>
    20 <!--创建jdbcTemplate对象-->
    21 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    22 <property name="dataSource" ref="dataSource"></property>
    23 </bean>
    24 <!---创建spring的事物管理器对象-->
    25 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    26 <property name="dataSource" ref="dataSource"></property></bean>
    27 <!---创建spring事物管理的通知类对象-->
    28 <tx:advice id="txAdvice" transaction-manager="transactionManager">
    29 <!---此处可以配置事物管理的一些特性-->
    30 <tx:attributes>
    31 <!---
    32 name属性中可以使用通配符,用来指定该属性对哪些方法起作用
    33 propagation 配置事物的传播特性
    34 REQUIRED 判断当前方法是否存在事物,如果不存在事物就创建一个新的事物,如果存在事物就使用当前的事
    35 36 REQUIRES_NEW 判断当前方法是否存在事物 ,如果不存在事物就创建一个新的事物,
    37 如果存在事物,就把当前事物挂起,再启动一个新的事物
    38 NESTED 嵌套事物 。判断当前方法是否存在事物,如果不存在事物就创建一个事物,如果存在事物,把当前事物
    39 挂起,
    40 再启动一个当前事物的子事物 。这样如果父事物产生了异常。子事物即使没有产生异常也会回滚。
    41 read-only 配置事物的只读特性
    42 true 当前事物是只读事物,在方法之后不会提交事物
    43 flase 默认值 当前事物是非只读事物,在方法之后会提交事物
    44 配置事物的只读特性 增删改方法一般需要事物,查询方法一般不需要事物,所以以后开发的时候最好查询方法把
    45 read-only
    46 设置成true,在一定程度上能提高程序的运行效率
    47 -->
    48 <tx:method name="save*" propagation="NESTED" />
    49 <tx:method name="select*" propagation="REQUIRED" read-only="true"></tx:method>
    50 </tx:attributes>
    51 </tx:advice>
    52 <!---配置spring的声明式事物-->
    53 <aop:config>
    54 <!---声明切入点对象 告诉spring 要对哪些类的哪些方法使用事物-->
    55 <aop:pointcut id="p1" expression="execution(* com.aaa.spring.service.impl.*.*(..))"></aop:pointcut>
    56 <!--声明事物管理专用的切面-->
    57 <aop:advisor advice-ref="txAdvice" pointcut-ref="p1"></aop:advisor>
    58 </aop:config>
    59 </beans>

    事物什么时候回滚?service方法如果抛出运行异常,才会回滚。

     注解配置:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xmlns:tx="http://www.springframework.org/schema/tx"
     6        xsi:schemaLocation="http://www.springframework.org/schema/beans
     7        http://www.springframework.org/schema/beans/spring-beans.xsd
     8        http://www.springframework.org/schema/context
     9        http://www.springframework.org/schema/context/spring-context.xsd
    10        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    11     <context:component-scan base-package="com.aaa.spring"></context:component-scan>
    12 
    13     <!--创建dbsp数据源连接池对象-->
    14     <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
    15         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
    16         <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
    17         <property name="username" value="scott"></property>
    18         <property name="password" value="tiger"></property>
    19     </bean>
    20     <!--创建jdbcTemplate对象-->
    21     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    22         <property name="dataSource" ref="dataSource"></property>
    23     </bean>
    24 
    25     <!--创建spring的事物管理器对象-->
    26     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    27         <property name="dataSource" ref="dataSource"></property>
    28     </bean>
    29 
    30     <!--声明以注解的方式配置声明式事物-->
    31     <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    32 </beans>
    第二步,要在需要使用事物的类上增加@Transactional注解 
     

    面试题:

    你知道那些数据源连接池?

    dbcp,c3p0, 阿里巴巴druid ,BoneCP,Proxool,DBPoll, Primrose等。

    事物分几种方式

    编程式事物,spring支持声明式事物。

    spring事物处理的机制,底层采用的是AOP 的机制

  • 相关阅读:
    Git的环境搭建
    AmazeUI HTML元素
    AmazeUI布局
    AmazeUI基本样式
    Bash简介
    Linux下拷贝目录和删除
    linux下的定时任务
    缓存
    隔离
    DEDECMS使用SQL命令批量替换语句
  • 原文地址:https://www.cnblogs.com/yanpingping/p/10920662.html
Copyright © 2011-2022 走看看