Today brings you more practical functions , use Java To generate a serial number / order number , List several common cases in our life :

* order number
* Item number
* Transaction No
* courier number
I use data storage mysql, Let's share with you how it is realized ~

Theoretical knowledge

What is a transaction ?

Database transaction ( abbreviation : affair ) It is a logical unit in the execution of database management system , It consists of a limited sequence of database operations .

Dirty reading , Non repeatable reading , Unreal reading

1, Dirty reading :A The transaction has modified the data but has not committed to the database , This time B Transaction to access , that B Transactions are not up to date with data , This phenomenon is called dirty reading .

2, Non repeatable reading :A The transaction reads one data more than once , It's Midway at this time B The transaction modified the data , cause A The results read by the transaction multiple times are inconsistent .

3, Unreal reading :A When a transaction queries the same range twice , The last query sees rows not seen in the previous query , because B The transaction adds a new piece of data before the next query .

mysql Four isolation levels

According to the isolation level, from low to high , Higher isolation , The worse the efficiency , Non repeatable reading , yes MySQL Default isolation level for .

Transaction isolation level dirty read, non repeatable read
Read uncommitted (read-uncommitted) Yes, yes, yes
Non repeatable reading (read-committed) No yes yes
Repeatable reading (repeatable-read) No no yes
Serialization (serializable) No no no
1, Read uncommitted : Allow other transactions , To read the data before the transaction is committed shortcoming : May cause dirty reading , Unreal reading , Non repeatable reading .

2, Non repeatable reading : Problems occur under concurrent conditions , such as :A User read data , subsequently B The user reads the data and modifies it , here A When the user reads the data again, it is found that the values before and after the two times are inconsistent
shortcoming : May cause unreal reading , Non repeatable reading .

3, Repeatable reading : When using the repeatable read isolation level , All rows referenced by the transaction in any way are locked during the execution of the transaction . shortcoming : Unreal reading

4, Serialization : Not used mysql of mvcc mechanism , In every select Obtain read lock under request , In every update Attempt to obtain write lock under operation . shortcoming : Worst efficiency

Two pessimistic locks

Shared lock (S lock ): If transaction T On data A After adding the shared lock , Other transactions can only A Add a shared lock , No exclusive locks . Transactions that are allowed to share locks can only read data , Data cannot be modified .

Exclusive lock (X lock ): If transaction T On data A With the exclusive lock , Then other transactions can no longer be A Add any type of blockade . Transactions that are granted exclusive locks can read data , And can modify the data .

Java code implementation
private String createNewBidNumber() { // Format description CODE20201111xxx
CODE+ Current date + number ( The specific length depends on the demand ) String front="CODE";// prefix // Current time code Date date = new
Date(); String bidDate = new SimpleDateFormat("yyyyMMdd").format(date); Object
bidService=null;// Modify to your own business code if (bidService != null){//
Found it in the data sheet , It shows that this order is not the first one today String bid = bidService.getXXXX(); // take out ID, That is, the business number bid
= bid.substring(10,13); // Take out the last three digits , That is, automatically generated three digits 001 int num =
Integer.valueOf(bid); num ++; // plus 1 if(num<10){ String bidNum =
String.format("%03d", num);//%03d Just three , Insufficient supplement 0 String code = front+bidDate+bidNum;
return code; } else if(num<100){ String bidNum = String.format("%03d",
num);//num<100, The description is two digits , One should be added in front 0 String code = front+bidDate+bidNum; return code;
} else { String bidNum = String.valueOf(num); String code
=front+bidDate+bidNum; return code; } }else { int number = 1; String bidNum =
"00" + number; String code = front+bidDate+bidNum; return code; } }

©2019-2020 Toolsou All rights reserved,
Redis Underlying data structure A person who dissuades others from learning computer , Are not good people win10 System computer C Where's the disc ,c disc users where? (win10c Disk not found users) Freshman c Language student management system ( lower ) hospital WIFI Which family is strong ? utilize Python Script unlimited spoof girlfriend computer C language program design —— Student management system Byte runout - Test development practice - One side cool meridian python Run code at specified time 5 Best style Linux Server system recommendation Anti anxiety life after naked resignation , I believe everything will have the best arrangement