Abstract: This article uses Redis to achieve optimistic locking, and the second kill system as an example to explain the whole process.
This article source code please download here: https://github.com/appleappleapple/DistributeLearning
Optimistic lock
Most are implemented based on the record mechanism of the version of the data. That is, to add a version of the data identity, in a database table-based version of the solution, generally by adding a "version" field for the database table to read out the data, the version number is read together, and then updated, the version number is added 1. At this point, the version number of the submitted data is compared to the current version number of the corresponding record in the database table, and if the submitted data version number is greater than the current version number of the database, it is updated, otherwise it is considered to be outdated data. The watch command can be used in Redis to monitor a given key, and the entire transaction will fail when exec has changed from the time the watch is called. You can also call watch to monitor multiple keys more than once. This allows you to add optimistic locks to the specified key. Note that watch key is valid for the entire connection, and the transaction is the same. If the connection is broken, both the monitoring and the transaction are automatically purged. Of course. The Exec,discard,unwatch command clears all monitoring in the connection.
Redis transactions
A transaction (transaction) in Redis is a set of commands. A transaction is the smallest execution unit of Redis, as is the command, and the commands in one transaction are either executed or not executed. The implementation of Redis transactions requires the use of MULTI and exec two commands, the start of the transaction to the Redis server to send the MULTI command, and then send the command to be processed in this transaction, and finally send the EXEC command to indicate the end of the transaction command. The Redis transaction is the following 4 commands to implement
1.multi, the transaction of Redis is turned on, the client is the transaction state.
2.exec, commits the transaction, executes the command queue from multi to this command, and the client is non-transactional.
3.discard, cancel the transaction, the client is non-transactional state.
4.watch, monitoring key-value pairs, when the transaction commits exec when it discovers that the monitored monitoring pair has changed, the transaction is canceled.
The following is a simple implementation of a redis optimistic lock implementation of the second kill system
Code implementation:
[HTML] View Plain copy package com.github.distribute.lock.redis; import java.util.list; import java.util.set; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import redis.clients.jedis.jedis; import redis.clients.jedis.transaction; /** * redis optimistic lock instance * @author linbingwen * */ public class optimisticlocktest { public static void main (String[] args) throws interruptedexception { &NBSP;&NBSP;&NBsp; long startime=system.currenttimemillis (); initprduct (); Initclient (); printresult (); long endtime=system.currenttimemillis (); long Time=endTime-starTime; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("program run Time: " +time+ "MS"); } /** * Output results */ public Static void printresult () { jedis jedis = redisutil.getinstance (). Getjedis (); set<string> set = jedis.smembers ("ClientList"); int i = 1; for (String value : set) { system.out.println (" + i++ + ", "+value + " "); } redisutil.returnresource (Jedis); } /* * Initialize customers to start robbing goods */ public static void initclient () { ExecutorService cachedThreadPool = Executors.newcachedthreadpool (); int clientNum = 10000;// number of analog customers for (int i = 0; i < clientnum; i++) { &NBSP;&NBSP;&NBsp; cachedthreadpool.execute (New ClientThread (i)); } cachedthreadpool.shutdown (); &