Testing C# MySQL connection pooling fails with Exception: Connection must be valid and open -
Testing C# MySQL connection pooling fails with Exception: Connection must be valid and open -
i trying utilize connection pooling on mysql's .net connector 6.6.4.0 serve multiple threads using code below:
static public uint unonquery(string query) { using (connection = new mysqlconnection(connectionstring)) { if (connection.state == connectionstate.open) { console.writeline("its open..>!!!!"); } connection.open(); mysqlcommand cmd = new mysqlcommand(query, connection); cmd.executenonquery(); mysqldataadapter adapter = new mysqldataadapter("select last_insert_id() 'identity'", connection); datatable dt = new datatable(); adapter.fill(dt); uint ret = 0; if (dt.rows.count > 0) { seek { ret = convert.touint32(dt.rows[0]["identity"]); } grab { ret = 0; } } else ret = 0; connection.close(); homecoming ret; } } my connection string is
"server = 127.0.0.1; user id = ****; password = ****; database = testdb; pooling=true; maximumpoolsize=500;" and thread testing is
static void doinserts(object o) { int = (int)o; console.writeline("insert thread {0} launched", i); for(int x = 0; x < 1000; x++) { uint insertid = datalayer.unonquery(string.format("insert testdb.dbtest (writeno,strno) values ({0},'x={0} thread={1}')", x, i)); } console.writeline("insert thread {0} completed", i); } i exception exception: connection must valid , open. after 6 inserts on 2 concurrent threads, there else need enable pooling work correctly?
thanks
in line using (connection = new mysqlconnection(connectionstring)) using variable connection. variable defined? static? should define in scope of unonquery. if variable connection static, possible connection closed after new function phone call unonquery has opened it.
example: - thread 1 calls unonquery , opens, things. - thread 2 calls unonquery, opens connection, - thread 1 closes connection - thread 2 cannot thing because connection closed.
if above not cause of problem need more of code examine problem.
c# mysql multithreading connection-pooling
Comments
Post a Comment