#1 2015-06-25 06:36:41

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

database connections handling

Hi Ab,

I have a server which I initialize as follows below. In my model I have a SQLRecord class which is TMessart. So, I start my server and do some REST requests from a client.

Q1: My REST requests works fine and I get data, but each time I send a http request to the server a new database connection is opened which is not closed after the request. Do you any idea how I can handle this?

Q2: In the code below if when ServerHTTP.Free is called an access violation occurs. If I debug into the method, the error is raised in THttpApiServer.Destroy the code check if fServerSessionID<>0 then begin
Is it a bug, or am I doing something wrong?

  // INITIALIZE THE SERVER
  DatabaseConnectionProperties := TSQLDBConnectionProperties.Create;

  Model := CreateModel;
  VirtualTableExternalRegister(Model, TMessart, DatabaseConnectionProperties, 'testdb.messart');

  ServerDB := TSQLRestServerDB.Create(Model, ChangeFileExt(ExeVersion.ProgramFileName, '.db3'), False);
  ServerDB.CreateMissingTables;

  ServerHTTP := TSQLHttpServer.Create('7979', [ServerDB], '+', useHttpApiRegisteringURI);
  ServerHTTP.AccessControlAllowOrigin := '*'; // allow cross-site AJAX queries  
  // DO SOME REST requests
  http://localhost:7979/root/messart/1
  http://localhost:7979/root/messart/2
  http://localhost:7979/root/messart/3
  ...
  ... and so on
  // SHUTDOWN THE SERVER
  DatabaseConnectionProperties.Free;
  Model.Free;
  ServerDB.Free;
  ServerHTTP.Free;

Offline

#2 2015-06-25 06:42:44

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

Re: database connections handling

Q1. There is a connection per thread.
Once each thread of the thread pool has its connection, there won't be any more connection.
You may use a single thread for all ORM process - see http://synopse.info/files/html/Synopse% … #TITLE_195

Q2. You should free the instances in the REVERSE order of the initialization, not the same order, of course!

// SHUTDOWN THE SERVER
  ServerHTTP.Free;
  ServerDB.Free;
  Model.Free;
  DatabaseConnectionProperties.Free;

Offline

#3 2015-06-25 07:24:17

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

Re: database connections handling

Great! It works! Thank you very much for your fast answer!

Offline

Board footer

Powered by FluxBB