相信在使用MSSQL數據庫下使用事務回滾方式操作多表記錄的時候,會經常出現“不能在手動或分布事務方式下創建新的連接”的出錯提示信息,這個問題也已困擾我多年。
這次在開發一個大型的商務平台的時候,涉及到數據的計算,同時也必須要多表更新(或刪除)。借助GOOGLE也沒找到一個真能解決的問題。以前收集過一個MSDN的說明,官方的提示必須,只有用SQL語句執行數據庫操作才能使用事務處理。
從官方提示上理解,事式處理中涉及到查詢(Select)時,會出現這種出錯提示。同時也應該與記錄指針(Cursors)有關聯。試著這樣的思路,將事務處理中原出現的Conn.Execute("select ... from ...")修改為使用rs.Open...命令打開記錄集,問題解決。
MSDN說明:
Tips for Working with Cursors
Some providers, such as SQL Server, implement a forward-scrolling, read-only (or 'firehose') cursor mode, meaning that they can efficiently retrIEve data by keeping a connection open. When working with such providers, the connection could be blocked by another user's transaction. The following examples demonstrate scenariOS
that result in errors.
dbConn.Open "DSN=SQLForum;UID=sa;PWD=;"
'Example 1
dbConn.BeginTrans
RS.Open "Select * FROM Message", dbConn
Set dbCmd.ActiveConnection = dbConn
Example 1: The problem is that the command object's ActiveConnection is being set to a connection that is forward-scrolling and in 'firehose' mode. This is the same connection involved in the batch mode. The error from the provider will only appear in the Err object, and it will return as unspecifIEd. For example, with the ODBC
Provider, you will get "UnspecifIEd error".
dbConn.Open "DSN=SQLForum;UID=sa;PWD=;"
'Example 2
RS.Open "Select * FROM Message", dbConn
dbConn.BeginTrans
Example 2: The problem here is that the connection is forward-scrolling and in firehose mode, so it cannot be put into transaction mode. The error returned in the Errors collection from the provider will indicate that it is Operating in firehose mode, and can't work in transaction mode. For example, with the ODBC Provider against
Microsoft SQL Server, you will get the error "Cannot start transaction while in firehose mode".
dbConn.Open "DSN=SQLForum;UID=sa;PWD=;"
'Example 3
RS.Open "Select * FROM Message", dbConn
Set dbCmd.ActiveConnection = dbConn
dbConn.BeginTrans
Example 3: The problem here is that the connection is in forward-scrolling firehose mode, so it cannot also be involved in a batch mode. The error returned in the Errors collection from the provider will indicate that the transaction could not be started. For example, with the ODBC Provider against Microsoft SQL Server, you will get the error "Cannot start transaction because more than one hdbc is in use".