Spring's @transactional Chapter

Source: Internet
Author: User

How to annotate a transaction: @Transactional I. Location of annotations

1. If you annotate the class (it works on the entire class):

@Transactional

public class Testdaoimpl implements Testdao {

Public List getList () {

return null;

}

}

2. Add to the method (only works on the entire method):

public class Testdaoimpl implements Testdao {

@Transactional

Public List getList () {

return null;

}

}

Ii. Introduction to the conduct of the Communication of affairs:

@Transactional (propagation=propagation.required): If there is a transaction, then join the transaction and create a new one (by default)@Transactional (propagation=propagation.not_supported): container does not open transactions for this method@Transactional (propagation=propagation.requires_new): Creates a new transaction regardless of whether a transaction exists, the original hangs, the new execution completes, and the old transaction continues@Transactional (propagation=propagation.mandatory): Must be executed in an existing transaction or throw an exception@Transactional (Propagation=propagation.never): Must be executed in a transaction that is not, otherwise throws an exception (as opposed to propagation.mandatory)@Transactional (propagation=propagation.supports): If another bean calls this method and declares a transaction in another bean, use the transaction. If the other bean does not declare the transaction, then the transaction is not used.@Transactional (propagation=propagation.nested): If there is an active transaction running in a absconded transaction with no active transaction, press the Propagation.required property to execute

Third, Transaction Timeout settings:

@Transactional (timeout=30)//default is 30s

IV. Transaction ISOLATION Level

@Transactional (isolation = isolation.read_uncommitted): READ UNCOMMITTED data (dirty read, non-repeatable read) basic not used

@Transactional (Isolation = isolation.read_committed): Read Committed data (non-repeatable read and Phantom reads occur)

@Transactional (Isolation = isolation.repeatable_read): Repeatable READ (phantom read occurs)

@Transactional (Isolation = isolation.serializable): serialization

MYSQL: Default to Repeatable_read levelSQL Server: Default is read_committedDirty read: One transaction read to another transaction uncommitted update data is not repeatable READ: In the same transaction, multiple reads of the same data return different results, in other words, subsequent reads can be read to another transaction committed update data. Conversely, "repeatable read" can guarantee the same read data when read data multiple times in the same transaction, that is, subsequent reads cannot be read to the updated data submitted by another transactionPhantom reads: One transaction reads the insert data that has been committed by another transaction

v. @Transactional description of common parameters in annotations

Parameter name Function description
ReadOnly This property is used to set whether the current transaction is a read-only transaction, set to True for read-only, false to read-write, and the default value to False. Example: @Transactional (readonly=true)
Rollbackfor This property is used to set an array of exception classes that need to be rolled back, and when the method throws an exception in the specified exception array, the transaction is rolled back. For example: Specify a single exception class: @Transactional (Rollbackfor=runtimeexception.class) specifies multiple exception classes: @Transactional (rollbackfor={ Runtimeexception.class,exception.class})
Rollbackforclassname This property is used to set an array of exception class names that need to be rolled back, and when the method throws an exception in the specified exception name array, the transaction is rolled back. For example: Specify a single exception class name: @Transactional (rollbackforclassname= "runtimeexception") specifies multiple exception class names: @Transactional ( rollbackforclassname={"RuntimeException", "Exception"})
Norollbackfor

This property is used to set an array of exception classes that do not need to be rolled back, and when a method throws an exception in the specified exception array, the transaction is not rolled back. For example: Specify a single exception class: @Transactional (Norollbackfor=runtimeexception.class) specifies multiple exception classes: @Transactional (norollbackfor={ Runtimeexception.class,exception.class})

Norollbackforclassname This property is used to set an array of exception class names that do not need to be rolled back, and when a method throws an exception in the specified exception name array, the transaction is not rolled back. For example: Specify a single exception class name: @Transactional (norollbackforclassname= "RuntimeException") to specify multiple exception class names @transactional ( norollbackforclassname={"RuntimeException", "Exception"})
Propagation This property is used to set the propagation behavior of a transaction, which can be referenced in table 6-7. Example: @Transactional (propagation=propagation.not_supported,readonly=true)
Isolation This property is used to set the transaction isolation level of the underlying database, which is used to handle multi-transaction concurrency, usually using the default isolation level of the database, and does not need to be set
Timeout This property is used to set the time-out seconds for a transaction, and the default value is 1 to never time out

vi. matters of caution

1, @Transactional can only be applied to the public method, for other non-public methods, if the tag @transactional will not error, but the method does not have transactional capabilities. 2, with the Spring transaction manager, The database is opened, committed, and rolled back by spring. By default, the run-time exception is encountered (the throw new RuntimeException ("note");) rolled back, which is the rollback when an exception is encountered that is not checked (unchecked);

When encountering exceptions that need to be caught (the throw new Exception ("note");) is not rolled back, that is, an exception that is checked (that is, an exception that is thrown when it is not run-time, and the exception that the compiler checks is called a check exception or a check exception).

We need to specify a way to let the transaction roll back to all exceptions, add @Transactional (Rollbackfor={exception.class, other exceptions}).

If you let unchecked exceptions do not roll back: @Transactional (Notrollbackfor=runtimeexception.class)

as follows: @Transactional (Rollbackfor=exception.class)//Specify rollback, rollback public void methodName () when encountering exception Exception () {throw new Exception ( "Comments");} @Transactional (Norollbackfor=exception.class)//Specifies no rollback, encounters a run-time exception (throw new RuntimeException ("note");) rolls back public Itimdaoimpl Getitemdaoimpl () {throw new RuntimeException ("comment");} 3. @Transactional annotations should only be applied to the public visibility method. If you use @Transactional annotations on protected, private, or package-visible methods, it will not error, but this annotated method will not show the configured transaction settings. 4. @Transactional annotations can be applied to interface definitions and interface methods, class definitions, and public methods of classes. Note, however, that only @Transactional annotations appear to be less than the open transaction behavior, which is only a meta-data that can be used to identify @Transactional annotations and the beans that are configured appropriately to have transactional behavior. In the above example, it is the presence of the element that opens the transaction behavior. 5. The spring team's recommendation is that you use @Transactional annotations on specific classes (or methods of classes) rather than on any interface that the class implements. You can of course use @Transactional annotations on the interface,

However, this will only take effect if you set up an interface-based proxy. Because annotations are not inherited, this means that if you are using a class-based proxy, the transaction's settings will not be recognized by the class-based proxy.

And the object will not be wrapped by the transaction agent (it will be identified as critical). Therefore, accept the suggestions from the spring team and use @Transactional annotations on specific classes.

Spring's @transactional Chapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.