Delphi, Firebird, FibPlus. Part 2 – Datasets and Transactions

BDE uses TTable component to access database tables; FibPlus uses TpFIBDataset component for the same purpose. The TpFIBDataset settings are more complicated than TTable settings. Let us consider the transactional settings of TpFIBDataset.
The things that should be taken into account first are TpFIBDataset.Transaction and TpFIBDataset.UpdateTransaction properties. We must assign TpFIBTransaction components to these properties. When we assign a value to the TpFIBDataset.Transaction property, the TpFIBDataset.UpdateTransaction property is automatically assigned the same value. If we don’t change the automatically assigned TpFIBDataset.UpdateTransaction value, the TpFIBDataset works in a single transaction context. If we assign a different TpFIBTransaction component to TpFIBDataset.UpdateTransaction property, we have separate read and write transactions, so that the “SELECT” queries are executed in the context of read transaction while “UPDATE” and “INSERT” queries are executed in the context of write transaction.
Another important property is TpFIBDataset.AutoCommit. If AutoCommit = False (default setting) we should call TpFIBDataset.UpdateTransaction.Commit or TpFIBDataset.UpdateTransaction.CommitRetaining manually to save any changes made in dataset. If we set AutoCommit = True, every successful TpFIBDataset.Post call automatically saves the changes like good old TTable component do. If we are working in a single transaction context the changes are saved by TpFIBDataset.UpdateTransaction.CommitRetaining call, and if we are using two transactions the changes are saved by TpFIBDataset.UpdateTransaction.Commit.
The Firebird professionals recommend to use two separate transactions for read/write operations – a long read transaction and a short write transaction. Usually it is sufficient to assign the same two transactions to all datasets in a database application.

Now let us come from theory to practice. Create a new VCL application (select “File->New->VCL Forms Application” in the Delphi main menu) and add a new data module to it (select “File->New->Other…”, in the “New Items” dialog select “Delphi Files” on the left panel and “Data Module” on the right panel, click “OK” button):

Drop TpFIBDatabase component and two TpFIBTransaction components to the data module. Set the Name property of TpFIBDatabase to dbElLib, the Name property of the first TpFIBTransaction component to trRead (read transaction) and the Name property of the second TpFIBTransaction component to trWrite (write transaction):

Assign the database (d:\ellib\ellib.fdb) we created in Part 1 to the ElLib.DBName property. I have also changed ElLib.LibraryName property from default “gds32.dll” to “C:\Firebird\Firebird_2_1\bin\fbclient.dll” – this step is not nessessary, but logical since gds32.dll is meant for legacy applications. Assign trRead to dbElLib.DefaultTransaction and trWrite to dbElLib.DefaultUpdateTransaction:

Set trRead.TRParams property to

read
nowait
read_committed
rec_version

Set trWrite.TRParams property to

write
nowait
concurrency

Drop a TpFIBDataset component to the same data module. All we have to do to set Transaction and UpdateTransaction properties of the TpFIBDataset component is to assign the TpFIBDatabase component to TpFIBDataset.Database property. One can see in object inspector that Transaction and UpdateTransaction properties are assigned automatically by trRead and trWrite transactions. If we don’t want to commit changes manually we must also set TpFIBDataset.AutoCommit = True:

So we see that after properly setting TpFIBDatabase and two TpFIBTransaction component’s properties we can set TpFIBDataset component’s transaction properties in two mouse clicks – the first click is to set Database property and the second click is to set AutoCommit = True.

2 thoughts on “Delphi, Firebird, FibPlus. Part 2 – Datasets and Transactions

  1. I have added the article to
    Firebirdnews.org

    could you add some screenshots to the second article ?

    also would you mind to join the
    MindTheBird.com campain for firebird 2.5 release party

Leave a comment