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 (requires: whether the post succeeds, does not affect the subsequent code execution; So to open a new transaction)//open a new transaction 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 transaction currently, create a new transaction, if a transaction already exists, is added to this 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 \
Java enterprise-Class generic rights security framework source SPRINGMVC MyBatis or Hibernate+ehcache Shiro Druid Bootstrap HTML5
"Java Framework source code download"
Spring Nested transaction control