简体   繁体   中英

How to prevent Sqlite Database is locked?

i got one thread reading on the database. When i click on the menustrip it shows an error "Database is locked." and it only happen sometimes. Any way to prevent the database lock? I have try WAL but its not working.

Reading:

private void checkPort(string ipIN, char[] input, string output)
    {
        try
        {
            bool building = false;
            Thread beep = new Thread(Beep);
            using (SQLiteConnection c = new SQLiteConnection(dbconnection))
            {
                c.Open();
                string query = "select * from ALL_IO(nolock)";
                using (SQLiteCommand cmd = new SQLiteCommand(query, c))
                {
                    using (SQLiteDataReader dr = cmd.ExecuteReader())
                    {
                        int Contact;
                        while (dr.Read())
                        {
                            string _IP = dr.GetString(0);
                            string _IO_I = dr.GetString(1);
                            string _BuildingName = dr.GetString(4);
                            int IO_I = Convert.ToInt32(dr.GetString(1).Replace("DI ", ""));
                            if (dr.GetString(3) == "NC")
                            {
                                Contact = 1;
                            }
                            else
                            {
                                Contact = 0;
                            }
                            _tableName = dr.GetString(8);
                            string _name = dr.GetString(5);
                            var _active = dr.GetString(6);
                            var _status = dr.GetString(7);
                            if (_active == "Yes" && _status == "Enable")
                            {
                                 //Some condition check here
                            }

        }
        catch { }
    }

Writing:

void contexMenuuu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        data = "";
        ToolStripItem item = e.ClickedItem;
        using (SQLiteConnection c = new SQLiteConnection(dbconnection))
        {
            c.Open();
            string sql = "select * from " + Properties.Settings.Default.TableName + "(nolock) where Name= '" + Properties.Settings.Default.LabelName.Replace(" DO", "") + "' ";
            using (SQLiteCommand cmd = new SQLiteCommand(sql, c))
            {
                using (SQLiteDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        _controllerIP = dr.GetString(0);
                        _IO = dr.GetString(1);
                        _IO_O = dr.GetString(2).Replace("DO ", "");
                        _Name = dr.GetString(4);
                        _Interval = Convert.ToInt32(dr.GetString(9));
                    }
                }
            }
        }
        if (item.Text == "Bypass Enable")
        {
            using (SQLiteConnection c = new SQLiteConnection(dbconnection))
            {
                //c.DefaultTimeout = 2000;
                c.Open();
                string sql = "update ALL_IO SET Active='Yes', Status='Bypass' where ControllerIP='" + _controllerIP + "' and DI='" + _IO + "';";
                using (SQLiteCommand cmd = new SQLiteCommand(sql, c))
                {
                    lock (lockobj)
                    {
                        //SQLiteConnection.ClearAllPools();
                        cmd.ExecuteNonQuery(); //Error occur here
                    }                     
                }

            }

        }

Once the functionality is finished you must close the Database connect for avoid database lock issue. For executing the query you opened the database connection after that you didn't close it. so you need to close.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM