简体   繁体   English

SQL事务:TSQL与VB.NET?

[英]SQL Transactions: TSQL vs VB.NET?

we're tracking down a nasty timeout bug in one of our applications. 我们正在追踪我们的一个应用程序中的一个令人讨厌的超时错误。

I wanted to know what (if any) differences there are to using the SQL Transaction within the application versus written into the stored procedure using TSQL statement. 我想知道在应用程序中使用SQL事务与使用TSQL语句写入存储过程有什么区别(如果有的话)。 We would need to restructure the stored procs and vb code to get this to work and I'm not sure it would be worth the effort at this time. 我们需要重新构建存储过程和vb代码才能使其工作,我不确定此时是否值得付出努力。

Public Sub RetrieveTData(ByVal cID As String, ByVal cnn As SqlConnection) As Boolean

    Dim sqlTran As SqlTransaction
    cnn.Open()
    sqlTran = cnn.BeginTransaction

    Try
        If Not DataAlreadyTransferred(cID) Then

            Dim Command As New SqlCommand("usp_BigNasty_CopyDataFromDB1toDB2")
            Command.CommandType = CommandType.StoredProcedure
            Command.Transaction = sqlTran
            Command.Parameters.AddWithValue("@cID", cID)
            Command.Connection = cnn

            Command.ExecuteNonQuery()
            sqlTran.Commit()
        Endif

    Catch ex As Exception
        sqlTran.Rollback()
        Throw
    Finally
        cnn.Close()
    End Try

End Sub

We aren't certain whether the timeout is occurring in DataAlreadyTransferred() or usp_BigNasty_CopyDataFromDB1toDB2 due to how the try/catch is written. 由于try / catch的编写方式,我们DataAlreadyTransferred()usp_BigNasty_CopyDataFromDB1toDB2是否发生超时。 We can restructure this code, but will take a week or so to get it to production (no errors occur on test/dev today) 我们可以重新构建这段代码,但需要一周左右的时间才能生成(今天测试/开发没有错误)

DB1 - permanent storage, used by other applications as well DB1 - 永久存储,也被其他应用程序使用
DB2 - working set, used only by Web App DB2 - 工作集,仅由Web App使用

DataAlreadyTransferred(cID) first checks to see if DB2 has any copies of the records, if DB2 does and those records are clean it deletes them (data could have changed in DB1 and we want the most up-to-date version). DataAlreadyTransferred(cID)首先检查DB2是否有记录的任何副本,如果DB2有,并且那些记录是干净的,则删除它们(数据可能在DB1中已更改,我们需要最新版本)。 If DB2's data is dirty it is left alone and no data is deleted. 如果DB2的数据是脏的,则不管它是什么,也不会删除任何数据。

usp_BigNasty_CopyDataFromDB1toDB2 copies rows from approximately 20-30 different tables and copies over the perm copies from DB1 into DB2, essentially creating a working set from which the Web App can access usp_BigNasty_CopyDataFromDB1toDB2从大约20-30个不同的表复制行,并通过perm副本从DB1复制到DB2,实质上创建了一个工作集,Web App可以从中访问

We are aware this is inefficient and are examining ways to improve it, just haven't had time yet... 我们意识到这是低效的,正在研究改进它的方法,还没有时间......

I believe by having the transactions in the app code it's locking many more tables than is really needed. 我相信通过在应用程序代码中进行交易,它会锁定比实际需要更多的表。 If we move them to the stored procs, less tables will be locked at one time thus improving our chances of removing deadlock conditions/timeout issues we're seeing today. 如果我们将它们移动到存储过程中,则会一次锁定较少的表,从而提高我们消除今天看到的死锁条件/超时问题的机会。 Just not sure on this.. 只是不确定这个..

Thanks for any input. 感谢您的任何意见。

Your transaction is open for longer than needed with round trips and multiple calls. 您的交易开放的时间比往返和多次通话所需的时间长。

I'd suggest a single stored proc that tests and cleans and writes = one atomic call to the server. 我建议单个存储过程测试清理写入=一个原子调用服务器。

I'd also consider replication to keep the data live in near real time, or even mirroring too if DB2 contains a lot of DB1. 我还考虑复制以保持数据接近实时,或者甚至镜像也是如果DB2包含大量DB1。 Let the DB engine do it? 让数据库引擎做到吗?

Upon further investigation we've discovered that many of the tables involved had no indexes on them. 经过进一步调查,我们发现所涉及的许多表都没有索引。 After creating the recommended indexes for the queries involved in this process our timeouts issue has seemed to go away (for now). 在为此过程中涉及的查询创建建议索引之后,我们的超时问题似乎已经消失(暂时)。

I'm still going to be looking into optimizing this entire process, as it's still pretty slow for the amount of data it's churning through... 我仍然会考虑优化整个过程,因为它仍然很慢,因为它正在流动的数据量......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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