vb.net - Diagnosing cause of SQLite locking -
on vb.net application, have case users actions in 1 portion of form creating sqlite lock causes problems later in application (in case, closing down).
here sub-routine called when users add data list of items printed:
private sub btnadd_click(byval sender system.object, byval e system.eventargs) handles btnadd.click ' add item printqueue => regenerate listprint.items dim queueitem new dictionary(of string, string) queueitem("quantity") = inputquantity.value.tostring queueitem("description") = textdesc.text queueitem("sizeuk") = inputsize.value.tostring.replace(".5", "½").replace(".0", "") queueitem("sku") = liststyles.selecteditem.tostring queueitem("colour") = textcolour.text queueitem("date") = textdatecode.text queueitem("name") = textname.text try queueitem("sizeeu") = sizeeu(inputsize.value).tostring.replace(".5", "½") catch ex exception queueitem("sizeeu") = "??" end try ' size: m = uk + 1; f = uk + 1.5 queueitem("sizeus") = (inputsize.value + 1.5 - (chksex.checkstate * 0.5)).tostring.replace(".5", "½") ' add image data string dblocalquery = new sqlitecommand("select * tblimages id ='" & liststyles.selecteditem.tostring & "'", dblocal) if dblocal.state = connectionstate.closed dblocal.open() end if dim r sqlitedatareader = dblocalquery.executereader() try r.read() queueitem("image") = byte2string((r("image"))) catch ex exception queueitem("image") = settings("imgnotfound") if dblocal.state = connectionstate.open dblocal.close() end if end try printqueue.add(printqueue.count + 1, queueitem) msgframe.items.add(printqueue(printqueue.count)("sku") & " x" & printqueue(printqueue.count)("quantity")) msgframe.selectedindex = msgframe.items.count - 1 ' update print queue list populatelist() end sub
the table in database touched query tblimages. then, connection closed once finished.
when closing form down, function called captures data written dictionary called settings on load , reinserts database.
public function dblocalsave() boolean 'recreates tblsettings current values stored in settings try if dblocal.state = connectionstate.closed dblocal.open() end if if dblocal.state = connectionstate.open msgframe.items.add("flushing local settings table") msgframe.selectedindex = msgframe.items.count - 1 each pair in settings debug.print("+ tblsettings: " & pair.key & " = " & pair.value) dblocalquery = new sqlitecommand("delete tblsettings name = '" & pair.key & "';", dblocal) dblocalquery.executenonquery() dblocalquery = new sqlitecommand("insert tblsettings (`name`,`value`) values ('" & pair.key & "','" & pair.value & "');", dblocal) dblocalquery.executenonquery() next return true else return false end if catch sqlex sqliteexception messagebox.show(sqlex.message, "sql error", messageboxbuttons.ok, messageboxicon.error) catch ex exception messagebox.show(ex.message, "error", messageboxbuttons.ok, messageboxicon.error) if dblocal.state = connectionstate.open dblocal.close() end if end try end function
this function fails @ dblocalquery.executenonquery()
stage db lock error, cannot understand why. not handling db closing correctly? under impression sqlite created locks on writes, never happens in explicit case described.
if not 'add' item print queue (fundamental functionality of program), program closes gracefully.
dispose each command after executing should solve problem
dblocalquery.executenonquery() dblocalquery.dispose()
the problem because have delete , insert command concurrently executing causing error
dblocalquery = new sqlitecommand("delete tblsettings name = '" & pair.key & "';", dblocal) dblocalquery.executenonquery() 'insert here ' dblocalquery.dispose() dblocalquery = new sqlitecommand("insert tblsettings (`name`,`value`) values ('" & pair.key & "','" & pair.value & "');", dblocal) dblocalquery.executenonquery() 'here ' dblocalquery.dispose()
Comments
Post a Comment