In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to achieve a distributed lock in the MySQL database. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Build a table
CREATE TABLE `globallocktable` (`id` int (11) NOT NULL AUTO_INCREMENT, `lockKey` varchar (60) NOT NULL COMMENT 'lock name', `createTime` datetime NOT NULL COMMENT 'creation time', PRIMARY KEY (`id`), UNIQUE KEY `lockKey` (`lockKey`) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT=' global lock'
Components for others to use
@ Componentpublic class GlobalLockComponent {@ Resource GlobalLockTableDAO globalLockDAO; / * attempted to acquire the lock with success of true and failure of false * / public boolean tryLock (String key) {return GlobalLockUtil.tryLock (this.globalLockDAO, key) } / * if another program already occupies the lock and exceeds the timeoutMs (millisecond) time, the lock occupation will be forcibly cleared * that is, delete the record first according to key, and then add the record * / public boolean tryLockWithClear (String key, Long timeoutMs) {return GlobalLockUtil.tryLockWithClear (this.globalLockDAO, key, timeoutMs) Release the lock and delete the record according to key * / public void releasLock (String key) {GlobalLockUtil.releasLock (this.globalLockDAO, key);}}
The lock object is defined as follows
Public class GlobalLockTable {private Integer id; private String lockKey; private Date createTime; / / omit get and set methods} GlobalLockTableDAO is defined as follows: public interface GlobalLockTableDAO {int deleteByPrimaryKey (Integer id); int deleteByLockKey (String lockKey); GlobalLockTable selectByLockKey (String key); int insertSelectiveWithTest (GlobalLockTable record);}
Specific locking and unlocking logic
Public class GlobalLockUtil {private static Logger logger = LoggerFactory.getLogger (GlobalLockUtil.class); private static GlobalLockTable tryLockInternal (GlobalLockTableDAO lockDAO, String key) {GlobalLockTable insert = new GlobalLockTable (); insert.setCreateTime (new Date ()); insert.setLockKey (key); / / attention 1 int count = lockDAO.insertSelectiveWithTest (insert); if (count = = 0) {GlobalLockTable ready = lockDAO.selectByLockKey (key) Logger.warn ("can not lock the key: {}, {}", insert.getLockKey (), ready.getCreateTime (), ready.getId ()); return ready;} logger.info ("yes got the lock by key: {}", insert.getId (), insert.getLockKey ()); return null } / * * timeout clears lock occupancy and relocks * * / public static boolean tryLockWithClear (GlobalLockTableDAO lockDAO, String key, Long timeoutMs) {GlobalLockTable lock = tryLockInternal (lockDAO, key); if (lock = = null) return true; if (System.currentTimeMillis ()-lock.getCreateTime (). GetTime ()
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.