In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
The knowledge points of this article "how to use the pessimistic lock of the database to achieve a distributed lock" are not quite understood by most people, so the editor summarizes the following contents, the content is detailed, the steps are clear, and it has a certain reference value. I hope you can get something after reading this article, let's take a look at this article "how to use the pessimistic lock of the database to achieve a distributed lock" article.
As the name implies, distributed locks occur in a distributed environment. For single-process scenarios, we can use locks provided by languages and class libraries, and for distributed locks, I can also use distributed locks. In other words, the same lock is used in a different environment, and the lock used in a distributed environment is called a distributed lock!
According to the above understanding, should distributed locks have the following characteristics:
Distributed locks must ensure that in a distributed deployment application cluster, the same method can only be executed by one thread on one machine at a time.
One thread acquires the lock, and the other thread must wait for the thread holding the lock to be released before it can be acquired again.
Locks must have a timeout mechanism (avoid deadlocks)
Based on the above characteristics, we can implement a distributed lock through the pessimistic lock of the database.
The code is very simple, just use for update. The details are as follows:
Public class XttblogLock {
Private DataSource dataSource
Private static final String cmd = "select * from xttblog_lock where id = 1 for update"
Public XttblogLock (DataSource ds) {
This.dataSource = ds
}
Public static interface CallBack {
Public void doAction ()
}
Public void lock (CallBack callBack) {
Connection conn = null
PreparedStatement stmt = null
ResultSet rs = null
Try {
/ / try get lock
System.out.println (Thread.currentThread (). GetName () + "begin try lock")
Conn = dataSource.getConnection ()
Conn.setAutoCommit (false)
Stmt = conn.prepareStatement (cmd)
Rs = stmt.executeQuery ()
/ / do business thing
CallBack.doAction ()
/ / release lock
Conn.commit ()
System.out.println (Thread.currentThread (). GetName () + "release lock")
} catch (SQLException e) {
E.printStackTrace ()
} finally {
If (null! = conn) {
Try {
Conn.close ()
} catch (SQLException e) {
E.printStackTrace ()
}
}
}
}
}
The call of the lock is also very simple, the specific code is as follows: the for update command of the database is mainly used to realize the distributed lock using the database pessimistic lock. After executing the change command, the corresponding row records will be locked and other threads will be blocked until the thread that gets the record commits the transaction. It is important to note that autocommit is set to false.
The call to the lock is also very simple. The specific code is as follows:
Final XttblogLock xttblogLock = new XttblogLock (dataSource)
XttblogLock.lock (new CallBack () {
@ Override
Public void doAction () {
System.out.println (Thread.currentThread (). GetName () + "beging do somthing")
Try {
System.out.println ("Amateur grass: welcome to www.xttblog.com!")
Thread.sleep (2000)
} catch (InterruptedException e) {
E.printStackTrace ()
}
System.out.println (Thread.currentThread (). GetName () + "end do somthing")
}
});
Although the for update pessimistic lock of the database can be used as a distributed lock, this method is rarely used in the actual production process, because its performance is not very high.
The above is the content of this article on "how to use the pessimistic lock of the database to achieve a distributed lock". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.