c# - NullReferenceException in MySqlClient.NativeDriver -
i have long running program exectues mysql-queries every 5 minutes (timed thread). after has run approximately 10 hours nullreferenceexception thrown following stacktrace:
at mysql.data.mysqlclient.nativedriver.getresult(int32& affectedrow, int32& insertedid) bei mysql.data.mysqlclient.driver.getresult(int32 statementid, int32& affectedrows, int32& insertedid) @ mysql.data.mysqlclient.driver.nextresult(int32 statementid) @ mysql.data.mysqlclient.mysqldatareader.nextresult() @ mysql.data.mysqlclient.mysqldatareader.close() @ mysql.data.mysqlclient.mysqlcommand.resetreader() @ mysql.data.mysqlclient.mysqlcommand.executereader(commandbehavior behavior) @ mysql.data.mysqlclient.mysqlcommand.executenonquery() @ myownprogram.mysql.senddata(string sqlcommmand)
the senddata method following (lockdb of type object)
public void senddata(string sqlcommmand) { try { using(mysqlcommand command = connection.createcommand()) { command.commandtimeout = 5; command.commandtext = sqlcommmand; lock(lockdb) { command.executenonquery(); } } } catch(mysqlexception e) { throw e; } catch(exception e) { // logging } }
the connection opened when calling constructor of mysql-class , disposed when calling destructor or calling dispose(). can tell me did wrong?
i recommend leaving connection handling ado.net:
public void senddata(string sqlcommmand) { using (var con = new mysqlconnection(connectionstring)) using(var cmd = con.createcommand()) { con.open(); cmd.commandtimeout = 5; cmd.commandtext = sqlcommmand; cmd.executenonquery(); } }
you no longer need finally
statement. add try/catch
if need log make sure propagate exception , not consume it.
Comments
Post a Comment