简体   繁体   中英

How to prevent simultaneous access of two applications to the one database

Imagine that you have an application that have access to SQL Server 2012, so it reads data from one table, process it and writes result to another table. If you launch two such applications simultaneously on different computers the resulting data will be doubled. The question is: How to prevent this situation?

Please provide you examples with Transact-SQL and C#.

You set some state in the DB that informs applications that a processing task is being performed. (I assume it's ok for both applications can run one after the other with no side-effect, or the same app can run twice)

The application will then check this state and refuse to run if its set.

Alternatively, you can lock an entire table so the 2nd instance cannot read (or write) data using the isolation level .

What you want is to lock the corresponding tables while one application is doing it's job.

More info here: http://www.sqlteam.com/article/introduction-to-locking-in-sql-server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM