#1 2020-03-19 21:49:04

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 516

BUG NewStatementPrepared - AddObject

Hi AB i found a litte BUG in NewStatementPrepared:

see the line with // itSDS

function TSQLDBConnection.NewStatementPrepared(const aSQL: RawUTF8;
  ExpectResults, RaiseExceptionOnError, AllowReconnect: Boolean): ISQLDBStatement;
var Stmt: TSQLDBStatement;
    ToCache: boolean;
    ndx,altern: integer;
    cachedSQL: RawUTF8;

  procedure TryPrepare(doraise: boolean);
  var Stmt: TSQLDBStatement;
  begin
    Stmt := nil;
    try
      InternalProcess(speActive);
      try
        Stmt := NewStatement;
        Stmt.Prepare(aSQL,ExpectResults);
        if ToCache then begin
          if fCache=nil then
            fCache := TRawUTF8List.Create([fObjectsOwned,fNoDuplicate,fCaseSensitive]);
          if fCache.AddObject(aSQL,Stmt)>=0 then			// itSDS
            Stmt._AddRef else // will be owned by fCache.Objects[]
            SynDBLog.Add.Log(sllWarning,'NewStatementPrepared: unexpected '+
              'cache duplicate for %',[Stmt.SQLWithInlinedParams],self);
        end;
        result := Stmt;
      finally
        InternalProcess(speNonActive);
      end;
    except
      on E: Exception do begin
        with SynDBLog.Add do
          if [sllSQL,sllDB,sllException,sllError]*Family.Level<>[] then
            LogLines(sllSQL,pointer(Stmt.SQLWithInlinedParams),self,'--');
        Stmt.Free;
        result := nil;
        StringToUTF8(E.Message,fErrorMessage);
        fErrorException := PPointer(E)^;
        if doraise then
          raise;
      end;
    end;
  end;

the former used cachedSQL - Value is sometimes empty


Rad Studio 12.1 Santorini

Offline

#2 2020-03-20 10:08:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,673
Website

Re: BUG NewStatementPrepared - AddObject

You are right: there was a problem here!

But your fix won't handle SQL collisions and multiple prepared statements generation (i.e. use another prepared statement if it is already in use).

I guess the correct fix may be https://synopse.info/fossil/info/ac5dc8e082
The multiple preparation of statements has now a specific parameter in properties, and is disabled and logged by default: the ISQLDBStatement instance should be released ASAP.

Offline

Board footer

Powered by FluxBB