Antecedents feed

This short article requires some knowledge of the JVM to read

This short article requires some knowledge of Spring and spring transactions

Define constants

The FOLLOWING JVM issues refer specifically to OOM or CPU100%

The article distribution

www.yuque.com/docs/share/… 【 Actual Combat 】 Some big Transaction Problems in code

Mp.weixin.qq.com/s?__biz=MzI…

The article main body

Definition of large transactions

When looking at large transaction problems in search engine articles, basically are introduced to the code related to MySQL, so let’s talk about pure code need to pay attention to large transaction problems

In our work, we often run into JVM problems that I thought were far away from us when we started, but in fact are all over every line of code we write

Large transactions are a common error that causes JVM problems

A common definition of large transaction problems: Transactions that run for a long time and operate on a large amount of data are called large transactions.

Business code practice

We often get requests for CRUD lists, or even requirements reviews that say, “I don’t want pagination.”

Products often say this, but often do not know how much data this list contains in a real scenario. If the prediction is 100, 1W is acceptable, but if it is 10W, 100W, our system will suddenly have A JVM problem, causing some or all of the services to hang, affecting the entire line of business

Scenario 1

Return the database list data directly

/** ** List<com.xuegao. Springboot_tool.model.doo.Product> * @author xuegao * @date 2021/12/4 Public List<Product> listService() {List<Product> List = super.list(); return list; }Copy the code

Scenario 2

When saving, to compare with the input data, to ensure the uniqueness of a single piece of data, so we are bound to find all the data of the database, and then compare

Public void saveService(List<Product> inputList) {public void saveService(List<Product> inputList) {List<Product> dbList = super.list();  List<Product> cacheList = new ArrayList<>(); this.checkUnique(inputList, dbList, Boolean.TRUE); super.saveBatch(inputList); } public void updateService(List<Product> inputList) { List<Product> dbList = super.list(); List<Product> cacheList = new ArrayList<>(); this.checkUnique(inputList, cacheList, Boolean.FALSE); super.saveBatch(inputList); } @param inputList: * @param dbList: * @param saveOrUpdate: * @author xuegao * @date 2021/12/5 11:43 */ private void checkUnique(List<Product> inputList, List<Product> dbList, boolean saveOrUpdate) { dbList.addAll(inputList); For (Product Product: inputList) {this.checkUniqueCore(Product, dbList); } } private void checkUniqueCore(Product product, List<Product> dbList) { dbList = dbList.stream().filter(p -> p.getName().equals(product.getName())) .collect(Collectors.toList()); dbList = dbList.stream().filter(p -> p.getDescription().equals(product.getDescription())) .collect(Collectors.toList());  If (dblist.size () > 0) {throw new RuntimeException(" data already exists "); }}Copy the code

In the first and second scenarios, if our data volume suddenly climbs from 100 to 1W, there will be some impact on the service, and if it climbs to 10W, there will be JVM problems

Conclusion: It is not appropriate to load all the data in the database or cache into memory at once

What should we do

1. The first step is to determine the amount of data with the product and the business side, and design schemes according to different levels

2. Try not to use selectList when making lists

Using the paging scheme, according to the server, select a current page to show how many numbers (front-end needs to design components)

Communicate with the product and design as few pages as possible that do not require pagination

3. Uniqueness check of Save and Update

3.1, if the amount of data is small, you can directly use the code shown above

If you have a lot of data

3.2. You can choose to find out the data of the database in batches for comparison with the input parameter data

3.3. According to some unique representations, only the data with the same identifier as the input parameter can be detected for uniqueness check

Vx official number: Java ice cream

Yuque:www.yuque.com/xuegao-btut…