In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What is the operation of SpringBoot to improve N-fold performance? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Environment: springboot2.3.9.RELEASE + JPA + MySQL
Generally speaking, we always add transaction support to methods or classes in spring projects, as follows:
@ Transactional public Account deduction (Long id, BigDecimal money) {Optional op = accountDAO.findById (id); if (! op.isPresent ()) {throw new RuntimeException ("does not exist");} account.setMoney (account.getMoney (). Subtract (money)); return accountDAO.saveAndFlush (account);}
This should be the way we use transactions in the project. Here is the method-level transaction. When the method is executed, the transaction is opened by a dynamic proxy, the code is executed, and the transaction is committed / rolled back. The logic for execution is roughly as follows:
Transaction.begin (); method.invoke (xxxx); transaction.commit (); / transaction.rollback ()
The example above is relatively simple. The whole operation is to calculate the deduction amount and then update the data. This business needs to use transactions when saving data, and some other calculations do not need to be in a transaction. Imagine that if we save the code above the operation here, the computational logic is a very complex logic that may take several seconds or even more than a dozen seconds, while the actual save operation may be completed in milliseconds. We also know that the execution of this method-level transaction is to get a Connection object (database connection object) and then open the transaction (set autocommit to false,connection.setAutoCommit (false)) Speaking of which, you should be able to think that it takes a few seconds or even more than ten seconds from getting a Connection object to releasing it, and most of this time is not related to transaction operations, that is to say, no transaction is needed, and our database connection object itself is very precious and limited, which results in a waste of resources in our system and a very low throughput of the system. Next, we will programmatically control the throughput of the transaction providing system.
Simulate regular transactions to show low-throughput operations
Database connection configuration:
Spring: datasource: driverClassName: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/x?serverTimezone=GMT%2B8 username: root password: xxxx type: com.zaxxer.hikari.HikariDataSource hikari: minimumIdle: 1 maximumPoolSize: 1 autoCommit: true idleTimeout: 30000 poolName: MasterDatabookHikariCP maxLifetime: 1800000 connectionTimeout: 30000 connectionTestQuery: SELECT 1
Here, the database connection pool is configured to 1.
Simulating time-consuming operations in Service
@ Transactional public Account deduction (Long id, BigDecimal money) {System.out.println ("Service current execution thread:" + Thread.currentThread () .getName () + ", id =" + id + ", money =" + money); Account account = accountDAO.findById (id) .orElse (null); if (account = = null) {return null;} try {TimeUnit.SECONDS.sleep (10) } catch (InterruptedException e) {e.printStackTrace ();} account.setMoney (account.getMoney (). Subtract (money)); return accountDAO.saveAndFlush (account);}
Controller interface
@ GetMapping ("/ deduction") public Object deductionAction (Long id, BigDecimal money) {System.out.println ("Controller current thread:" + Thread.currentThread (). GetName ()); return accountService.deduction (id, money);}
Start two browser tests and observe the output of the console
Both browsers are still spinning and are not responding.
The console shows that all the Controller methods have entered, but the Service method has entered only one, because we have only one configured connection pool, and the other is waiting for an available connection object. As I said above, in fact, a very long computation in Service does not require transactions, and we should allow these operations that do not require transactions to be performed even if there are no connection objects available. Next, modify the code.
Programming transactions to improve system throughput @ Resource private TransactionTemplate transactionTemplate; public Account deduction (Long id, BigDecimal money) {System.out.println ("Service current execution thread:" + Thread.currentThread () .getName () + ", id =" + id + ", money =" + money); Account account = accountDAO.findById (id) .orElse (null); if (account = = null) {return null } try {TimeUnit.SECONDS.sleep (10);} catch (InterruptedException e) {e.printStackTrace ();} / the execution of the above business code can be a time-consuming operation. Return transactionTemplate.execute (new TransactionCallback () {@ Override public Account doInTransaction (TransactionStatus status) {try {account.setMoney (account.getMoney (). Subtract (money)); return accountDAO.saveAndFlush (account) } catch (Exception e) {logger.error ("error occurs: {}", e); status.setRollbackOnly ();} return null;}});}
Here, we delete the transaction comments on the method, wrap the operations that require transactions programmatically, and inject them into Service
TransactionTemplate object, SpringBoot project has been automatically configured for us, auto-assembly class:
TransactionAutoConfiguration.java
Test:
The browsers are still in circles. Check the console:
The two Service methods are all in, and there is only one connection object in the basic connection pool, but it does not hinder my non-transactional code execution. Through this transformation, does our system throughput improve N?
After reading the above, have you mastered how SpringBoot can improve N-fold performance? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.