How to annotate things: @Transactional
When marked in front of a class, all methods in the marked class are handled by the object, for example:
Implements Testservice {}
When certain methods in a class do not require things:
@Transactional Public class Implements testservice { private Testdao DAO; Public void Setdao (Testdao dao) { this. dao = dao; } =propagation.not_supported) public List getAll () { return NULL ; } }
Introduction to the act of spreading things:
@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.
Things timeout settings:
@Transactional (timeout=30)//default is 30 seconds
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 level
SQL Server: Default is read_committed
Dirty reads : One transaction reads uncommitted update data to another transaction
non-repeatable read : In the same transaction, multiple reads of the same data are returned with different results, in other words,
Subsequent reads can be read to the updated data submitted by another transaction. Conversely, "repeatable reads" are repeated in the same transaction
When reading data, it is guaranteed to read the same data, that is, subsequent reads cannot be read to another transaction committed update data
Phantom reads : One transaction reads the insert data that has been committed by another transaction
Description of common parameters in @Transactional 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 |
Specify multiple exception classes: @Transactional (Rollbackfor={runtimeexception.class, Exception.class}) |
rollbackforclassname |
Specify multiple Exception class names: @Transactional (rollbackforclassname={"RuntimeException", "Exception"}) |
norollbackfor |
Specify multiple exception classes: @Transactional (Norollbackfor={runtimeexception.class, Exception.class}) |
norollbackforclassnam E |
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 |
Note the points:
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 spring transaction manager, Spring is responsible for database open, Commit, rollback. Default run-time exceptions are encountered (the throw new RuntimeException ("comment");) rolled back, that is, the rollback occurs when an exception is encountered that is not checked (unchecked) While encountering exceptions that need to be caught (the throw new Exception ("note");) not be rolled back, that is, an exception that is checked (that is, an exception that is thrown when it is not run-time, the exception that the compiler checks for is called a check exception or a check exception). We need to specify a way to let the transaction roll back. If you want all exceptions to be rolled back, 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 () { throw when encountering exception exception New Exception ("note");} @Transactional (norollbackfor=exception. class) // Specifies no rollback, encounters a run-time exception (throw new RuntimeException ("comment");) rolls back the public Itimdaoimpl Getitemdaoimpl () { thrownew runtimeexception ("note");}
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 certainly use @Transactional annotations on the interface, but 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 proxy (it will be identified as critical). Therefore, accept the suggestions from the spring team and use @Transactional annotations on specific classes.
Spring @Transactional Annotation Parameters