QldbSession Reference

class pyqldb.session.qldb_session.QldbSession(session, read_ahead, retry_limit, executor)[source]

A class representing a session to a QLDB ledger for interacting with QLDB. A QldbSession is linked to the specified ledger in the parent driver of the instance of the QldbSession. In any given QldbSession, only one transaction can be active at a time. This object can have only one underlying session to QLDB, and therefore the lifespan of a QldbSession is tied to the underlying session, which is not indefinite, and on expiry this QldbSession will become invalid, and a new QldbSession needs to be created from the parent driver in order to continue usage.

When a QldbSession is no longer needed, pyqldb.session.qldb_session.QldbSession.close() should be invoked in order to clean up any resources.

See pyqldb.driver.pooled_qldb_driver.PooledQldbDriver for an example of session lifecycle management, allowing the re-use of sessions when possible. There should only be one thread interacting with a session at any given time.

There are three methods of execution, ranging from simple to complex; the first two are recommended for inbuilt error handling:

Parameters
  • session (pyqldb.communication.session_client.SessionClient) – The session object representing a communication channel with QLDB.

  • read_ahead (int) – The number of pages to read-ahead and buffer when retrieving results.

  • retry_limit (int) – The limit for retries on execute methods when an OCC conflict or retriable exception occurs.

  • executor (concurrent.futures.thread.ThreadPoolExecutor) – The executor to be used by the retrieval thread.

close()[source]

Close this session. No-op if already closed.

execute_lambda(query_lambda, retry_indicator=<function QldbSession.<lambda>>)[source]

Implicitly start a transaction, execute the lambda function, and commit the transaction, retrying up to the retry limit if an OCC conflict or retriable exception occurs.

If an InvalidSessionException is received, it is considered a retriable exception by starting a new pyqldb.communication.session_client.SessionClient to use to communicate with QLDB. Thus, as a side effect, this QldbSession can become valid again despite a previous InvalidSessionException from other method calls on this instance, any child transactions, or cursors, when this method is invoked.

Parameters
  • query_lambda (function) – The lambda function to execute. A lambda function cannot have any side effects as it may be invoked multiple times, and the result cannot be trusted until the transaction is committed.

  • retry_indicator (function) – Optional function called when the transaction execution is about to be retried due to an OCC conflict or retriable exception.

Return type

pyqldb.cursor.buffered_cursor.BufferedCursor/object

Returns

The return value of the lambda function which could be a pyqldb.cursor.buffered_cursor.BufferedCursor on the result set of a statement within the lambda.

Raises
execute_statement(statement, *parameters, retry_indicator=<function QldbSession.<lambda>>)[source]

Implicitly start a transaction, execute the statement, and commit the transaction, retrying up to the retry limit if an OCC conflict or retriable exception occurs.

If an InvalidSessionException is received, it is considered a retriable exception by starting a new pyqldb.communication.session_client.SessionClient to use to communicate with QLDB. Thus, as a side effect, this QldbSession can become valid again despite a previous InvalidSessionException from other method calls on this instance, any child transactions, or cursors, when this method is invoked.

Parameters
  • statement (str) – The statement to execute.

  • parameters (Variable length argument list) –

    Ion values or Python native types that are convertible to Ion for filling in parameters of the statement.

    Details on conversion support and rules.

  • retry_indicator (function) – Optional function called when the transaction execution is about to be retried due to an OCC conflict or retriable exception.

Return type

pyqldb.cursor.buffered_cursor.BufferedCursor

Returns

Fully buffered Cursor on the result set of the statement.

Raises
  • IllegalStateError – When the commit digest from commit transaction result does not match.

  • SessionClosedError – When this session is closed.

  • ClientError – When there is an error executing against QLDB.

  • TypeError – When conversion of native data type (in parameters) to Ion fails due to an unsupported type.

property ledger_name

The read-only ledger name.

list_tables()[source]

Get the list of table names in the ledger.

Return type

pyqldb.cursor.buffered_cursor.BufferedCursor

Returns

Iterable of table names in amazon.ion.simple_types.IonPyText format found in the ledger.

Raises

SessionClosedError – When this session is closed.

property session_id

The read-only session ID.

property session_token

The read-only session token.

start_transaction()[source]

Start a transaction using an available database session.

Return type

pyqldb.transaction.transaction.Transaction

Returns

A new transaction.

Raises

SessionClosedError – When this session is closed.

throw_if_closed()[source]

Check and throw if this session is closed.

Raises

SessionClosedError – When this session is closed.