PRB: Error Creating More Than One Recordset in a Transaction

Symptoms

When you try to create more than one recordset in the middle of a transaction, the following error occurs:

Cannot create a new connection because in manual transaction mode
The HRESULT is 0x80004005.

Alternatively, you may receive the following, more informative error message:

Runtime error '-2147467259(80004005)':
You can't have multiple recordsets with this cursortype (in a transaction.) Either change the cursor type, commit the transaction, or close one of the recordsets.
or this similar error message:

Runtime error '-2147467259(80004005)':
Transaction cannot have multiple recordsets with this cursor type (in a transaction.) Change the cursor type, commit the transaction, or close one of the recordsets.

Cause

SQL Server can only open one ForwardOnly cursor at a time on a connection, because SQL Server can only process one active statement at a time per connection.



When you try to open more than one ForwardOnly ADO recordset at a time on a single Connection, only the first ADO recordset is actually opened on the Connection object. New, separate connections are created for subsequent ForwardOnly cursors.

A transaction is on a single connection. When you attempt to open more than one ForwardOnly recordset within a single transaction, ADO attempts to open more than one ForwardOnly recordset on the connection of the transaction. An error occurs because SQL Server only allows one ForwardOnly recordset on a single connection. Because the error is within a manual transaction, you might see the error above. Microsoft Data Access Objects 2.1 Service Pack 2 and later versions of MDAC contain more informative error messages. For that reason, you may see the more informative second or third error message, above.

Resolution

Use a different cursor type, change the cursor location to adUseClient or close the first recordset before opening another on the same connection/transaction.


For example, if you use the following code the error will not occur:



Note You must change Username=<username> and PWD=<strong password> to the correct values before you run this code. Make sure that User ID, UID, and Username have the appropriate permissions to perform this operation on the database.
Dim cnn1 As New ADODB.Connection
Dim rs1 As New ADODB.Recordset
Dim rs2 As New ADODB.Recordset

strCnn = "DSN=Springhill;Username=<username>;PWD=<strong password>;database=pubs"
cnn1.Open strCnn
cnn1.BeginTrans
rs1.Open "Select * from authors", cnn1, adOpenStatic, adLockOptimistic
rs2.Open "Select * from publishers", cnn1, adOpenStatic,

adLockOptimistic

cnn1.CommitTrans
cnn1.Close
NOTE: If you refer to either recordset after the transaction has been committed or aborted, you see the following error:

Run-time error '-2147418113': Catastrophic failure
You must either use and then close the recordsets within the transaction or preserve the cursors. The OLE DB Provider for ODBC does not allow preserving cursors, so you must close the recordsets within the transaction. The OLE DB Provider for SQL Server does allow preserving cursors. See the REFERENCES section of this article for more information.

Status

This behavior is by design.

More Information

Steps to Reproduce Behavior

The following Visual Basic code causes the error to occur:



Note You must change Username=<username> and PWD=<strong password> to the correct values before you run this code. Make sure that User ID, UID, and Username have the appropriate permissions to perform this operation on the database.
Dim cnn1 As ADODB.Connection
Dim rs1 As Recordset
Dim rs2 As Recordset
Dim strCnn As String

Set cnn1 = New ADODB.Connection
strCnn = "DSN=SQLServer;Username=<username>;PWD=<strong password>;database=pubs"

cnn1.Open strCnn
cnn1.BeginTrans
Set rs1 = cnn1.Execute("Select * from authors")
Set rs2 = cnn1.Execute("Select * from publishers")

cnn1.CommitTrans

cnn1.Close
End Sub

References

For additional information about the 'Catastrophic failure' message in this context, click the following article number to view the article in the Microsoft Knowledge Base:

187942 PRB: Catastrophic Error Occurs Referencing ADO Recordset

For additional information about ForwardOnly recordsets and ADO, click the following article number to view the article in the Microsoft Knowledge Base:

235282 INFO: SQL Server Spawns Additional Connections When You Open Multiple ForwardOnly ADO Recordsets

Propiedades

Id. de artículo: 180843 - Última revisión: 03/14/2005 - Revisión: 1

Comentarios