POSTGRESQL: Dealing with 'current transaction is aborted, commands ignored until end of transaction block' in Lazarus/Delphi
Scenario:
Programming Environment: Lazarus/Delphi
Database: PostgreSQL
DB Component: Zeos
Problem:
When user inserting record which fails going through Table integriry constraints (foreign key, unique constraint, check constraints etc), postgresql will throw exception: current transaction is aborted, commands ignored until end of transaction block. After this, all operation is not possible except closing the form and losing all modification you might have done.
Solution:
Define events of the query component, FDBConnection is db the connection component.
BeforePost and BeforeDelete events:
BeforePost and BeforeDelete events:
Self.FDBConnection.ExecuteDirect( 'SAVEPOINT mysavepoint');
AfterPost and AfterDelete events:
Self.FDBConnection.ExecuteDirect( 'RELEASE SAVEPOINT mysavepoint');
OnPostError and OnDeleteError events:
Self.FDBConnection.ExecuteDirect( 'ROLLBACK TO SAVEPOINT mysavepoint');
Note: you can change mysavepoint with any valid identifier name.
Comments
Post a Comment