SQL Server : are transaction locking table for other users?

后端 未结 2 794
既然无缘
既然无缘 2021-01-27 23:41

Does a transaction lock my table when I\'m running multiple queries?

Example: if another user will try to send data in same time which I use transaction, what will happe

相关标签:
2条回答
  • 2021-01-28 00:22

    You have to implement transaction smartly. Below are some performance related points :-

    1. Locking Optimistic/Pessimistic. In pessimistic locking whole table is locked. but in optimistic locking only specific row is locked.
    2. Isolation level Read Committed/Read Uncommitted. When table is locked it depends upon on your business scenario if it allowed you then you can go for dirty read using with NoLock.
    3. Try to use where clause in update and do proper indexing. For any heavy query check the query plan.
    4. Transaction timeout should be very less. So if the table is locked then it should throw error and In catch block you can retry.

    These are few points you can do.

    0 讨论(0)
  • 2021-01-28 00:36

    You cannot avoid that multiples users load data to the database. It is neither feasible nor clever to lock every time a single user requested the usage of a table. Actually you do not have to worry about it, because the DB itself will provide mechanism to avoid such issues. I would recommend you reading into ACID properties.

    • Atomicity
    • Consistency
    • Isolation
    • Durability

    What may happen is that you could suffer a ghost read, which basically consist that you cannot read data unless the user who is inserting data commits. And even if you have finished inserting data and do not commit, there is a fair chance that you will not see the changes.

    DDL operations such as creation, removal, etc. are themselves committed at the end. However DML operation, such as update, insert, delete, etc. are not committed at the end.

    0 讨论(0)
提交回复
热议问题