Class: QgsTransaction

Allows creation of a multi-layer database-side transaction.

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.

Note

This is an abstract class, with methods which must be implemented by a subclass.

The following methods must be implemented: executeSql()

Class Hierarchy

Inheritance diagram of qgis.core.QgsTransaction

Base classes

QObject

class qgis.core.QgsTransaction[source]

Bases: QObject

addLayer(self, layer: QgsVectorLayer | None, addLayersInEditMode: bool = False) bool[source]

Add the layer to the transaction. The connection string must match.

Parameters:
  • layer (Optional[QgsVectorLayer]) – that will be added to the transaction

  • addLayersInEditMode (bool = False) – if set layers that are already

Added in version 3.26.

Return type:

bool

signal afterRollback[source]

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.

signal afterRollbackToSavepoint[source]

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.

begin(self, statementTimeout: int = 20)[source]

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) -> (bool)

commit(self)[source]

Commit transaction.

connectionString(self) str[source]

Returns the connection string of the transaction

Added in version 3.26.

Return type:

str

static create(connString: str | None, providerKey: str | None) QgsTransaction | None[source]

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

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

Parameters:
  • connString (Optional[str])

  • providerKey (Optional[str])

Return type:

Optional[QgsTransaction]

createSavepoint(self)[source]

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

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

signal dirtied[source]

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.

dirtyLastSavePoint(self)[source]

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

abstract executeSql(self, sql: str | None, isDirty: bool = False, name: str | None = '')[source]

Execute the sql string.

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

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

  • name (Optional[str] = '') -> (bool) – Name of the transaction ( only used if isDirty is True)

Returns:

  • True if everything is OK, False otherwise

  • error: The error message

lastSavePointIsDirty(self) bool[source]

returns the last created savepoint

Return type:

bool

rollback(self)[source]

Roll back transaction.

virtual rollbackToSavepoint(self, name: str | None)[source]

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

Parameters:

name (Optional[str]) -> (bool)

savePoints(self) List[str]

returns savepoints

Return type:

List[str]

static supportsTransaction(layer: QgsVectorLayer | None) bool[source]

Checks if the provider of a given layer supports transactions.

Parameters:

layer (Optional[QgsVectorLayer])

Return type:

bool