Class: QgsTransaction

class qgis.core.QgsTransaction

Bases: PyQt5.QtCore.QObject

This class allows including a set of layers in a database-side transaction, provided the layer data providers support transactions and are compatible with each other.

Only layers which are not in edit mode can be included in a transaction, and all layers need to be in read-only mode for a transaction to be committed or rolled back.

Layers can only be included in one transaction at a time.

When editing layers which are part of a transaction group, all changes are sent directly to the data provider (bypassing the undo/redo stack), and the changes can either be committed or rolled back on the database side via the QgsTransaction.commit and QgsTransaction.rollback methods.

As long as the transaction is active, the state of all layer features reflects the current state in the transaction.

Edits on features can get rejected if another conflicting transaction is active.

Methods

addLayer

Add the layer to the transaction.

begin

Begin transaction The statementTimeout (in seconds) specifies how long an sql statement is allowed to block QGIS before it is aborted.

commit

Commit transaction.

create

Create a transaction for the specified connection string connString and provider with providerKey.

createSavepoint

creates a save point returns empty string on error returns the last created savepoint if it's not dirty

dirtyLastSavePoint

dirty save point such that next call to createSavepoint will create a new one

executeSql

Execute the sql string.

lastSavePointIsDirty

returns the last created savepoint

rollback

Roll back transaction.

rollbackToSavepoint

rollback to save point, the save point is maintained and is "undertied"

savePoints

returns savepoints

supportsTransaction

Checks if the provider of a given layer supports transactions.

Signals

afterRollback

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

dirtied

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

addLayer(self, layer: QgsVectorLayer) bool

Add the layer to the transaction. The layer must not be in edit mode and the connection string must match.

Parameters:

layer (QgsVectorLayer) –

Return type:

bool

afterRollback

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

begin(self, statementTimeout: int = 20) Tuple[bool, str]

Begin transaction The statementTimeout (in seconds) specifies how long an sql statement is allowed to block QGIS before it is aborted. Statements can block, if multiple transactions are active and a statement would produce a conflicting state. In these cases, the statements block until the conflicting transaction is committed or rolled back. Some providers might not honour the statement timeout.

Parameters:

statementTimeout (int = 20) –

Return type:

Tuple[bool, str]

commit(self) Tuple[bool, str]

Commit transaction.

Return type:

Tuple[bool, str]

create(connString: str, providerKey: str) QgsTransaction

Create a transaction for the specified connection string connString and provider with providerKey.

create(layers: Iterable[QgsVectorLayer]) -> QgsTransaction Create a transaction which includes the layers. All layers are expected to have the same connection string and data provider.

Parameters:
  • connString (str) –

  • providerKey (str) –

Return type:

QgsTransaction

createSavepoint(self) Tuple[str, str]

creates a save point returns empty string on error returns the last created savepoint if it’s not dirty

New in version 3.0.

createSavepoint(self, savePointId: str) -> Tuple[str, str] creates a save point returns empty string on error

New in version 3.0.

Return type:

Tuple[str, str]

dirtied

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

dirtyLastSavePoint(self)

dirty save point such that next call to createSavepoint will create a new one

New in version 3.0.

executeSql(self, sql: str, isDirty: bool = False, name: str = '') Tuple[bool, str]

Execute the sql string.

Parameters:
  • sql (str) – The sql query to execute

  • isDirty (bool = False) – Flag to indicate if the underlying data will be modified

  • name (str = '') – Name of the transaction ( only used if isDirty is True)

Return type:

Tuple[bool, str]

Returns:

  • True if everything is OK, False otherwise

  • error: The error message

lastSavePointIsDirty(self) bool

returns the last created savepoint

New in version 3.0.

Return type:

bool

rollback(self) Tuple[bool, str]

Roll back transaction.

Return type:

Tuple[bool, str]

rollbackToSavepoint(self, name: str) Tuple[bool, str]

rollback to save point, the save point is maintained and is “undertied”

New in version 3.0.

Parameters:

name (str) –

Return type:

Tuple[bool, str]

savePoints(self) List[str]

returns savepoints

New in version 3.0.

Return type:

List[str]

supportsTransaction(layer: QgsVectorLayer) bool

Checks if the provider of a given layer supports transactions.

Parameters:

layer (QgsVectorLayer) –

Return type:

bool