In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to analyze the difficulties of Spring senior affairs management, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
1. Spring transaction propagation behavior
The so-called transaction propagation behavior is how transactions propagate between multiple transaction methods when they are called to each other. Spring supports 7 transaction propagation behaviors
PROPAGATION_REQUIRED (join existing transactions)
If there is no current transaction, create a new transaction, and if one already exists, join it. This is the most common and default way.
PROPAGATION_SUPPORTS (follow the environment)
Supports the current transaction and executes in a non-transactional manner if there is no current transaction.
PROPAGATION_MANDATORY (transaction required)
Use the current transaction and throw an exception if there is no current transaction.
PROPAGATION_REQUIRES_NEW (standalone transaction)
Create a new transaction and suspend the current transaction if it exists.
PROPAGATION_NOT_SUPPORTED (non-transactional)
The operation is performed in a non-transactional manner, suspending the current transaction if there is a current transaction.
PROPAGATION_NEVER (exclude transactions)
Executes in a non-transactional manner and throws an exception if a transaction currently exists.
PROPAGATION_NESTED (nested transactions)
If a transaction currently exists, it is executed within a nested transaction. If there is no transaction currently, perform an operation similar to PROPAGATION_REQUIRED.
Spring's default transaction propagation behavior is PROPAGATION_REQUIRED, which is suitable for the vast majority of situations. Assuming that ServiveX#methodX () all works in a transactional environment (that is, all are enhanced by Spring transactions), and that there is a call chain in the program as follows: Service1#method1 ()-> Service2#method2 ()-> Service3#method3 (), then the three methods of these three service classes all work in the same transaction through Spring's transaction propagation mechanism.
If you start a thread in a ServiceA and a () method, execute ServiceB's transaction method b () in the newly created thread. Transaction methods that make nested calls to each other in the same thread work in the same transaction. If these methods that are called within each other work in different threads, transaction methods under different threads work in separate transactions.
2. Multiple data persistence methods for transaction management
If you use a high-end ORM technology (Hibernate,JPA,JDO) and a JDBC technology (Spring JDBC,iBatis), since the Session of the former is an encapsulation of the Connection of the latter, Spring will be "smart enough" to let the former's session encapsulate the latter's connection on the same transaction thread. Therefore, we just need to use the transaction manager of the former directly. The following table shows the transaction manager corresponding to the hybrid data access technology:
1) Unification of transactions in different persistence ways
Spring provides a utility class that can obtain bound data connections from the context of the current transaction, and that is DataSourceUtils. Spring emphasizes the need to use the DataSourceUtils utility class to obtain data connections.
Static Connection doGetConnection (DataSource dataSource)
First try to get the connection from the transaction context, and then get the connection from the data source after failure
Static Connection getConnection (DataSource dataSource)
The function of the doGetConnection method is the same. In fact, it calls the doGetConnection method internally to obtain the connection.
Static void doReleaseConnection (Connection con, DataSource dataSource)
Release the connection and put it back in the connection pool
Static void release Connection (Connection con, DataSource dataSource)
Like the function of the doReleaseConnection method, in fact, it internally calls the doReleaseConnection method to obtain the connection
Test demo:
Service public class TestTranscationServiceImpl implements TestTranscationService {@ Autowired private TestTranscationDao testTranscationDao; @ Override @ Transactional public int test () {testTranscationDao.update1 (); testTranscationDao.update2 (); return 0;} @ Autowired private JdbcTemplate jdbcTemplate; @ Override public int update1 () {/ / 1. Get the database connection Connection con = DataSourceUtils.getConnection (jdbcTemplate.getDataSource ()); try {con.prepareStatement ("update grade_info set grade_name='11' where grade_id=1"). ExecuteUpdate ();} catch (SQLException e) {throw new RuntimeException (e) } finally {/ / 2 if the current method does not have contextual transaction management, not releasing the database connection will cause database connection leakage / / if there is a contextual transaction, there is no problem with calling or not invoking the database connection release DataSourceUtils.releaseConnection (con, jdbcTemplate.getDataSource ());} return 0 } @ Override public int update2 () {/ / 3. Get the database connection and the database connection of 1 is the same connection Connection con = DataSourceUtils.getConnection (jdbcTemplate.getDataSource ()); try {/ / 4. The database connection obtained by this method is different from that obtained by 1Power3. Connection conn = jdbcTemplate.getDataSource (). GetConnection (); conn.close ();} catch (SQLException e) {e.printStackTrace ();} return jdbcTemplate.update ("update grade_info set grade_name=' third year 'where grade_id=1");}
Spring provides a utility class for obtaining transaction context-bound data connections (or their derivatives) and a proxy class for data sources (or their derivatives) for each data access technology framework.
2) precautions for mixed use of Hibernate and JDBC
When the transaction management of Hibernate commits the transaction, it automatically invokes the flush () operation to synchronize the first-level cache to the database, and then the SQL statement is generated and sent to the database.
Because of the above reasons, there may be a problem of losing updates when using JDBC and Hibernate together.
When Hibernate and JDBC are mixed, the operation of JDBC will not be synchronized to the cache of Hibernate (primary cache and secondary cache), and the state changes in Hibernate cache will not be perceived by JDBC. Therefore, special attention must be paid to this point when mixed use.
Because the transaction of the hybrid data access technology is synchronized and the cache is not synchronized, the read and write operations are completed with Hibernate and the read operations are completed with Spring JDBC. For example, use Spring JDBC to query the brief list, and use Hibernate to maintain the queried data. If you really want to use both Hibernate and Spring JDBC to read and write data, you must fully consider the problems caused by the Hibernate caching mechanism: you must fully analyze the data maintenance logic, call Hibernate's flush () method in a timely manner as needed, so as not to overwrite Spring JDBC's changes, and maintain Hibernate's cache when Spring JDBC changes the database.
3. Transaction enhancement constraints of Spring
Because Spring transaction management is based on interface proxy or dynamic bytecode technology, transaction enhancement is implemented through AOP.
For AOP transaction enhancement based on interface dynamic proxy, because the interface method is public, it requires that the implementation method of the implementation class must be public (not protected,private, etc.), and can not use static modifiers. Therefore, the only way to implement an interface dynamic proxy is to use the "public" or "public final" modifiers. Other methods cannot be dynamically proxied, and accordingly, AOP enhancements cannot be implemented, that is, Spring transaction enhancements cannot be performed.
The scheme based on CGLib bytecode dynamic proxy is implanted with AOP enhancement by extending enhanced classes and dynamically creating subclasses. Since methods using the final,static,private modifier cannot be overridden by subclasses, these methods will not be enhanced by the implemented AOP. Therefore, special attention must be paid to the use of these modifiers so as not to be accidentally missed by transaction management.
4. Exception catch and transaction rollback of Spring transaction management
Spring's transaction manager only rolls back exceptions to unchecked exception, and Error and RuntimeException and their subclasses are unchecked exception. The other exception is checked exception.
If try and catch are used in the service layer to catch exceptions, the exception in the runtimeException layer is "intercepted" and cannot be thrown to the transaction manager, which creates an illusion to the transaction manager, just as the program is running without any problems, so there will be no rollback operation on the occurrence of the device.
After reading the above, do you have any further understanding of how to analyze the difficulties of Spring advanced transaction management? If you want to know more knowledge or related content, please follow 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.