Connection issue

Have some trouble establishing a DB connection. Or rather, making a common connection routine. I have 2 functions:



function GetAllSSones(): TObjectList<TSSone>;

var

MyConnection: IDBConnection;

SQLConn: TSQLConnection;

manager: TObjectManager;

dt: TDateTime;

s: Integer;

begin

SQLConn := TSQLConnection.Create(nil);

SQLConn.DriverName := 'MSSQL';

SQLConn.ConnectionName := 'LOCALSQL';

SQLConn.LoadParamsFromIniFile('FDConnectionDefs.ini');

SQLConn.LoginPrompt := False;

MyConnection := TDBExpressConnectionAdapter.Create(SQLConn, False);

manager := TObjectManager.Create(MyConnection);

manager.OwnsObjects := False;

Result := manager.FindAll<TSSone>;

manager.Free;

MyConnection.Disconnect;

MyConnection:=nil;

SQLConn.Close;

SQLConn.Free;

end;







function SaveSSone(p: TSSone): TSSone;

var

MyConnection: IDBConnection;

SQLConn: TSQLConnection;

manager: TObjectManager;

begin

try

    try

      SQLConn := TSQLConnection.Create(nil);

      SQLConn.DriverName := 'MSSQL';

      SQLConn.ConnectionName := 'LOCALSQL';

      SQLConn.LoadParamsFromIniFile('FDConnectionDefs.ini');

      SQLConn.LoginPrompt := False;

      MyConnection := TDBExpressConnectionAdapter.Create(SQLConn, False);

      manager := TObjectManager.Create(MyConnection);

      manager.OwnsObjects := False;

      manager.SaveOrUpdate(p);

    except

      Result := nil;

    end;

finally

    try

      manager.Flush;

      manager.Free;

      MyConnection.Disconnect;

      MyConnection:=nil;

      SQLConn.Close;

      SQLConn.Free;



    except

    end;

end;

Result := p;

end;





These work just fine. I will be making more of these, so I wanted to put the connection in its own routine:





var

SQLConn: TSQLConnection;



implementation





procedure GetOpenSQLConnection();

begin

if SQLConn = nil then

begin

    SQLConn := TSQLConnection.Create(nil);

    SQLConn.DriverName := 'MSSQL';

    SQLConn.ConnectionName := 'LOCALSQL';

    SQLConn.LoadParamsFromIniFile('FDConnectionDefs.ini');

    SQLConn.LoginPrompt := False;

end;

if SQLConn.Connected = false then

begin

    SQLConn.Open;

end;

end;



procedure CloseSQLConnection();

begin

SQLConn.Close;

SQLConn.Free;

SQLConn:= nil;

end;





And change my functions:





function GetAllSSones(): TObjectList<TSSone>;

var

MyConnection: IDBConnection;

manager: TObjectManager;

dt: TDateTime;

s: Integer;

begin

GetOpenSQLConnection();

MyConnection := TDBExpressConnectionAdapter.Create(SQLConn, False);

manager := TObjectManager.Create(MyConnection);

manager.OwnsObjects := False;

Result := manager.FindAll<TSSone>;

manager.Free;

MyConnection.Disconnect;

MyConnection:=nil;

CloseSQLConnection();

end;



function SaveSSone(p: TSSone): TSSone;

var

MyConnection: IDBConnection;

manager: TObjectManager;

begin

try

    try

      GetOpenSQLConnection();

      MyConnection := TDBExpressConnectionAdapter.Create(SQLConn, False);

      manager := TObjectManager.Create(MyConnection);

      manager.OwnsObjects := False;

      manager.SaveOrUpdate(p);

    except

      Result := nil;

    end;

finally

    try

      manager.Flush;

      manager.Free;

      MyConnection.Disconnect;

      MyConnection:=nil;

      CloseSQLConnection();

    except

    end;

end;

Result := p;

end;





Now, after running GetAllSSones successfully once, it crashes when trying to establish a connection for SaveSSone (Meaning it crashes the second time i try to use the connection). I have also tried not closing the connection at all between operations, but same problem. "Read of address ...." error on second attempt to reach DB. Anyone?

Your code doesn't seem wrong, but maybe a closer debug is needed. If you are passing the second parameter false while creating the dbexpress adapter, your TSQLConnection won't be touched by Aurelius (destroyed, I mean), so either it's being destroyed by something else, or your code is not correctly recreating it, or maybe the problem is not with connection? Where is the exact line where the AV is raised?

Jostein, why don't you try to build a sample project that I could compile here (no 3rd party controls) so maybe I can try debug and see what's wrong?

Hi. I did just that, built a tiny sample Project to provoke the error, but it worked fine! So i rebuildt the original from scratch, and now it is working. I can't really say what the issue was, but thanks for replying. :)

Wish delphi would give me some more info than memory allocation errors just ONCE....