lazarus - SQLTransaction2 : Operation cannot be performed on an active transaction FreePascal - Code Typhon -
i solving error message.
i using code typhon32 v4.9 on windows 7 pro.
i had project connecting firebird database, single form works fine- no problems.
i added 2nd form data entry form linked database in firebird - need both databases in 1 project.
since adding 2nd form getting following error when close code typhon , when close form1 while using debugger: "sqltransaction2 : operation cannot performed on active transaction"
i have tried numerous of solution found on internet can not solve it.
i have button open 2nd follows:
procedure tform1.bitbtn1click(sender: tobject); begin frmcontributions := tfrmcontributions.create(nil); try frmcontributions.showmodal; frmcontributions.free; end; end;
here 2nd form code:
unit ufirebirddemo1; {$mode objfpc}{$h+} interface uses classes, sysutils, sqldb, ibconnection, pqconnection, db, fileutil, forms, controls, graphics, dialogs, stdctrls, dbgrids, dbctrls; type tfrmcontributions = class(tform) btnupdate: tbutton; btndeleteprogrammer: tbutton; dsprogrammer: tdatasource; dbgrdprogrammer: tdbgrid; dbnavprogrammer: tdbnavigator; ibconnection2: tibconnection; sqlqprogrammer: tsqlquery; sqlscript1: tsqlscript; sqltransaction2: tsqltransaction; procedure btndeleteprogramclick(sender: tobject); procedure btnupdateclick(sender: tobject); procedure btnupdateprogramsclick(sender: tobject); procedure btndeleteprogrammerclick(sender: tobject); procedure dbgrdcombinedtitleclick(column: tcolumn); procedure dbgrdprogrammertitleclick(column: tcolumn); procedure dbgrdprogramstitleclick(column: tcolumn); procedure edtsearchchange(sender: tobject); procedure savechanges; procedure formclose(sender: tobject; var closeaction: tcloseaction); end; var frmcontributions: tfrmcontributions; implementation {$r *.lfm} procedure tfrmcontributions.btnupdateclick(sender: tobject); begin sqlqprogrammer.edit; sqlqprogrammer.post; sqlqprogrammer.applyupdates(1); sqltransaction2.commitretaining; end; procedure tfrmcontributions.btndeleteprogrammerclick(sender: tobject); begin sqlscript1.script.text:= 'delete programmer id = ' + dbgrdprogrammer.columns[0].field.asstring + ';'; sqlscript1.execute; sqltransaction2.commitretaining; sqlqprogrammer.refresh; end; procedure tfrmcontributions.dbgrdprogrammertitleclick(column: tcolumn); begin sqlqprogrammer.close; sqltransaction2.active := true; sqlqprogrammer.sql.text := 'select * programmer order id desc'; sqlqprogrammer.open; end; procedure tfrmcontributions.savechanges; // saves edits done user, if any. begin try if sqltransaction2.active // if within started transaction // otherwise "operation cannot performed on inactive dataset" begin sqlqprogrammer.applyupdates; //pass user-generated changes database... sqltransaction2.commit; //... , commit them using transaction. //sqltransaction2.active false end; except on e: edatabaseerror begin messagedlg('error', 'a database error has occurred. technical error message: ' + e.message, mterror, [mbok], 0); end; end; end; procedure tfrmcontributions.formclose(sender: tobject; var closeaction: tcloseaction); begin savechanges; sqlqprogrammer.close; sqltransaction2.commit; sqltransaction2.rollback; sqltransaction2.active := false; ibconnection2.connected := false; end; end.
i tried adding formclose first form follows in order solved problem not seem doing anything:
procedure tform1.formclose(sender: tobject; var closeaction: tcloseaction); begin frmcontributions.savechanges; frmcontributions.sqltransaction2.rollback; frmcontributions.sqltransaction2.active := false; frmcontributions.ibconnection2.connected := false; end;
i have tried different solutions found on internet still getting:
"sqltransaction2 : operation cannot performed on active transaction" when close project using debugger, , same error when closing down code typhon.
here lfm file:
object frmcontributions: tfrmcontributions left = 816 height = 415 top = 172 width = 774 caption = 'data entry promo' clientheight = 415 clientwidth = 774 color = clhighlight onclose = formclose lclversion = '1.3' object btnupdate: tbutton left = 536 height = 25 top = 376 width = 168 caption = 'update programmers' onclick = btnupdateclick taborder = 0 end object dbgrdprogrammer: tdbgrid left = 16 height = 360 top = 0 width = 744 color = clwindow columns = < item title.caption = 'id' width = 60 fieldname = 'id' end item title.caption = 'datesent' fieldname = 'datesent' end item title.caption = 'course' width = 140 fieldname = 'course' end item title.caption = 'promotype' width = 140 fieldname = 'promotype' end item title.caption = 'link' width = 100 fieldname = 'link' end item title.caption = 'value' width = 50 fieldname = 'valuechart' end item title.caption = 'totalsent' fieldname = 'totalsent' end item title.caption = 'notes' fieldname = 'notes' end> datasource = dsprogrammer taborder = 1 ontitleclick = dbgrdprogrammertitleclick end object dbnavprogrammer: tdbnavigator left = 280 height = 25 top = 376 width = 241 bevelouter = bvnone childsizing.enlargehorizontal = crsscalechilds childsizing.enlargevertical = crsscalechilds childsizing.shrinkhorizontal = crsscalechilds childsizing.shrinkvertical = crsscalechilds childsizing.layout = ccllefttorightthentoptobottom childsizing.controlsperline = 100 clientheight = 25 clientwidth = 241 datasource = dsprogrammer options = [] taborder = 2 visiblebuttons = [nbfirst, nbprior, nbnext, nblast, nbinsert, nbrefresh] end object btndeleteprogrammer: tbutton left = 64 height = 25 top = 376 width = 201 caption = 'delete selected programmer' onclick = btndeleteprogrammerclick taborder = 3 end object dsprogrammer: tdatasource dataset = sqlqprogrammer left = 696 top = 168 end object ibconnection2: tibconnection connected = true loginprompt = false databasename = '.......contributions.fdb' keepconnection = false password = 'password' transaction = sqltransaction2 username = 'username' hostname = 'hostname' left = 696 top = 8 end object sqltransaction2: tsqltransaction active = true action = cacommitretaining database = ibconnection2 left = 696 top = 56 end object sqlqprogrammer: tsqlquery indexname = 'default_order' fielddefs = < item name = 'id' datatype = ftinteger precision = -1 size = 0 end item name = 'datesent' datatype = ftdate precision = -1 size = 0 end item name = 'promotype' datatype = ftstring precision = -1 size = 25 end item name = 'course' datatype = ftstring precision = -1 size = 25 end item name = 'link' datatype = ftstring precision = -1 size = 450 end item name = 'valuechart' datatype = ftinteger precision = -1 size = 0 end> active = true database = ibconnection2 transaction = sqltransaction2 sql.strings = ( 'select * programmer order id desc' ) updatesql.strings = ( '' ) insertsql.strings = ( '' ) deletesql.strings = ( '' ) params = <> updatemode = upwherechanged useprimarykeyaskey = false left = 696 top = 224 end object sqlscript1: tsqlscript database = ibconnection2 transaction = sqltransaction2 directives.strings = ( 'set term' 'commit' '#ifdef' '#ifndef' '#else' '#endif' '#define' '#undef' '#undefine' ) script.strings = ( '' ) terminator = ';' commentsinsql = true usesetterm = true usecommit = true usedefines = true left = 696 top = 112 end end
thank you.
actually don't know should next, but
(0) i'd move database code standalone tdatamodule (outside ui code) make code more straightforward read , maintain. during refactoring may spot hidden problem..
(1) http://forum.lazarus.freepascal.org/index.php?topic=14301.0 seems solve similar problem
(2) book lazarus, complete guide seems have chapter tdatamodule
(3) martin fowler wrote book refactoring , runs site same @ http://refactoring.com/
(4) components don't behave if active in design mode. different , more complicated scenario (lots of if
s csdesigning in componentstate
, sequence diagram of events different, random , should foolproof , re-entrant..). safe side production turn component's active state off in design mode , activate them in code in defined order anticipated component authors
(5) if experience error when in debugger , not appear in production code think can ignore problem debugging environment <see="(4)">
Comments
Post a Comment