Skip to content

Commit 301ad9b

Browse files
authored
Invalidate connections (#7)
1 parent f521b78 commit 301ad9b

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

MiniREST.SQL.Base.pas

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ TMiniRESTSQLConnectionFactoryBase = class abstract(TInterfacedObject, IMiniRES
4848
function GetQueueCount: Integer; virtual; abstract;
4949
function GetConnection(const AIdentifier: string): IMiniRESTSQLConnection; overload;
5050
function GetSingletonConnection: IMiniRESTSQLConnection;
51+
procedure InvalidateConnections;
5152
end;
5253

5354
{ TMiniRESTSQLConnectionBase }
@@ -56,9 +57,11 @@ TMiniRESTSQLConnectionBase = class abstract(TInterfacedObject, IMiniRESTSQLCon
5657
strict private
5758
FOwner: TObject;
5859
FConnectionID: Integer;
60+
FValid: Boolean;
5961
protected
6062
FName: string;
6163
FEstaNoPool: Boolean;
64+
procedure SetValid(const AValid: Boolean);
6265
function _Release: Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
6366
function GetObject: TObject; virtual; abstract;
6467
procedure SetOwner(AOwner: Pointer);
@@ -78,6 +81,8 @@ TMiniRESTSQLConnectionBase = class abstract(TInterfacedObject, IMiniRESTSQLCon
7881
function GetDatabaseInfo: IMiniRESTSQLDatabaseInfo; virtual; abstract;
7982
function InTransaction: Boolean; virtual; abstract;
8083
function GetConnectionID: Integer;
84+
function IsValid: Boolean;
85+
procedure Invalidate; virtual; abstract;
8186
end;
8287

8388
TMiniRESTSQLPrimaryKeyInfo = class(TInterfacedObject, IMiniRESTSQLPrimaryKeyInfo)
@@ -354,6 +359,7 @@ function TMiniRESTSQLConnectionFactoryBase.GetConnection(const AIdentifier: stri
354359
LConnection := InternalGetconnection.SetName('Connection' + IntToStr(FConnectionCounter));
355360
Inc(FConnectionCounter);
356361
Result := LConnection;
362+
TMiniRESTSQLConnectionBase(Result).SetValid(True);
357363
end
358364
else
359365
begin
@@ -379,6 +385,7 @@ function TMiniRESTSQLConnectionFactoryBase.GetConnection(const AIdentifier: stri
379385
LConnection := InternalGetconnection.SetName('Connection' + IntToStr(FConnectionCounter) + ' ' + AIdentifier);
380386
Inc(FConnectionCounter);
381387
Result := LConnection;
388+
TMiniRESTSQLConnectionBase(Result.GetObject).SetValid(True);
382389
end
383390
else
384391
begin
@@ -484,6 +491,30 @@ function TMiniRESTSQLConnectionFactoryBase.GetSingletonConnection: IMiniRESTSQLC
484491
Result := FSingletonConnection;
485492
end;
486493

494+
procedure TMiniRESTSQLConnectionFactoryBase.InvalidateConnections;
495+
var
496+
I: Integer;
497+
LConnectionObj: TMiniRESTSQLConnectionBase;
498+
LConnection: IMiniRESTSQLConnection;
499+
begin
500+
for I := 0 to (FConnectionsToNotifyFree.Count - 1) do
501+
begin
502+
LConnectionObj := FConnectionsToNotifyFree.Items[I];
503+
if LConnectionObj.GetInterface(IMiniRESTSQLConnection, LConnection) then
504+
LConnection.Invalidate;
505+
end;
506+
end;
507+
508+
function TMiniRESTSQLConnectionBase.IsValid: Boolean;
509+
begin
510+
Result := FValid;
511+
end;
512+
513+
procedure TMiniRESTSQLConnectionBase.SetValid(const AValid: Boolean);
514+
begin
515+
FValid := AValid;
516+
end;
517+
487518
initialization
488519
gConnectionIDCounter := 0;
489520

MiniREST.SQL.Intf.pas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ interface
5555
function SetName(const AName: string): IMiniRESTSQLConnection;
5656
function GetDatabaseInfo: IMiniRESTSQLDatabaseInfo;
5757
function GetConnectionID: Integer;
58+
function IsValid: Boolean;
59+
procedure Invalidate;
5860
end;
5961

6062
IMiniRESTSQLConnectionFactory = interface
@@ -66,6 +68,7 @@ interface
6668
function GetObject: TObject;
6769
function GetConnectionsCount: Integer;
6870
function GetQueueCount: Integer;
71+
procedure InvalidateConnections;
6972
property ConnectionsCount: Integer read GetConnectionsCount;
7073
property QueueCount: Integer read GetQueueCount;
7174
end;

MiniREST.SQL.SQLDb.pas

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ TMiniRESTSQLConnectionSQLDb = class(TMiniRESTSQLConnectionBase)
7575
function GetDriverName(const ADatabaseType: TMiniRESTSQLDatabaseType): string;
7676
procedure Log(Sender : TSQLConnection; EventType : TDBEventType; Const Msg : String);
7777
procedure SetMiniRESTSQLParamToSQLParam(AMiniRESTSQLParam: IMiniRESTSQLParam; ASQLParam: TParam);
78+
procedure CheckConnectionIsValid;
7879
public
7980
constructor Create(AOwner: IMiniRESTSQLConnectionFactory; AParams: IMiniRESTSQLConnectionFactoryParamsSQLDb);
8081
destructor Destroy; override;
@@ -89,6 +90,7 @@ TMiniRESTSQLConnectionSQLDb = class(TMiniRESTSQLConnectionBase)
8990
function Execute(const ACommand: string; AParams: array of IMiniRESTSQLParam): Integer; override;
9091
function GetDatabaseInfo: IMiniRESTSQLDatabaseInfo; override;
9192
function InTransaction: Boolean; override;
93+
procedure Invalidate; override;
9294
end;
9395

9496
{ TMiniRESTSQLQuerySQLDb }
@@ -224,6 +226,7 @@ procedure TMiniRESTSQLConnectionSQLDb.Connect;
224226
LName: string;
225227
I: Integer;
226228
begin
229+
CheckConnectionIsValid;
227230
LStringList := TStringList.Create;
228231
try
229232
if FSQLConnection.Connected then
@@ -247,6 +250,7 @@ procedure TMiniRESTSQLConnectionSQLDb.Connect;
247250

248251
procedure TMiniRESTSQLConnectionSQLDb.StartTransaction;
249252
begin
253+
CheckConnectionIsValid;
250254
if FTransaction.Active then
251255
FTransaction.Rollback;
252256
FTransaction.StartTransaction;
@@ -255,18 +259,21 @@ procedure TMiniRESTSQLConnectionSQLDb.StartTransaction;
255259

256260
procedure TMiniRESTSQLConnectionSQLDb.Commit;
257261
begin
262+
CheckConnectionIsValid;
258263
FTransaction.Commit;
259264
FInExplicitTransaction := False;
260265
end;
261266

262267
procedure TMiniRESTSQLConnectionSQLDb.Rollback;
263268
begin
269+
CheckConnectionIsValid;
264270
FTransaction.Rollback;
265271
FInExplicitTransaction := False;
266272
end;
267273

268274
function TMiniRESTSQLConnectionSQLDb.GetQuery: IMiniRESTSQLQuery;
269275
begin
276+
CheckConnectionIsValid;
270277
Result := TMiniRESTSQLQuerySQLDb.Create(Self);
271278
end;
272279

@@ -293,6 +300,7 @@ function TMiniRESTSQLConnectionSQLDb.Execute(const ACommand: string; AParams: ar
293300
LParam: TParam;
294301
LMiniRESTSQLParam: IMiniRESTSQLParam;
295302
begin
303+
CheckConnectionIsValid;
296304
Self.Connect;
297305
LQry := TSQLQuery.Create(nil);
298306
try
@@ -321,6 +329,7 @@ function TMiniRESTSQLConnectionSQLDb.Execute(const ACommand: string; AParams: ar
321329

322330
function TMiniRESTSQLConnectionSQLDb.GetDatabaseInfo: IMiniRESTSQLDatabaseInfo;
323331
begin
332+
CheckConnectionIsValid;
324333
Result := nil;
325334
case FConnectionParams.GetDatabaseType of
326335
dbtFirebird: Result := TMiniRESTSQLDatabaseInfoFirebird.Create(Self);
@@ -523,6 +532,7 @@ procedure TMiniRESTSQLConnectionSQLDb.SetMiniRESTSQLParamToSQLParam(AMiniRESTSQL
523532

524533
function TMiniRESTSQLConnectionSQLDb.InTransaction: Boolean;
525534
begin
535+
CheckConnectionIsValid;
526536
Result := FSQLConnection.Transaction.Active;
527537
end;
528538

@@ -538,4 +548,17 @@ function TMiniRESTSQLConnectionFactorySQLDb.GetQueueCount: Integer;
538548
Result := 0;
539549
end;
540550

551+
procedure TMiniRESTSQLConnectionSQLDb.Invalidate;
552+
begin
553+
SetValid(False);
554+
FreeAndNil(FSQLConnection);
555+
FreeAndNil(FTransaction);
556+
end;
557+
558+
procedure TMiniRESTSQLConnectionSQLDb.CheckConnectionIsValid;
559+
begin
560+
if not IsValid then
561+
raise Exception.Create('A conexão foi invalidada.');
562+
end;
563+
541564
end.

unittest/SQL/Test.SQL.Default.pas

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ TMiniRESTSQLTest = class({$IFNDEF FPC}TObject{$ELSE}TTestCase{$IFEND})
100100
[Test]
101101
{$IFEND}
102102
procedure TestGetSingletonConnection;
103+
{$IFNDEF FPC}
104+
[Test]
105+
{$IFEND}
106+
procedure TestConnectionIsValid;
107+
{$IFNDEF FPC}
108+
[Test]
109+
{$IFEND}
110+
procedure TestConnectionIsNotValid;
103111
(* {$IFNDEF FPC}
104112
[Test]
105113
{$IFEND}
@@ -150,8 +158,9 @@ procedure TMiniRESTSQLTest.TearDown;
150158
var
151159
LConnection: IMiniRESTSQLConnection;
152160
begin
153-
LConnection := FConnectionFactory.GetConnection;
161+
(* LConnection := FConnectionFactory.GetConnection;
154162
LConnection.Execute('DELETE FROM CUSTOMER', []);
163+
*)
155164
FConnectionPoolEvents.Free;
156165
end;
157166

@@ -713,4 +722,34 @@ procedure TMiniRESTSQLTest.TestGetSingletonConnection;
713722
{$IFEND}
714723
end;
715724

725+
procedure TMiniRESTSQLTest.TestConnectionIsValid;
726+
var
727+
LConn1: IMiniRESTSQLConnection;
728+
begin
729+
LConn1 := FConnectionFactory.GetConnection;
730+
{$IFNDEF FPC}
731+
Assert.IsTrue(LConn1.IsValid, 'LCon1 não está válida.');
732+
{$ELSE}
733+
CheckTrue(LConn1.IsValid, 'LCon1 não está válida.');
734+
{$IFEND}
735+
end;
736+
737+
procedure TMiniRESTSQLTest.TestConnectionIsNotValid;
738+
var
739+
LConn1: IMiniRESTSQLConnection;
740+
begin
741+
LConn1 := FConnectionFactory.GetConnection;
742+
{$IFNDEF FPC}
743+
Assert.IsTrue(LConn1.IsValid, 'LCon1 não está válida.');
744+
{$ELSE}
745+
CheckTrue(LConn1.IsValid, 'LCon1 não está válida.');
746+
{$IFEND}
747+
FConnectionFactory.InvalidateConnections;
748+
{$IFNDEF FPC}
749+
Assert.IsFalse(LConn1.IsValid, 'LCon1 está válida.');
750+
{$ELSE}
751+
CheckFalse(LConn1.IsValid, 'LCon1 está válida.');
752+
{$IFEND}
753+
end;
754+
716755
end.

unittest/TEST.FDB

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)