dualarrow

Sample multithreded TMS ORM for MariaDb

Mar 12th, 2021 (edited)
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.96 KB | None | 0 0
  1. unit Unit2;
  2.  
  3. interface
  4.  
  5. uses
  6.   Aurelius.Sql.Register,
  7.   Aurelius.Sql.MySQL,
  8.   Aurelius.Schema.MySQL,
  9.   Aurelius.Engine.ObjectManager,
  10.   Aurelius.Engine.DatabaseManager,
  11.   Aurelius.Drivers.Interfaces,
  12.   Aurelius.Drivers.FireDac,
  13.  
  14.  
  15.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  16.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.VCLUI.Wait, Data.DB,
  17.   FireDAC.Comp.Client, FireDAC.Phys.FB, FireDAC.Phys.FBDef, Vcl.StdCtrls, FireDAC.Phys.ODBC, FireDAC.Phys.ODBCDef, FireDAC.Phys.MySQL, FireDAC.Phys.MySQLDef, Vcl.ExtCtrls;
  18.  
  19. type
  20.   TForm2 = class(TForm)
  21.     Label1: TLabel;
  22.     Timer1: TTimer;
  23.     procedure FormCreate(Sender: TObject);
  24.     procedure Timer1Timer(Sender: TObject);
  25.   end;
  26.  
  27. var
  28.   Form2: TForm2;
  29.  
  30. implementation
  31.  
  32. uses
  33.   Threading,
  34.   SyncObjs,
  35.   MariaData;
  36.  
  37. {$R *.dfm}
  38.  
  39. var
  40.   Generator: TMySqlSQLGenerator;
  41.   cnt: integer = 0;
  42.   Sem: TCriticalSection;
  43.   ready: boolean = false;
  44.  
  45. procedure Run(ref: integer);
  46. var
  47.   lConnectionIntf: IDBConnection;
  48.   lDatabaseManager: TDatabaseManager;
  49.   lObjectManager: TObjectManager;
  50.   procedure Prepare;
  51.   var
  52.     lConnection: TFDConnection;
  53.   begin
  54.       lConnection := TFDConnection.Create(nil);
  55.       lConnection.DriverName := 'MySql';
  56.       lConnection.Params.Database := 'Test';
  57.       lConnection.Params.DriverId := 'MySql';
  58.       lConnection.Params.Password := 'lartsek';
  59.       lConnection.Params.UserName := 'root';
  60.       lConnectionIntf := TFireDacConnectionAdapter.Create(lConnection, 'MySql', false);
  61.   end;
  62.   procedure Open;
  63.   begin
  64.     if ref = 1 then   // only the 1st one can create/alter the schema
  65.     begin
  66.       lDatabaseManager := TDatabaseManager.Create(lConnectionIntf);
  67.       lDatabaseManager.UpdateDatabase;
  68.     end;
  69.     lObjectManager := TObjectManager.Create(lConnectionIntf);
  70.     ready := true;
  71.   end;
  72.  
  73.   procedure Close;
  74.   begin
  75.     lObjectManager.Free;
  76.     if ref = 1 then
  77.       lDatabaseManager.Free;
  78.   end;
  79. begin
  80.   Prepare;
  81.   Open;
  82.   for var i:=1 to 1000 do
  83.   begin
  84.     Sem.Enter;
  85.     inc(cnt);  // protect the counter
  86.     Sem.Leave;
  87.     var lTransaction := lConnectionIntf.BeginTransaction;
  88.     var x := TTest.Create;
  89.     x.data := 'Testing ' + i.ToString;
  90.     lObjectManager.Save(x);
  91.     lObjectManager.Flush;
  92.     lTransaction.Commit;
  93.   end;
  94.   Close;
  95. end;
  96.  
  97.  
  98. procedure TForm2.FormCreate(Sender: TObject);
  99. begin
  100.   Sem := TCriticalSection.Create;
  101.   Generator := (TSQLGeneratorRegister.GetInstance.GetGenerator('MySql') as TMySqlSQLGenerator);
  102.  
  103.   TTask.Run(procedure begin Run(1); end);
  104.   while not ready do
  105.     sleep(10);
  106.   for var j := 2 to 10 do
  107.     TTask.Run(procedure begin Run(j); end);
  108. end;
  109.  
  110. procedure TForm2.Timer1Timer(Sender: TObject);
  111. begin
  112.   sem.Enter;
  113.   Label1.Caption := cnt.ToString;
  114.   sem.Leave;
  115. end;
  116.  
  117. end.
Add Comment
Please, Sign In to add comment