コード例 #1
0
def list_tables():
    """
    List all tables.

    :rtype: list
    :return: List of ledgers.
    """
    logger.info("Let's list all the tables...")
    with create_qldb_session() as session:
        logger.info("Success. List of tables:")
        tables = session.list_tables()
        for table in tables:
            logger.info(table)
    return tables
コード例 #2
0
    :type gov_id: str
    :param gov_id: The owner's government ID.
    """
    document_ids = get_document_ids(transaction_executor,
                                    Constants.PERSON_TABLE_NAME, 'GovId',
                                    gov_id)

    query = "SELECT Vehicle FROM Vehicle INNER JOIN VehicleRegistration AS r " \
            "ON Vehicle.VIN = r.VIN WHERE r.Owners.PrimaryOwner.PersonId = ?"

    for ids in document_ids:
        cursor = transaction_executor.execute_statement(query, ids)
        logger.info(
            'List of Vehicles for owner with GovId: {}...'.format(gov_id))
        print_result(cursor)


if __name__ == '__main__':
    """
    Find all vehicles registered under a person.
    """
    try:
        with create_qldb_session() as session:
            # Find all vehicles registered under a person.
            gov_id = SampleData.PERSON[0]['GovId']
            session.execute_lambda(
                lambda executor: find_vehicles_for_owner(executor, gov_id),
                lambda retry_attempt: logger.info(
                    'Retrying due to OCC conflict...'))
    except Exception:
        logger.exception('Error getting vehicles for owner.')
コード例 #3
0
                transaction = qldb_session.start_transaction()
                execute_transaction(qldb_session, transaction, statement, parameters)


if __name__ == '__main__':
    """
    Demonstrates how to handle OCC conflicts, where two users try to execute and commit changes to the same document.
    When OCC conflict occurs on execute or commit, implicitly handled by restarting the transaction.
    In this example, two sessions on the same ledger try to access the registration city for the same Vehicle Id.
    """
    vehicle_vin = SampleData.VEHICLE_REGISTRATION[0]['VIN']
    parameters = [convert_object_to_ion(vehicle_vin)]
    query1 = "UPDATE VehicleRegistration AS v SET v.City = 'Tukwila' WHERE v.VIN = ?"
    query2 = 'SELECT City FROM VehicleRegistration AS v WHERE v.VIN = ?'

    with create_qldb_session() as session1, create_qldb_session() as session2:
        logger.info('Updating the VehicleRegistration city in transaction 1...')
        transaction1 = session1.start_transaction()
        logger.info('Selecting the VehicleRegistration city in transaction 2...')
        transaction2 = session2.start_transaction()

        logger.info('Executing transaction 1')
        execute_transaction(session1, transaction1, query1, parameters)
        logger.info('Executing transaction 2')
        execute_transaction(session2, transaction2, query2, parameters)

        logger.info('Committing transaction 1...')
        commit_transaction(session1, transaction1, query1, parameters)

        # The first attempt to commit on transaction 2 will fail due to an OCC conflict.
        logger.info('Committing transaction 2...')