In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Spring propagation attribute of what is the role, many novices are not very clear about this, in order to help you solve this problem, the following small series will be explained in detail for everyone, there are people who need to learn, I hope you can harvest.
In Spring transaction management, we can use @Transactional to set transactions declaratively. Specifically, add @ Transaction to the class or method and pass in the attribute parameter to get the required Transaction attribute. @ Transaction in Spring has five properties: Propagation, Isolation, Rollback Rules, Timeout, and Read-Only, where the Propagation property defines the boundaries of the Transaction-whether to use the Transaction, how to behave if the Transaction already exists, and so on.
Add @Transactional to the service class to declare that all methods of this service require transaction management. Each business method opens a transaction at the beginning.
Spring rolls back transactions for run-time exceptions by default. The exception is unchecked, which does not roll back if it encounters a checked exception.
How to change default rules:
1 Make checked exceptions roll back: Prefix the entire method with @Transactional(rollbackFor=Exception.class)
2 Make unchecked exceptions not rollback: @Transactional(notRollbackFor=RunTimeException.class)
3 (query only) methods that do not require transaction management: @Transactional(propagation=Propagation.NOT_SUPPORTED)
No transaction is opened until the entire method runs
You can also add: @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true), which makes a read-only transaction more efficient.
Meaning of various attributes:
REQUIRED: Business methods need to run in a container. If the method is already in a transaction when it runs, join the transaction, otherwise create a new transaction yourself.
NOT_SUPPORTED: Declares that the method does not require transactions. If the method is not associated with a transaction, the container does not open the transaction for it. If the method is called in a transaction, the transaction is suspended. After the call is completed, the original transaction resumes execution.
REQUIRESNEW: This method collection initiates a new transaction for itself, regardless of whether a transaction exists. If the method is already running in a transaction, the original transaction is suspended and a new transaction is created.
MANDATORY: This method can only be executed within an existing transaction; business methods cannot initiate their own transactions. The container throws an exception if invoked in a non-transactional environment.
SUPPORTS: If the method is called within the scope of a transaction, the method becomes part of that transaction. If a method is invoked outside the scope of the transaction, the method executes in a transaction-free environment.
NEVER: This method must not be executed within a transaction scope. If so, throw an exception. The method executes correctly only if it is not associated with any transactions.
NESTED: If an active transaction exists, it runs in a nested transaction. If there are no active transactions, press the REQUIRED attribute. It uses a single transaction that has multiple savepoints that can be rolled back. Rollback of internal transactions has no effect on external transactions. It only works for DataSourceTransactionManager transaction managers.
Propagation property outMethodinnerMethodPropagation.MANDATORY throws exception Run Propagation in outMethod Transaction.NEVER does not run exception Propagation.NOT_SUPPORTED Do not run outMethod Transaction pauses until innerMethod completes Propagation.REQUIRED (default) Opens a new Transaction and runs Propagation in outMethod Transaction.REQUIRES_SUPPORTED NEW Create a new Transaction and run it in outMethod Transaction pause until the new Transaction in innerMethod completes Propagation.SUPPORTS does not run in Transaction Run in outMethod Transaction Is it helpful to read all of this? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.