论坛里与事务相关的贴子我已经基本都看过了。但是在实际应用中发现用以下的方式写的代码会在执行查询时报错。
var
qry, qryIn: TQuery;
lHandle: TTransactionHandle;
begin
qry := TQuery.Create(self);
qryIn := TQuery.Create(self);
try
qry.Connection := DataSetBrokerInDepotBill.DataSet.Connection; //DataSetBrokerInDepotBill在信息层声明,通过ConnectionString连接数据库。
qryin.Connection := DataSetBrokerInDepotBill.DataSet.Connection;
lHandle := DataSetBrokerInDepotBill.DataSet.Connection.Transaction.Start(True);
try
qry.CommandText := 'select distinct jizu_unit, machine_sort_id, ' +
' project_id, is_installed, SubjectID, NeedTaxRate ' +
' from in_depot_bill ' +
' where type = 2 and coalesce(Offseted, 0) = 0 and ' +
' contract_id = ' + IntToStr(AContractID);
qry.Open;
while not qry.Eof do
begin
qryIn.CommandText := 'Insert Into in_depot_bill (...) values (...)'; //实际内容忽略
qryIn.Execute;
qry.Next;
end;
with DataSetBrokerInDepotBill.DataSet do
begin
Edit;
FieldByName('Offseted').AsInteger := 1;
Post;
ApplyUpdates;
Connection.Transaction.Commit(lHandle);
end;
except
On E:Exception do
begin
DataSetBrokerInDepotBill.DataSet.Connection.Transaction.RollBack(lHandle);
jsDialogs.ShowWarning(E.Message, Caption);
exit;
end;
end;
finally
qry.Free;
qryIn.Free;
end;
end;
首次执行有时不会报错,但不关闭程序的情况下,再次执行到第一个查询语句就会报错了。是后台Tomcat报出的错误。具体错误信息“---------------------------
X3 Studio
---------------------------
运行系统初始化的时候发生异常:java.lang.Exception
类型:Exception。
---------------------------
确定
---------------------------”
我想问的是:是不是事务的句柄需要释放,或者有不该释放的被释放掉了? |