Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use transactions correctly when Springboot has locks

Shulou Source: shulou.com Published: 2022-06-02 06:16:37 10月14日 Update

This article mainly introduces "Springboot in the case of a lock how to use transactions correctly", in daily operation, I believe many people in Springboot in the case of a lock how to use transactions correctly there are doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, I hope to answer "Springboot in the case of a lock how to use transactions correctly" doubts helpful! Next, please follow the small series to learn together!

1. overview

As the old saying goes, if you want to make money, go see what the rich want, because the rich have more money, so they earn more.

Anyway, in Java project development, the word "lock" is not unfamiliar, the most classic use scenario is the oversold problem of goods.

A lot of Java white, usually think, add a "lock" to the code, can solve the problem of multi-deduction inventory, but ignore the problem of database transactions, today we will do an experiment, analyze the problem of oversold goods.

2. Scene Introduction

There is one item, only 1 left in stock.

When purchasing goods, do three actions, one is to check inventory, the other is to deduct inventory, and the third is to generate orders. All three actions are performed in one transaction.

Simulate concurrent scenarios, using 10 threads to execute user purchases simultaneously.

3. Code Implementation 3.1 Unlocked Code Implementation @Transactional(rollbackFor = Exception.class) public void buy() { //Check Yes Item No Stock Integer count = getProductCount(); if(count

Tags: Transactions inventory goods code problems situations phenomena orders learning experiments generation scenarios methods annotations money three actions more rich people theory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MySQL Microsoft Redmi NVidia