Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server: Could not find prepared statement with handle x
    text
    copied!<p>Recently our QA team reported a very interesting bug in one of our applications. Our application is a C# .Net 3.5 SP1 based application interacting with a SQL Server 2005 Express Edition database.</p> <p>By design the application is developed to detect database offline scenarios and if so to wait until the database is online (by retrying to connect in a timely manner) and once online, reconnect and resume functionality.</p> <p>What our QA team did was, while the application is retrieving a bulk of data from the database, stop the database server, wait for a while and restart the database. Once the database restarts the application reconnects to the database without any issues but it started to continuously report the exception "Could not find prepared statement with handle x" (x is some number).</p> <p>Our application is using prepared statements and it is already designed to call the Prepare() method again on all the SqlCommand objects when the application reconnects to the database. For example,</p> <p>At application startup,</p> <pre><code> SqlCommand _commandA = connection.CreateCommand(); _commandA.CommandText = @"SELECT COMPANYNAME FROM TBCOMPANY WHERE ID = @ID"; _commandA.CommandType = CommandType.Text; SqlParameter _paramA = _commandA.CreateParameter(); _paramA.ParameterName = "@ID"; _paramA.SqlDbType = SqlDbType.Int; _paramA.Direction = ParameterDirection.Input; _paramA.Size = 0; _commandA.Parameters.Add(_paramA); _commandA.Prepare(); </code></pre> <p>After that we use ExceuteReader() on this _commandA with different @ID parameter values in each cycle of the application.</p> <p>Once the application detects the database going offline and coming back online, upon reconnect to the database the application only executes,</p> <pre><code> _commandA.Prepare(); </code></pre> <p>Two more strange things we noticed. 1. The above situation on happens with CommandType.Text type commands in the code. Our application also uses the same exact logic to invoke stored procedures but we never get this issue with stored procedures. 2. Up to now we were unable to reproduce this issue no matter how many different ways we try it in the Debug mode in Visual Studio.</p> <p>Thanks in advance.. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload