c# - Do I have to lock the database connections when multithreading? -


here sample of class use database interaction:

using system; using system.data; using system.collections.generic;  // libraries using log4net; using log4net.config; using mysql.data.mysqlclient;  namespace aic {     class db     {         private static readonly ilog _logger = logmanager.getlogger(typeof(db));         private mysqlconnection _connection;         private mysqlcommand _cmd;         private string _server;         private string _database;         private string _username;         private string _password;          //constructor         public db(string server, string database, string username, string password)         {             log4net.config.xmlconfigurator.configure();              _server = server;             _database = database;             _username = username;             _password = password;              _connection = new mysqlconnection(string.format("server={0};database={1};uid={2};password={3};charset=utf8;", _server, _database, _username, _password));         }          public bool testconnection()         {             try             {                 _connection.open();                 _connection.close();                 _logger.info("connection test, passed...");                 return true;             }             catch (mysqlexception ex)             {                 _logger.error(ex.tostring());                 return false;             }         }          //open connection database         private bool open()         {             try             {                 if (_connection.state != connectionstate.open)                     _connection.open();                 _logger.info("starting connection database...");                 return true;             }             catch (mysqlexception ex)             {                 _logger.error(ex.tostring());                 return false;             }         }          //close connection         private bool close()         {             try             {                 if (_connection.state != connectionstate.closed)                     _connection.close();                 _logger.info("closing connection database...");                 return true;             }             catch (mysqlexception ex)             {                 _logger.error(ex.tostring());                 return false;             }         }          // basic functions         public bool userexist(string user)         {             string query = "select user_id users username=@name limit 1";             if (this.open())             {                 try                 {                     // assign connection                     _cmd = new mysqlcommand(query, _connection);                      // prepare receive params                     _cmd.prepare();                      // fill params                     _cmd.parameters.addwithvalue("@name", user);                      // returned count bool                     bool result = convert.toint32(_cmd.executescalar()) > 0;                      // close connection                     this.close();                     return result;                 }                 catch (mysqlexception ex)                 {                     _logger.error(ex.tostring());                     this.close();                     return false;                 }             }             else             {                 _logger.error("you must connected database before performing action");                 return false;             }         }          public bool adduser(string user)         {             // .... add user database         }          public bool deluser(string user)         {             // .... del user database         }          public int countusers()         {             // .... count total users database         }     } } 

currently, don't have management opening , closing connections check wether database connected or not, perform action , close as shown in userexist function.

considering this, came attention might closing own connections in middle or transactions since using in 2 different threads.

my doubt here wether simple class lock application reason making unresponsive or cause me troubles in long run?

what should consider, improve, etc.?

would appreciate code samples.

each thread should have own connection instance, in case instance of db.

but problem solved (a lot) better not storing connection in db objects @ all. best pattern use connections local variables in using() {} statement.

currently, class should implement idisposable (just case try/catch logic fails).


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -