add分布式锁

pull/1/head
595208882@qq.com 3 years ago
parent f6a30f7692
commit 335648de33

@ -1986,12 +1986,13 @@ DELETE FROM method_lock WHERE lock_key ='methodName';
```mysql ```mysql
CREATE TABLE `methodLock` ( CREATE TABLE `methodLock` (
`id` INT ( 11 ) NOT NULL AUTO_INCREMENT COMMENT '主键', `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
`method_name` VARCHAR ( 64 ) NOT NULL DEFAULT '' COMMENT '锁定的方法名', `lock_key` varchar(64) NOT NULL DEFAULT '' COMMENT '锁的键值',
`desc` VARCHAR ( 1024 ) NOT NULL DEFAULT '备注信息', `lock_timeout` datetime NOT NULL DEFAULT NOW() COMMENT '锁的超时时间',
`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '保存数据时间,自动生成', `remarks` varchar(255) NOT NULL COMMENT '备注信息',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY ( `id` ), PRIMARY KEY ( `id` ),
UNIQUE KEY `uidx_method_name` ( `method_name ` ) USING BTREE UNIQUE KEY `uidx_lock_key` ( `lock_key ` ) USING BTREE
) ENGINE = INNODB DEFAULT CHARSET = utf8 COMMENT = '锁定中的方法'; ) ENGINE = INNODB DEFAULT CHARSET = utf8 COMMENT = '锁定中的方法';
``` ```
@ -2007,7 +2008,7 @@ public boolean lock() {
// 循环阻塞,等待获取锁 // 循环阻塞,等待获取锁
while (true) { while (true) {
// 执行获取锁的sql // 执行获取锁的sql
String sql = "select * from methodLock where method_name = xxx for update"; String sql = "select * from methodLock where lock_key = xxx for update";
// 创建prepareStatement对象用于执行SQL // 创建prepareStatement对象用于执行SQL
ps = conn.prepareStatement(sql); ps = conn.prepareStatement(sql);
// 获取查询结果集 // 获取查询结果集

Loading…
Cancel
Save