FireDAC.IFDPhysConnection.Transactions Sample
This sample demonstrates the use of the IFDPhysTransaction interface to manage database transactions.
Contents
Location
You can find the Transactions sample project at:
- Start | Programs | Embarcadero RAD Studio Athens | Samples and then navigate to:
Object Pascal\Database\FireDAC\Samples\Phys Layer\IFDPhysConnection\Transactions
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
Description
The Transactions sample shows you how to use the IFDPhysTransaction interface to manage database transactions. To this end, the sample makes use of the following properties and methods of IFDPhysTransaction:
- The Isolation property, which is used for setting up the transaction isolation level for the transactions that FireDAC manages.
- The StartTransaction method, which is used to start a new database transaction.
- The Commit method, which is used to permanently store modifications made in the current transaction to the database.
- The Rollback method, which is used to cancel all modifications made in the current transaction to the database.
How to Use the Sample
- Navigate to the location given above and open
IFDPhys_Transactions.dproj
. - Press F9 or choose Run > Run.
Files
File in Delphi | Contains |
---|---|
|
The project itself. |
|
The main form. |
Implementation
When you run the application, clik the Use Connection Definition combo box and select an option in order to define a connection. When the connection is defined, the sample uses the CreateCommand method of IFDPhysConnection to create a command interface. Then, the sample implements the following database transaction features in run time:
- Set up the transaction isolation level
- The sample sets the Isolation property of IFDPhysTransaction to
xiReadCommitted
to allow the reading of committed (permanent) changes made to the database by other simultaneous transactions. This is the default value of the Isolation property. - Start a transaction
- The sample uses the StartTransaction method to start a new database transaction. During the transaction, the sample uses the Prepare method of IFDPhysCommand to execute a
DELETE
command. The SQL command is the following:'delete from {id FDQA_TransTable}'
. - Commit the transaction
- The sample uses the Commit method to permanently store modifications made in the current transaction to the database.
- Rollback the transaction
- The sample uses the Rollback method to cancel all the modifications made in the current transaction.
- Note: The sample use this method if an exception is raised.
Uses
See Also
- FireDAC
- Defining Connection (FireDAC)
- Transactions
- Managing Transactions (FireDAC)
- Specifying the Transaction Isolation Level