Outside of the admin APIs, all work with actual table data in a database occurs in the context of a session.
To create a :class:`~google.cloud.spanner.session.Session` object:
session = database.session()After creating the session object, use its :meth:`~google.cloud.spanner.session.Session.create` method to trigger its creation on the server:
session.create()After creating the session object, use its :meth:`~google.cloud.spanner.session.Session.exists` method to determine whether the session still exists on the server:
assert session.exists()Once done with the session object, use its :meth:`~google.cloud.spanner.session.Session.delete` method to free up its resources on the server:
session.delete()Rather than calling the Session's :meth:`~google.cloud.spanner.session.Session.create` and :meth:`~google.cloud.spanner.session.Session.delete` methods directly, you can use the session as a Python context manager:
with database.session() as session:
assert session.exists()
# perform session operations hereNote
At the beginning of the with block, the session's
:meth:`~google.cloud.spanner.session.Session.create` method is called.
At the end of the with block, the session's
:meth:`~google.cloud.spanner.session.Session.delete` method is called.
Next, learn about :doc:`spanner-session-implicit-txn-usage`.