如何锁定数据库直到事务完成 [英] how to lock database until transaction complete

查看:83
本文介绍了如何锁定数据库直到事务完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要在开始交易时锁定数据库中的所有表格

Hi I need to lock all tables in my database the moment i start transaction

Try
   If con.State = ConnectionState.Closed Then con.Open()
   tran = con.BeginTransaction()
   cmd.Transaction = tran
   'I need to lock database until tran commit or rollback
   cmd.CommandText = "select max(id)+1 from table_1;"
   cmd.ExecuteNonQuery()
   'Do some stuff
   tran.Commit() ' realease database lock
Catch ex As Exception
   MsgBox(ex.Message)
   tran.Rollback()  ' realease database lock
Finally
   con.Close()
End Try

推荐答案

简而言之,数据库事务就是这样:锁定数据库直到提交或回滚。如果我在这里写下已多次写下的内容,那就没有意义了;请参考这些链接,特别注意交易隔离级别方面:



http://en.wikipedia.org/wiki/Database_transaction [ ^ ]

http:// en。 wikipedia.org/wiki/Isolation_%28database_systems%29 [ ^ ]

Google搜索:vb.net交易教程 [ ^ ]

Google搜索: Codeproject交易教程 [ ^ ]



如果要实现同时访问数据库的应用程序如果有多个线程或与其他应用程序一起使用,我建议您将自己限制在隔离级别可序列化。作为一个初学者,它绝对是最安全的选择,不冒险允许任何不必要的事情发生。
To put it simply, a database-transaction is exactly that: Locking the database until commit or rollback. It wouldn't make sense if I wrote down here what has been written down many times already; please refer to these links and pay attention especially to the aspect of "Transaction Isolation Level":

http://en.wikipedia.org/wiki/Database_transaction[^]
http://en.wikipedia.org/wiki/Isolation_%28database_systems%29[^]
Google search: vb.net transaction tutorial[^]
Google search: Codeproject transaction tutorial[^]

If you are going to implement an application that accesses a database simultaneously with multiple threads or in concert with other applications I would recommend you restrict yourself to the Isolation-level "Serializable". As a beginner it's definitely the safest option to not risk allowing anything unwanted to happen.


这篇关于如何锁定数据库直到事务完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆