@@ -136,13 +136,27 @@ def close(self):
136
136
137
137
138
138
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
+
142
154
"""
143
155
156
+ #: The current :class:`.Transaction` instance, if any.
144
157
transaction = None
145
158
159
+ #: The bookmark received from completion of the last :class:`.Transaction`.
146
160
last_bookmark = None
147
161
148
162
def __del__ (self ):
@@ -155,7 +169,8 @@ def __exit__(self, exc_type, exc_value, traceback):
155
169
self .close ()
156
170
157
171
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.
159
174
"""
160
175
if self .transaction :
161
176
try :
@@ -164,14 +179,16 @@ def close(self):
164
179
pass
165
180
166
181
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.
168
185
"""
169
186
170
187
def run (self , statement , parameters = None , ** kwparameters ):
171
188
""" Run a parameterised Cypher statement. If an explicit transaction
172
189
has been created, the statement will be executed within that
173
190
transactional context. Otherwise, this will take place within an
174
- auto-commit transaction.
191
+ * auto-commit transaction* .
175
192
176
193
:param statement: Cypher statement to execute
177
194
:param parameters: dictionary of parameters
@@ -180,14 +197,16 @@ def run(self, statement, parameters=None, **kwparameters):
180
197
"""
181
198
182
199
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)
185
203
"""
186
204
return 0
187
205
188
206
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
191
210
"""
192
211
return 0
193
212
@@ -197,6 +216,7 @@ def begin_transaction(self, bookmark=None):
197
216
:param bookmark: a bookmark to which the server should
198
217
synchronise before beginning the transaction
199
218
:return: new :class:`.Transaction` instance.
219
+ :raise: :class:`.TransactionError` if a transaction is already open
200
220
"""
201
221
if self .transaction :
202
222
raise TransactionError ("Explicit transaction already open" )
@@ -208,11 +228,20 @@ def clear_transaction():
208
228
return self .transaction
209
229
210
230
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
+ """
211
236
if not self .transaction :
212
237
raise TransactionError ("No transaction to commit" )
213
238
self .transaction = None
214
239
215
240
def rollback_transaction (self ):
241
+ """ Rollback the current transaction.
242
+
243
+ :raise: :class:`.TransactionError` if no transaction is currently open
244
+ """
216
245
if not self .transaction :
217
246
raise TransactionError ("No transaction to rollback" )
218
247
self .transaction = None
0 commit comments