The requirement is that each piece of data has a number at the same time (accurate to milliseconds). If the number at this time in the database exists, the count is updated. If the number does not exist, add one. The program is to first query and execute insert or update based on the result. There are some, at the same time, the same... the requirement is that at the same time (accurate to milliseconds), each piece of data has a number.
If a number exists at this time, the count is updated. If no number exists, a new one is added.
The program first queries and inserts or updates the program based on the results. There are some cases where the same number exists at the same time.
It should be caused by concurrency. Is it possible to solve this problem only by locking in queries, inserts, and updates?
If multiple tomcat clusters exist, can they be solved only by distributed locks?
Reply content:
The requirement is that at the same time (accurate to milliseconds), each piece of data has a number.
If a number exists at this time, the count is updated. If no number exists, a new one is added.
The program first queries and inserts or updates the program based on the results. There are some cases where the same number exists at the same time.
It should be caused by concurrency. Is it possible to solve this problem only by locking in queries, inserts, and updates?
If multiple tomcat clusters exist, can they be solved only by distributed locks?
The default value is 0 when the data is created. You can simply + x... Why insert
Mysql syntax can solve this problem.
INSERT... ON DUPLICATE KEY UPDATE
Link INSERT... ON DUPLICATE KEY UPDATE
First, how large your concurrency is. If you update the database directly, it will put a lot of pressure on the database.
Provide another way of thinking, using redis
Another method in redis, INCR, can be atomic increments
Redis is a good choice in this scenario.
The main problem is to remove duplicates. Use an hset to insert the key of each record. before inserting a new value, run the hget command to check whether the record has been inserted and no counting is inserted.
INCR as the Count is certainly no problem, read this value.
You can use the database lock mechanism.