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
Post a Comment