@@ -103,13 +103,12 @@ class Driver(object):
103103 def __init__ (self , url , ** config ):
104104 self .url = url
105105 parsed = urlparse (self .url )
106- transports = ['bolt' ]
107- if parsed .scheme in transports :
106+ if parsed .scheme == "bolt" :
108107 self .host = parsed .hostname
109108 self .port = parsed .port
110109 else :
111- raise ProtocolError ("Unsupported transport : '%s' in url: '%s'. Supported transports are: '%s '." %
112- (parsed .scheme , url , transports ))
110+ raise ProtocolError ("Unsupported URI scheme : '%s' in url: '%s'. Currently only supported 'bolt '." %
111+ (parsed .scheme , url ))
113112 self .config = config
114113 self .max_pool_size = config .get ("max_pool_size" , DEFAULT_MAX_POOL_SIZE )
115114 self .session_pool = deque ()
@@ -241,7 +240,7 @@ def keys(self):
241240 # Fetch messages until we have the header or a failure
242241 while self ._keys is None and not self ._consumed :
243242 self .connection .fetch ()
244- return self ._keys
243+ return tuple ( self ._keys )
245244
246245 def buffer (self ):
247246 if self .connection and not self .connection .closed :
@@ -264,9 +263,9 @@ def single(self):
264263 records = list (self )
265264 num_records = len (records )
266265 if num_records == 0 :
267- raise ResultError ("No records found in stream " )
266+ raise ResultError ("Cannot retrieve a single record, because this result is empty. " )
268267 elif num_records != 1 :
269- raise ResultError ("Multiple records found in stream " )
268+ raise ResultError ("Expected a result with a single record, but this result contains at least one more. " )
270269 else :
271270 return records [0 ]
272271
@@ -486,9 +485,11 @@ def run(self, statement, parameters=None):
486485 :rtype: :class:`.StatementResult`
487486 """
488487 if self .transaction :
489- raise ProtocolError ("Please close the currently open transaction object before running more "
490- "statements/transactions in the current session." )
488+ raise ProtocolError ("Statements cannot be run directly on a session with an open transaction;"
489+ " either run from within the transaction or use a different session." )
490+ return self ._run (statement , parameters )
491491
492+ def _run (self , statement , parameters = None ):
492493 # Ensure the statement is a Unicode value
493494 if isinstance (statement , bytes ):
494495 statement = statement .decode ("UTF-8" )
@@ -521,6 +522,8 @@ def close(self):
521522 """
522523 if self .last_result :
523524 self .last_result .buffer ()
525+ if self .transaction :
526+ self .transaction .close ()
524527 self .driver .recycle (self )
525528
526529 def begin_transaction (self ):
@@ -529,8 +532,8 @@ def begin_transaction(self):
529532 :return: new :class:`.Transaction` instance.
530533 """
531534 if self .transaction :
532- raise ProtocolError ("Please close the currently open transaction object before running more "
533- "statements/transactions in the current session." )
535+ raise ProtocolError ("You cannot begin a transaction on a session with an open transaction; "
536+ " either run from within the transaction or use a different session." )
534537 self .transaction = Transaction (self )
535538 return self .transaction
536539
@@ -558,7 +561,7 @@ class Transaction(object):
558561
559562 def __init__ (self , session ):
560563 self .session = session
561- self .session .run ("BEGIN" )
564+ self .session ._run ("BEGIN" )
562565
563566 def __enter__ (self ):
564567 return self
@@ -576,7 +579,7 @@ def run(self, statement, parameters=None):
576579 :return:
577580 """
578581 assert not self .closed
579- return self .session .run (statement , parameters )
582+ return self .session ._run (statement , parameters )
580583
581584 def commit (self ):
582585 """ Mark this transaction as successful and close in order to
@@ -597,9 +600,9 @@ def close(self):
597600 """
598601 assert not self .closed
599602 if self .success :
600- self .session .run ("COMMIT" )
603+ self .session ._run ("COMMIT" )
601604 else :
602- self .session .run ("ROLLBACK" )
605+ self .session ._run ("ROLLBACK" )
603606 self .closed = True
604607 self .session .transaction = None
605608
0 commit comments