C # Writing chat program series notes (3) database lock operations

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.