Recent

Author Topic: Multiply TSQLConnections in Multithreading [Solution]  (Read 2576 times)

DimProfi

  • Full Member
  • ***
  • Posts: 126
    • http://www.dimprofi.de
Multiply TSQLConnections in Multithreading [Solution]
« on: February 14, 2015, 02:04:57 am »
I made some research because of one very tricky SIGSERV.
And found a solution, which I would like to share, because there were nothing in Google about it  ::)

Its valid for Windows and Linux (both tested).


Example:

We got 10 threads and every of it creates its own TSQLConnection like
Code: [Select]
conn:=TMySQL55Connection.Create(nil);
If we start this threads extremly simultaneously,

Code: [Select]
Thread1.Run();
  Thread2.Run();
  Thread3.Run();
  Thread4.Run();
  ...
  Thread10.Run();

we will get SIGSERV on TSQLConnection.ExecuteDirect(), TSQLQuery.ExecSQL(), TDatset.Open() in all threads, rather on recreating all objects incl. TSQLConnection after an exception.

The problem is a simultaneous loading of the same dynamic library in every thread, while creating TSQLConnection.
This action is not thread safe.


The solution is to use TSQLDBLibraryLoader before starting any threads and creating TSQLConnection in it.

I made this way:

Code: [Select]
  SQLDBLibraryLoader1:=TSQLDBLibraryLoader.Create(nil);
  SQLDBLibraryLoader1.ConnectionType:='MySQL 5.5';

  {$IFDEF UNIX}
  SQLDBLibraryLoader1.LibraryName:='libmysqlclient.so.18';
  {$ELSE}
  SQLDBLibraryLoader1.LibraryName:='libmysql.dll';
  {$ENDIF}

  SQLDBLibraryLoader1.Enabled := true;
  SQLDBLibraryLoader1.LoadLibrary;

  Thread1.Run();
  Thread2.Run();
  Thread3.Run();
  Thread4.Run();
  ...
  Thread10.Run();


Is there a better solution, which I dont know?  ::)
« Last Edit: February 14, 2015, 02:07:38 am by DimProfi »
Lazarus 1.2/FPC 2.6.4 (x86/x86_64/win32/win64/Linux) :: Be smart - use predictable {$INTERFACES CORBA}! :)

 

TinyPortal © 2005-2018