In actual projects, there will be a large number of users connecting to the database and requesting various operations. At this time, we need to process each request creation thread. At this time, the problem arises, what should I do when all requests involve database writing? Database locks are required at this time. You can use Baidu for locking specific databases. However, you do not need much advanced locking knowledge here. You only need to write locks.
What is the write lock? Specifically, it is an exclusive lock. When writing, other programs are not allowed to write data, which ensures data consistency. OK. That's enough. How can I lock the database in the program?
Database locks are implemented when we write a database. Of course, many databases now carry their own shackles, but when we have a lot of concurrency, it is better to write it by ourselves.
C # operations on MySQL as mentioned in the previous article. Here we will explain how to apply locks.
The Code is as follows:
string lockDB = "LOCK TABLE t_userinfo WRITE"; MySQLCommand sqlcmd = new MySQLCommand(lockDB, conn); sqlcmd.ExecuteReaderEx(); string cmd = "update t_userinfo set F_State = 0 where F_Name= '" + name + "'"; sqlcmd = new MySQLCommand(cmd, conn); sqlcmd.ExecuteReaderEx(); string UnlockDB = "UNLOCK TABLES"; sqlcmd = new MySQLCommand(UnlockDB, conn); sqlcmd.ExecuteReaderEx();
Note that you must unlock the lock after it is completed.
Previous Article: http://www.bkjia.com/kf/201312/261976.html