Skip to content

Commit bd02ae7

Browse files
committed
Session docs
1 parent 675a9e9 commit bd02ae7

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

neo4j/v1/api.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,27 @@ def close(self):
136136

137137

138138
class Session(object):
139-
""" Logical session carried out over an established TCP connection.
140-
Sessions should generally be constructed using the :meth:`.Driver.session`
141-
method.
139+
""" A `Session` is a logical context for transactional units of work.
140+
It typically wraps a TCP connection and should generally be constructed
141+
using the :meth:`.Driver.session` method.
142+
143+
Sessions are not thread safe and can recycle connections via a
144+
:class:`.Driver` connection pool. As such, they should be considered
145+
lightweight and disposable.
146+
147+
Typically, Session instances will be created and destroyed within a
148+
`with` context. For example::
149+
150+
with driver.session() as session:
151+
result = session.run("MATCH (a:Person) RETURN a.name")
152+
# do something with the result...
153+
142154
"""
143155

156+
#: The current :class:`.Transaction` instance, if any.
144157
transaction = None
145158

159+
#: The bookmark received from completion of the last :class:`.Transaction`.
146160
last_bookmark = None
147161

148162
def __del__(self):
@@ -155,7 +169,8 @@ def __exit__(self, exc_type, exc_value, traceback):
155169
self.close()
156170

157171
def close(self):
158-
""" Close the session.
172+
""" Close the session. This will release any borrowed resources,
173+
such as connections, and will roll back any outstanding transactions.
159174
"""
160175
if self.transaction:
161176
try:
@@ -164,14 +179,16 @@ def close(self):
164179
pass
165180

166181
def closed(self):
167-
""" Return true if the session is closed, false otherwise.
182+
""" Indicator for whether or not this session has been closed.
183+
184+
:return: :const:`True` if closed, :const:`False` otherwise.
168185
"""
169186

170187
def run(self, statement, parameters=None, **kwparameters):
171188
""" Run a parameterised Cypher statement. If an explicit transaction
172189
has been created, the statement will be executed within that
173190
transactional context. Otherwise, this will take place within an
174-
auto-commit transaction.
191+
*auto-commit transaction*.
175192
176193
:param statement: Cypher statement to execute
177194
:param parameters: dictionary of parameters
@@ -180,14 +197,16 @@ def run(self, statement, parameters=None, **kwparameters):
180197
"""
181198

182199
def fetch(self):
183-
""" Fetch the next message if available and return
184-
the number of messages fetched.
200+
""" Fetch the next message if available.
201+
202+
:return: The number of messages fetched (zero or one)
185203
"""
186204
return 0
187205

188206
def sync(self):
189-
""" Full send and receive. Return the total number
190-
of records received.
207+
""" Carry out a full send and receive.
208+
209+
:return: Total number of records received
191210
"""
192211
return 0
193212

@@ -197,6 +216,7 @@ def begin_transaction(self, bookmark=None):
197216
:param bookmark: a bookmark to which the server should
198217
synchronise before beginning the transaction
199218
:return: new :class:`.Transaction` instance.
219+
:raise: :class:`.TransactionError` if a transaction is already open
200220
"""
201221
if self.transaction:
202222
raise TransactionError("Explicit transaction already open")
@@ -208,11 +228,20 @@ def clear_transaction():
208228
return self.transaction
209229

210230
def commit_transaction(self):
231+
""" Commit the current transaction.
232+
233+
:return: the bookmark returned from the server, if any
234+
:raise: :class:`.TransactionError` if no transaction is currently open
235+
"""
211236
if not self.transaction:
212237
raise TransactionError("No transaction to commit")
213238
self.transaction = None
214239

215240
def rollback_transaction(self):
241+
""" Rollback the current transaction.
242+
243+
:raise: :class:`.TransactionError` if no transaction is currently open
244+
"""
216245
if not self.transaction:
217246
raise TransactionError("No transaction to rollback")
218247
self.transaction = None

neo4j/v1/bolt.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ def session(self, access_mode=None):
100100

101101

102102
class BoltSession(Session):
103-
""" Logical session carried out over an established TCP connection.
104-
Sessions should generally be constructed using the :meth:`.Driver.session`
105-
method.
106-
"""
107103

108104
def __init__(self, connection, access_mode=None):
109105
self.connection = connection
@@ -130,16 +126,6 @@ def closed(self):
130126
return self.connection is None or self.connection.closed
131127

132128
def run(self, statement, parameters=None, **kwparameters):
133-
""" Run a parameterised Cypher statement. If an explicit transaction
134-
has been created, the statement will be executed within that
135-
transactional context. Otherwise, this will take place within an
136-
auto-commit transaction.
137-
138-
:param statement: Cypher statement to execute
139-
:param parameters: dictionary of parameters
140-
:return: Cypher result
141-
:rtype: :class:`.StatementResult`
142-
"""
143129
if not self.connection:
144130
raise SessionError("This session is closed.")
145131

@@ -161,9 +147,6 @@ def run(self, statement, parameters=None, **kwparameters):
161147
return result
162148

163149
def fetch(self):
164-
""" Fetch the next message if available and return
165-
the number of messages fetched (one or zero).
166-
"""
167150
try:
168151
return self.connection.fetch()
169152
except ServiceUnavailable as cause:
@@ -194,6 +177,7 @@ def commit_transaction(self):
194177
self.sync()
195178
summary = result.summary()
196179
self.last_bookmark = summary.metadata.get("bookmark")
180+
return self.last_bookmark
197181

198182
def rollback_transaction(self):
199183
super(BoltSession, self).rollback_transaction()

0 commit comments

Comments
 (0)