Spring嵌套事务机制补充

上一篇文章,是关于Spring嵌套事务机制的,回过头来看发现有不少问题,但暂时不想删掉它,所以再写一篇文章更正一下之前的错误。这里先再次说明一下PROPAGATION_NESTED的作用,若当前存在事务则以嵌套事务方式执行,若不存在事务则创建一个新事务。

阅读全文

Spring嵌套事务机制

Spring的几种事务传播机制如下:

PROPAGATION_REQUIRED:Support a current transaction, create a new one if none exists.
PROPAGATION_SUPPORTS:Support a current transaction, execute non-transactionally if none exists.
PROPAGATION_MANDATORY:Support a current transaction, throw an exception if none exists.
PROPAGATION_REQUIRES_NEW:Create a new transaction, and suspend the current transaction if one exists.
PROPAGATION_NOT_SUPPORTED:Execute non-transactionally, suspend the current transaction if one exists.
PROPAGATION_NEVER:Execute non-transactionally, throw an exception if a transaction exists.
PROPAGATION_NESTED:Execute within a nested transaction if a current transaction exists, behave like PROPAGATION_REQUIRED.

阅读全文

单例模式

如何保证一个对象只有一个实例并且易于访问呢?定义一个全局变量可以确保对象随时都可以被访问,但是不能防止我们实例化多个对象。一个更好的解决方法是让类自身负责保存它的唯一实例。这个类可以保证没有其他实例被创建,并且它可以提供一个访问该实例的方法。单例模式主要有五种实现方式,分别是饿汉模式、懒汉模式、双检锁(DCL)、静态内部类和枚举,下面分别展示了这几种模式的Java写法。

阅读全文