1

I have a code which demonstrates simple mysql database connection with ServiceStack ORM

var dbFactory = new OrmLiteConnectionFactory(DB_CONNECTION_STRING, MySqlDialect.Provider);
var db = dbFactory.Open();
Console.WriteLine(db.State.ToString());
db.Close();
db.Dispose();

Result

Open

However when I hit the SQL command

show full processlist;
Id          User    Host                  db       Command  Time    State   Info    Rows_sent   Rows_examined
81107052    user1   <111.21.96.32>:51120  my_db    Sleep    7       NULL    0   0
81107178    user1   localhost             my_db    Query    0       init    show full processlist   0   0
81107179    user1   localhost             my_db    Sleep    0       NULL    0   0

That process with id 81107052 is the process which started with code execution however, goes into sleep and does not terminate by db.Close(); This triggers <max_connections_reached> error with parallel application use.

So How do I close the connection process?

dexterslab
  • 226
  • 2
  • 14

2 Answers2

1

Just use a using statement, i.e:

using (var db = dbFactory.Open())
{
}

Some RDBMS providers will return the connection to the pool and not close it, but you won't get a too many open connections error if all connections are properly disposed.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • No luck, still the same issue persist! – dexterslab May 20 '17 at 14:26
  • @dexterslab maybe it's somewhere else in your code where you're not disposing of the db connection properly, also you can't share db connections with other threads, each thread will need to resolve and dispose of their own connections. – mythz May 20 '17 at 14:30
  • '@mythz adding `Pooling=false` to connection string solves the problem as described in http://stackoverflow.com/questions/5567097/using-mysqlconnection-in-c-sharp-does-not-close-properly – dexterslab May 20 '17 at 14:34
0

Adding Pooling=false is required in connection string

dexterslab
  • 226
  • 2
  • 14