Class A Callback_test ()
Class B Testadd ()
Class C Select () to query to the new data in Class B Testadd method, and initialize some properties
Scenario: Class A nested category B Class B nested Class C, all of which are controlled by spring.
Requirements: Class B Testadd method is not controlled by the spring transaction, a separate new transaction execution, can make the C class query.
Problem: Class A is the underlying method of the system and must be subject to transaction control, and the class C must query the data just added.
Analysis: The initial analysis is: Spring transaction propagation caused, from a class open transactions, there is the end, in order to avoid this situation, it is best to the related classes, do not let spring control transactions.
Resolution: The Testadd() method in class B shows the open transaction. Here's how:
The data in the Testadd method is not subject to transaction control (requirements: Whether it is issued successfully, does not affect subsequent code execution; So to open a new transaction)
Turn on new transactions to prevent confusion with other transactions
Datasourcetransactionmanager TransactionManager = (datasourcetransactionmanager) springcontextfactory
. Getbean ("TransactionManager");
Defaulttransactiondefinition def = new Defaulttransactiondefinition ();
Def.setpropagationbehavior (transactiondefinition.propagation_requires_new); Object isolation level, opening new transactions, and not using the same transaction with Class A and Class B.
Transactionstatus status = Transactionmanager.gettransaction (Def); Get transaction status
try{
Code Logic ****************
Transactionmanager.commit (status);
}catch (Exception e) {
Todo:handle exception
Transactionmanager.rollback (status);
}
Add:
The Testadd method in Class B is configured to not allow spring transaction control, and the Testadd method displays an open transaction, which is manually submitted.
Try:
The Testadd method of Class B is added to the spring transaction control, and the configuration transaction level is propagation_requires_new,
does not work because the current transaction is suspended directly
- propagation_ REQUIRED: If there is no current transaction, create a new transaction, and if one already exists, add it to the transaction. This is the most common choice.
- propagation_supports: Supports the current transaction and executes non-transacted if no transaction is currently in use.
- propagation_mandatory: Throws an exception if no transaction is currently in use for the current transaction.
- propagation_requires_new: Creates a new transaction and suspends the current transaction if a transaction is currently present.
- propagation_not_supported: Performs the operation in a non-transactional manner, if a transaction is currently present, Suspends the current transaction.
- propagation_never: Executes in a non-transactional manner and throws an exception if a transaction is currently present.
- propagation_nested: Executes within a nested transaction if a transaction is currently present. If there is currently no transaction, perform an operation similar to propagation_required
2. Remark:
Question: How do I manually submit a spring-managed transaction? Note: The spring transaction level is propagation_required
Answer: First in the class Start section, open a transaction, the isolation level of the transaction if it is propagation_required, the manual commit transaction does not work.
The isolation level of the transaction needs to be configured. Propagation_requires_new, manually committing the transaction will work.
Turn on new transactions to prevent confusion with other transactions
Datasourcetransactionmanager TransactionManager = (datasourcetransactionmanager) springcontextfactory
. Getbean ("TransactionManager");
Defaulttransactiondefinition def = new Defaulttransactiondefinition ();
Def.setpropagationbehavior (transactiondefinition.propagation_requires_new); Object isolation level, opening new transactions, and not using the same transaction with Class A and Class B.
Transactionstatus status = Transactionmanager.gettransaction (Def); Get transaction status
Reference: http://blog.csdn.net/ziyunlong1984/article/details/7725585
Http://blog.chinaunix.net/uid-10289334-id-2964925.html
http://blog.csdn.net/hy6688_/article/details/44763869
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Spring Nested transaction control