c# - SQLite Database Locked exception -


i getting database locked exception sqlite queries only.

below code: when execute select statement works fine.
when executing write statement on jobs table works fine.

this works fine:

executenonquery("delete jobs id=1"); 

but same way if executing queries employees table throwing exception database locked.
throws exception:

executenonquery("delete employees id=1"); 

below functions:

public bool openconnection() {     if (con == null)     {         con = new sqliteconnection(connectionstring);     }     if (con.state == connectionstate.closed)     {         con.open();         //cmd = new sqlitecommand("pragma foreign_keys=on", con);         //cmd.executenonquery();         //cmd.dispose();         //cmd=null;         return true;     }     if (isconnectionbusy())     {         msg.log(new exception("connection busy"));     }     return false; }  public boolean closeconnection() {     if (con != null && con.state == connectionstate.open)     {         if (cmd != null) cmd.dispose();         cmd = null;         con.close();         return true;     }      return false; }  public boolean executenonquery(string sql) {     if (sql == null) return false;     try     {         if (!openconnection())             return false;         else         {             //tx = con.begintransaction(isolationlevel.readcommitted);             cmd = new sqlitecommand(sql, con);             cmd.executenonquery();             //tx.commit();             return true;         }     }     catch (exception exception)     {         //tx.rollback();         msg.log(exception);         return false;     }         {         closeconnection();     } } 

this exception: @ line 103 : cmd.executenonquery();

exception found: type: system.data.sqlite.sqliteexception message: database locked database locked source: system.data.sqlite

stacktrace: @ system.data.sqlite.sqlite3.step(sqlitestatement stmt) @ system.data.sqlite.sqlitedatareader.nextresult() @ system.data.sqlite.sqlitedatareader..ctor(sqlitecommand cmd, commandbehavior behave) @ system.data.sqlite.sqlitecommand.executereader(commandbehavior behavior) @ system.data.sqlite.sqlitecommand.executenonquery() @ timesheet6.dbop.executenonquery(string sql) in d:\projects\c# applications\completed projects\timesheet6\timesheet6\dbop.cs:line 103

somewhere along way connection getting left open. rid of openconnection , closeconnection , change executenonquery this:

using (sqliteconnection c = new sqliteconnection(connectionstring)) {     c.open();     using (sqlitecommand cmd = new sqlitecommand(sql, c))     {         cmd.executenonquery();     } } 

further, change way read data this:

using (sqliteconnection c = new sqliteconnection(connectionstring)) {     c.open();     using (sqlitecommand cmd = new sqlitecommand(sql, c))     {         using (sqlitedatareader rdr = cmd.executereader())         {             ...         }     } } 

do not attempt, manage connection pooling on own here. first, it's more complex have coded, second, it's handled inside sqliteconnection object. finally, if you're not leveraging using, you're not disposing these objects , end issues you're seeing now.


Comments