Spring Service @Transactional Comments

Notes things way: @Transactional

When the pre-cursor to a class, the class label for transaction processing are all methods, examples:

1 @Transactional public class TestServiceBean implements TestService {}

When the method does not require class when certain things:

Copy the code

 1 @Transactional  
 2 public class TestServiceBean implements TestService { 
 3     private TestDao dao; 
 4     public void setDao(TestDao dao) { 
 5         this.dao = dao; 
 6     } 
 7     @Transactional(propagation =Propagation.NOT_SUPPORTED)
 8     public List getAll() { 
 9         return null; 
10     } 
11 }

Copy the code

 

Introduction propagation behavior of things: 

  @Transactional (propagation = Propagation.REQUIRED): If there is a transaction, then the transaction is added, if not a new (default)
  the @Transactional (propagation = Propagation.NOT_SUPPORTED): container is not open for this method Affairs
  @Transactional (propagation = Propagation .REQUIRES_NEW): whether or not there is a transaction, creates a new transaction, the original hangs new finished, continue with the old transaction
  @Transactional (propagation = Propagation.MANDATORY): must be performed in an existing transaction otherwise, an exception is thrown
  @Transactional (propagation = Propagation.NEVER): must be executed in a transaction not, otherwise an exception is thrown (and Propagation.MANDATORY opposite)
  the @Transactional (propagation = Propagation.SUPPORTS): If you call this other bean method, declared in other bean in a transaction, then use the transaction. If there is no other bean declared the transaction, it would not have affairs.

 

Things timeout settings:

  @Transactional (timeout = 30) // Default is 30 seconds

 

Transaction isolation level:

  @Transactional (isolation = Isolation.READ_UNCOMMITTED): read uncommitted (dirty read will not be repeated read) does not substantially use
  @Transactional (isolation = Isolation.READ_COMMITTED): read committed data (read and will not be repeated magic reading)
  @Transactional (= Isolation.REPEATABLE_READ Isolation): repeatable read (read occur phantom)
  @Transactional (= Isolation.SERIALIZABLE Isolation): serialized

  MYSQL: The default is REPEATABLE_READ level
  SQLSERVER: default READ_COMMITTED

Dirty read  : a transaction to read data for another update uncommitted transactions
unrepeatable reads  : In the same transaction, the same data repeatedly reading result returned is different, in other words, 
the subsequent reading may be read further update data transaction has been submitted to the contrary, "repeatable read" multiple times in the same transaction
to read data, to ensure that the read data, the subsequent read is not read another transaction has updated the data submitted by the
phantom read  : insert a transaction read data another transaction has been submitted

 

@Transactional annotation commonly used Parameter Description

parameter name

Functional Description

readOnly

This property is used to set whether the current transaction is a read-only transaction, as provided for read-only true, false, said writable, default is false. For example: @Transactional (readOnly = true)

rollbackFor

This attribute is used to set the required array rollback exception class, when the method specified Exceptions Exceptions thrown array, then the transaction is rolled back. E.g:

Specify a single exception classes: @Transactional (rollbackFor = RuntimeException.class)

Specify multiple exception classes: @Transactional (rollbackFor = {RuntimeException.class, Exception.class})

rollbackForClassName

This property is used to set the exception class name of the array need to be rolled back when the method specified Exceptions Exceptions thrown array name, then the transaction is rolled back. E.g:

Specify a single exception class name: @Transactional (rollbackForClassName = "RuntimeException")

Specify a plurality of exception class name: @Transactional (rollbackForClassName = { "RuntimeException", "Exception"})

noRollbackFor

This property is used to set the array need not be an exception class rollback when the method specified Exceptions Exceptions thrown array, no transaction for rollback. E.g:

Specify a single exception classes: @Transactional (noRollbackFor = RuntimeException.class)

Specify multiple exception classes: @Transactional (noRollbackFor = {RuntimeException.class, Exception.class})

noRollbackForClassName

This property does not need to set the rollback exception class name of the array, when the method specified Exceptions Exceptions thrown in the name of the array, no transaction for rollback. E.g:

Specify a single exception class name: @Transactional (noRollbackForClassName = "RuntimeException")

Specify multiple exception class name:

@Transactional(noRollbackForClassName={"RuntimeException","Exception"})

propagation

This property is used to set the propagation of the transaction, the specific values ​​refer to Table 6-7.

例如:@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)

isolation

Transaction isolation level is used to set the property of the underlying database, transaction isolation level for concurrent handling of multiple transactions, the default isolation level can be generally used database, the basic need to set

timeout

This property is used to set the timeout in seconds of the transaction, the default value of -1 indicates never expires

 

 

Points to note:


  1, @ Transactional public can only be applied to the method for other non-public way, if marked @Transactional not an error, but the method is not transactional capabilities.

  2, the transaction manager with the spring, the spring is responsible for the database open, commit, rollback default encounter runtime exceptions. (throw new RuntimeException ( "Notes");) will be rolled back, that encounter exceptions are not checked (unchecked) when rollback; encountered need to capture exceptions (throw new exception ( "Notes");) is not rolled back, that encounter exceptions under examination (that is, non-runtime exceptions thrown by the compiler checks to abnormal call inspected by the said exceptions or abnormalities) when we need to specify the way in order to make the transaction rollback all exceptions are rolled back, to add @Transactional (rollbackFor = {Exception.class, other abnormal}) If let unchecked exception does not roll back:. @Transactional (notRollbackFor = RunTimeException.class)
as follows:

Copy the code

1 @Transactional(rollbackFor=Exception.class) //指定回滚,遇到异常Exception时回滚
2 public void methodName() {
3    throw new Exception("注释");
4 }
5 @Transactional(noRollbackFor=Exception.class)//指定不回滚,遇到运行期例外(throw new RuntimeException("注释");)会回滚
6 public ItimDaoImpl getItemDaoImpl() {
7    throw new RuntimeException("注释");
8 }

Copy the code

  3、@Transactional 注解应该只被应用到 public 可见度的方法上。 如果你在 protected、private 或者 package-visible 的方法上使用 @Transactional 注解,它也不会报错, 但是这个被注解的方法将不会展示已配置的事务设置。


  4、@Transactional 注解可以被应用于接口定义和接口方法、类定义和类的 public 方法上。然而,请注意仅仅 @Transactional 注解的出现不足于开启事务行为,它仅仅 是一种元数据,能够被可以识别 @Transactional 注解和上述的配置适当的具有事务行为的beans所使用。上面的例子中,其实正是 元素的出现 开启 了事务行为。


  5, Spring team's recommendation is that you use @Transactional comment on specific (method or class) class, but do not use on any interface class to be achieved. Of course you can use @Transactional annotation on an interface, but this will only be when you set it to take effect when the agent interface-based. Because annotations are not inherited, which means that if you are using a proxy class-based, set the transaction will not be recognized based on the proxy class, and the object will not be the Acting packed (to be confirmed severe). Therefore, please accept the recommendation of the Spring team and the @Transactional comment on the specific class.

Original link: https://www.cnblogs.com/caoyc/p/5632963.html

Guess you like

Origin blog.csdn.net/xiaomojun/article/details/82424797