TUESDAY, 07 APRIL

Comprehensive transaction support is the most compelling reasons to use the Spring-Framework.

不得不选择Spring-Framework的原因在于其全面事务的支持。

Q1: What is transaction?

A1:transaction_事务:一步或几步数据操作序列组成的逻辑执行单元。特性:原子性、一致性、隔离性、持续性

事务的控制是保证应用程序底层数据完整性的重要手段。

11-1.Global transactions and Local transactions

  • Global transaction: enable you to work with multiple transaction resources;

The app server manages global transactions through the JTA, and you also need to use JNDI because a JTA UserTransaction needs to be sourced from JNDI.

So it could limit any potential reuser of app code.

总结:全局事务允许同时使用多个事务资源(数据库和进程),但复杂的依赖关系导致全局事务的使用限制了代码的复用性。

关键词:笨重,复杂

- Local transaction: resource-specific, more easier more disadvantages: code that manages transactions using a JDBC connection cannot run within a global JTA transaction; another downside is that local transaction are invasive to the programming model

总结:本地事务是基于特定资源进行执行逻辑的单元,更容易使用,没有复杂的依赖关系,但本地事务的缺点也很明显,单一性,无法使用多个事务资源,只适合于执行数据的本地且单一进程的事务,本地事务不涉及多个数据来源。

其次,本地事务更趋近于强侵入性的编程模型。

关键词:单一,侵入性

11-2.Spring-Framework's consistent programming model

  • Spring-Framework resolves the disadvantages of global and local transaction.

You Write your code once, and it can benefit from different transaction management strategies in different environments

You typically write little or no code related to transaction management

Don't depend on the Spring Framework transaction API or any other transaction API

- Declarative and programmatic transaction management.

总结:Spring-Framework提供的一致性编程模型能够解决上述两种事务的不足。(声明式事务+编程式事务)

1、一次编写,适应不同的事务管理策略,在不同的环境中能够运行

2、只需编写少许代码,或者根本不用编写代码就能实现事务管理

3、并不依赖与Spring-Framework事务API或者其他事务API

11-3.Spring-Framework transaction abstraction

Key: transaction strategy(事务策略)

Defined: org.springframework.transaction.PlatformTransactionManager interface

Manager:

-getTransaction(TransactionDefinition
definition)

return a TransactionStatus object,
depending on a TransactionDefinition
parameter

tips:

TransactionDefinition
interface:

  • Isolation:事务是否独立于其他事务;
  • Propagation:
  • Timeout:
    事务执行的时长限制,何时执行自动回滚(rollback)
  • Read-only
    status: 只允许用户使用代码读取数据,并不允许修改数据。Read-only事务是一个非常有用的选项,特别是在面对hibernate的时候。

-commit(TransactionStatus status)

-rollback(TransactionStatus
status)

tips:

Public interface TransactionStatus
extends SavepointManager{

Boolean isNewTransaction();

//是否是新建的事务

Boolean hasSavepoint();

//是否有逻辑点,可以将事务回退到这个点,而不是回退整个事务。

Void setRollbackOnly();

//将回滚事务的操作停留在方法级别,可以只回滚关键方法。

Boolean isRollbackOnly();

//是否只回滚部分

Void flush();

Boolean is Completed();

//事务是否完成

}

As we can see, this transaction
strategy is defined by an interface, it can be easily mocked or
stubbed as necessary. And it's not tie to a look up strategy such
as JNDI.

This PlatformTransactionManager
implementations is just defined like any other object or bean in
Spring's IOC container. This benefit makes Spring Framework
transactions a worthwhile abstraction even you work with JTA.

总结:事务管理策略的定义是基于一个普通的接口类实现的,而该接口类直接交由Spring的IOC容器管理。

无论你在Spring中使用哪一种事务管理策略,声明式 or 编程式, platformTransactionManager
的实现才是最重要的一步。

-JDBC Template:

1)定义dataSource

2)定义事务管理策略,需要声明dataSource的位置

-JTA
+ JNDI

定义的JTA事务管理策略并不需要知道dataSource的位置,因为JTA是全局事务管理。

-Hibernate

本地事务管理,Hibernate,需要告知sessionFactory的位置,而sessionFactory则是Hibernate对dataSource的管理方法。

JDBC与Hibernate的例子中也可使用JTA,并不需要提供dataSource位置。

11-4 Declarative Transaction
management

The Spring Framework's declarative
transaction management is similar to EJB CMT(Container Managed
Transaction) in that you can specify transaction behavior down to
individual method level.

总结:Spring-Framework的声明式事务管理可以将指定的事务提高到方法级别。即通过将一个或者几个方法声明为事务。

-Key
words: non-invasive, lightweight

-The
differences between EJB CMT and Spring-Framework's
declarative Transaction management

1)works in any environment,
which is not like EJB CMT tied to JTA(依赖性弱,对运行环境要求不高)

2)apply the Spring-Framework DTM
to any class(对对象的限制不强,而JTA在管理transaction的时候要求必须为EJB类)

3)customize transactional
behavior by using AOP(DTM可以通过AOP自定义事务内容,将自定义事务内容插入事务回滚中。)

4)Spring-Framework
不支持事务上下文远程调用的propagation属性???

-Rollback rules:
指定当抛出异常的时候回滚的方法与内容。可以用配置的方式指定回滚声明,并不需要用代码实现。一般情况下,通过调用TransactionStatus对象中的setRollbackOnly()方法实现回滚,但在使用Spring-Framework的大多数时候都直接指定回滚内容,例如MyApplicationException,这样一来,业务对象就不依赖于事务的基础结构,比如说,无需再引入Spring事务API或者其他Spring的API,独立性强。

-Understanding the Spring-Framework's declarative
transaction implementation

仅仅只是知道用@Transactional注解类,添加@EnableTransactionManagement用来注解配置是不够的。我们需要从原理上理解how
it works。

The
combination of AOP with transactional metadata yields an AOP proxy
that uses a TransactionInterceptor
in conjunction with an
appropriate platformTransactionManager(interface
class) implementation
arround method invocations.

通过AOP与事务元数据的结合,提供AOP代理,将TransactionInterceptor(事务拦截器,控制事务的第一步,获取事务)与适当的PlatformTransactionManager实现类共同实现在驱动事务。原理如下图:

事务管理器的配置内容:

标记1:定义管理事务的事务管理器,在下方代码有提到。

标记2:的配置内容确保了txAdvice定义的事务方案在合适的时间执行。

利用将pointcut和标记1种定义的txAdvice 关联起来。fooServiceOperation执行,txAdvice定义

的方案will be run。

实例(from Spring framework Reference
Document)

从日志文件的记录上我们能够很清楚的看到配置的事务管理器在事务管理上的作用。

最新文章

  1. TopCoder SRM 639 Div.2 500 AliceGameEasy
  2. POJ 3414 Pots
  3. Android 的 AlarmManager 和 wakeLock联合使用
  4. 昨晚值班将发dla的程序改好后放入正式环境
  5. POJ 1816 Wild Words
  6. 【BZOJ 3196】二逼平衡树 线段树套splay 模板题
  7. MST:Roadblocks(POJ 3255)
  8. look
  9. 论SOA架构的几种主要开发方式【转】
  10. Ueditor自定义默认宽度高度
  11. mui实现退出当前应用
  12. Spring + CXF(REST):webservice not found
  13. Spring Cloud Eureka 常用配置详解,建议收藏!
  14. Hadoop 集群安装(主节点安装)
  15. pycharm 3.4 破解
  16. vmware中如何检查cpu的使用状况-一个考题引发的思考
  17. server2008远程开端口的方法
  18. Android-Java-面向对象与面向过程的简单理解
  19. 利用hadoop来解决“单表关联”的问题
  20. Java applets A Java applet example

热门文章

  1. cacti (可以利用yum安装cacti的配置)
  2. jequry_rotate.js用来写旋转类的东西的插件(如:抽奖转盘)
  3. spring boot&&cloud干货系列
  4. 20:django中的安全问题
  5. Fel表达式实践
  6. 都是干货---真正的了解scrapy框架
  7. Spiral Matrix I&&II
  8. Python图像处理库(1)
  9. Qt笔记——多线程
  10. 微信小程序 - video组件poster无效 / 视频播放列表