Transactional configuration in spring
Spring
Transactional configuration in spring
(18:56:32)
Reprinted
Tags:
Miscellaneous
Category: Spring
@ Transactional (norollbackfor = runtimeexception. Class) method transaction description
@ Transactional (rollbackfor = Exception. Class)
@ Transactional (readonly = true)
@ Transactional (timeout = 100)
@ Transactional (isolation) database isolation level
{
Read uncommited: Read uncommitted data (dirty reads may occur and unrepeatable Phantom reads may occur)
Read commited: Read committed data (Repeatable read and phantom read may occur)
Repeatable read: Repeatable read (phantom read may occur)
Serializable: serializable
}
Dirty read: one transaction reads the uncommitted update data of another transaction.
Non-repeated read: In the same transaction, the results returned by reading the same data are different for multiple times, that is, subsequent reads can read the committed update data of another transaction.
Repeatable read: when the same transaction reads data multiple times, it can ensure that the data read is the same, that is, the data read later cannot read the data committed by another transaction.
Phantom read: one transaction reads the updated data committed by another transaction
Query Methods
@ Transactional (propagation = propagation. not_supported) the transaction is not enabled for a method.
@ Transactional (propagation = propagation. Required) default transaction support
Example:
Public class bean1 {
@ Transactional (propagation = propagation. required)
Public void Update (){
Executeupdate (SQL );
}
}
Public class bean2 {
@ Transactional (propagation = propagation. Supports)
Public void Update (){
Executeupdate (SQL );
}
}
Public class transaction {
@ Transactional (propagation = propagation. requires_new)
Public void test1 ()
{
Bean1.update ();
Bean2.update ();
}
@ Transactional (propagation = propagation. not_supported)
Public void Test2 ()
{
Bean1.update ();
Bean2.update ();
}
@ Transactional (propagation = propagation. Supports)
Public void test3 ()
{
Bean1.update ();
Bean2.update ();
}
}
// Transaction propagation attributes
@ Transactional (propagation = propagation. Required) // if a transaction exists, add it to the transaction. If no, create a new one.
@ Transactional (propagation = propagation. not_supported) // This method does not enable transactions.
@ Transactional (propagation = propagation. requireds_new) // creates a new transaction regardless of whether a transaction exists. The original transaction is suspended, the new transaction is completed, and the old transaction continues to be executed.
@ Transactional (propagation = propagation. Mandatory) // It must be executed in an existing transaction; otherwise, an exception is thrown.
@ Transactional (propagation = propagation. Never) // cannot be executed in a transaction, that is, there must be no transaction currently; otherwise, an exception is thrown.
@ Transactional (propagation = propagation. Supports) // other beans call this method. If transactions are declared in other beans, transactions are used. Transactions are not required without declaration.
@ Transactional (propagation = propagation. nested) // If an active transaction exists, it runs in a nested transaction. If there is no active transaction, it is executed according to the required attribute, and it uses a separate transaction. This bookstore has multiple storage points for rollback. the rollback of internal transactions does not affect external transactions. It only takes effect for the datasource transactionmanager Transaction Manager.
@ Transactional (propagation = propagation. required, readonly = true) // read-only, update unavailable, delete
@ Transactional (propagation = propagation. required, timeout = 30) // timeout 30 seconds
@ Transactional (propagation = propagation. required, isolation = isolation. Default) // database isolation level
References:
Http://www.cnblogs.com/xupei/archive/2011/01/07/1929632.html
Http://hi.baidu.com/qmzwhl/blog/item/aae2d6581afd17d29d8204ca.html
Spring transaction Annotation
Throw new runtimeexception ("...") by default; rollback
Throw new exception ("...") to be captured; will not be rolled back
// Specify rollback
@ Transactional (rollbackfor = Exception. Class)
Public void methodname (){
// Does not roll back
Throw new exception ("...");
}
// Do not roll back
@ Transactional (norollbackfor = Exception. Class)
Public itimdaoimpl getitemdaoimpl (){
// Rollback
Throw new runtimeexception ("comment ");
}
// If a transaction exists, add it to the transaction. If no transaction exists, create a new one (without writing)
@ Transactional (propagation = propagation. required)
// The container does not start the transaction for this method
@ Transactional (propagation = propagation. not_supported)
// No matter whether a transaction exists or not, a new transaction is created, the original transaction is suspended, the new execution is completed, and the old transaction continues to be executed.
@ Transactional (propagation = propagation. requires_new)
// It must be executed in an existing transaction; otherwise, an exception is thrown.
@ Transactional (propagation = propagation. Mandatory)
// It must be executed in a non-existing transaction; otherwise, an exception is thrown (opposite to propagation. Mandatory)
@ Transactional (propagation = propagation. Never)
// If other beans call this method and declare transactions in other beans, use transactions. If other beans do not declare transactions, no transactions are required.
@ Transactional (propagation = propagation. Supports)
/*
Public void methodname (){
// Method 1 of this class
Update ();
// Call the modification method of other classes
Otherbean. Update ();
// Method 2 of this class
Update ();
}
If the other fails, the modification submission of this class will not be affected.
The update of this class fails, and the other fails.
*/
@ Transactional (propagation = propagation. nested)
// Readonly = true read-only, cannot be updated, delete
@ Transactional (propagation = propagation. required, readonly = true)
// Set the timeout value
@ Transactional (propagation = propagation. required, timeout = 30)
// Set the database isolation level
@ Transactional (propagation = propagation. required, isolation = isolation. Default)