C# Assign MySql Connection per thread -


i guess simple question, how create new connection per thread?

umm i'm using windows service call in 3 instances of same process, each of them must have own connection. i'm using right grab connection.

public static mysqlconnection connection { get; set; }     public static mysqlconnection opencon()     {         mysqlconnection masteropencon = new mysqlconnection(staticstringclass.masterconstring);         masteropencon.open();         return masteropencon;     } 

trying resolve error: there open datareader associated connection

despite initial urges, i'm not going critique design. i'm answering question given code snippet you've presented:

[threadstatic] private static mysqlconnection _connection;  public static mysqlconnection getconnection() {     // no need locks on threadstatic field, obviously.     if (_connection == null) {         _connection = new mysqlconnection(...);         _connection.open();     }     return _connection; } 

hope helps. read on threadstaticattribute more information. oh, , remember it's each thread's responsibility close own connection.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -