tags:

views:

61

answers:

1

Hi:

I have multiple processes inserting data to the same table in my database. I am using one database connection for all the processes. When I was trying to run one of the sqlcommand, I got the above exception. I did some researching online and it seems that Mysql server only support one datareader per connection. And since each processes uses executeNonQuery to execute SQL commands in a fast speed, it is possible that the previous datareader was not closed when a new commands get issued.

Is there a way around this problem?

Thanks

+1  A: 

The root cause of your error is that (hoping to avoid the obvious), the same connection is being used, and already open/executing. Are they static at the moment? Suggest looking into why the pooling isn't being used or is being limited.

Consider refactoring your code to have using statements. Pseudo, noncompiling example:

private void ExecNonQuery(string sqlStatement)
{          
    using (var conn = new MySqlConnection(connString))
    {
       using (var cmd = new MySqlDataCommand(conn))
       {
           cmd.ExecuteNonQuery(sqlStatement);
       }
    }
}

or

private List<Customer> GetCustomers( )
{
    string sqlStatement = "SELECT ID, Name FROM Customer";                
    using (var conn = new MySqlConnection(connString))
    {
        using (var cmd = new MySqlDataCommand(conn))
        {
           //get a reader and do something
           using (var reader = new MySqlDataReader(sqlStatement))
           { 
               ...
           }
        }
    }
}

By wrapping the creation of those objects in a using statement, you'll be ensuring that your objects are closed and disposed of properly.

I'd suggest that yes, you should be opening a new connection everytime. The ADO.NET Driver for MySQL should use proper connection pooling. You really shouldn't have to think about it.

Why have multiple processes share the same connection? What's the need there?

p.campbell
Does this requires to open a new connection every time I try to run a sql command? I have multiple processes sharing a same connection. Thanks.
each process receving data from socket interfaces and storing to database. The data come in from socket interface in a very fast speed sometimes.
Do you think BeginExecuteNonQuery will help?
@user: I'd suggest refactoring with `using` and see if it helps. Avoid any `static` as well.
p.campbell