Skip to content

Commit

Permalink
Ensure the client pulls results from the coordinator on COMMIT and RO…
Browse files Browse the repository at this point in the history
…LLBACK (fix #45)

Rather than duplicating the code in `transaction.py`, `commit()` and `rollback()` use `prestodb.client.PrestoQuery` to execute the query. It ensures that the client pulls result until there is nothing more to pull.
  • Loading branch information
Greg Leclercq authored and ggreg committed Oct 31, 2017
1 parent e4a4080 commit e31194e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
4 changes: 2 additions & 2 deletions prestodb/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ def start_transaction(self):
return self._transaction

def commit(self):
if not self.isolation_level:
if self.transaction is None:
return
self._transaction.commit()
self._transaction = None

def rollback(self):
if not self.isolation_level:
if self.transaction is None:
raise RuntimeError('no transaction was started')
self._transaction.rollback()
self._transaction = None
Expand Down
29 changes: 11 additions & 18 deletions prestodb/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from __future__ import print_function

from prestodb import constants
import prestodb.client
import prestodb.exceptions
import prestodb.logging

Expand Down Expand Up @@ -90,26 +91,18 @@ def begin(self):

def commit(self):
self._request.transaction_id = self._id
response = self._request.post(COMMIT)
if not response.ok:
query = prestodb.client.PrestoQuery(self._request, COMMIT)
try:
list(query.execute())
except Exception as err:
raise prestodb.exceptions.DatabaseErrror(
'failed to commit transaction {}: {}'.format(
self._id,
response.status_code,
)
)
status = self._request.process(response)
if not status.next_uri:
return
response = self._request.get(status.next_uri)
'failed to commit transaction {}: {}'.format(self._id, err))

def rollback(self):
self._request.transaction_id = self._id
response = self._request.post(ROLLBACK)
if not response.ok:
query = prestodb.client.PrestoQuery(self._request, ROLLBACK)
try:
list(query.execute())
except Exception as err:
raise prestodb.exceptions.DatabaseErrror(
'failed to rollback transaction {}: {}'.format(
self._id,
response.status_code,
)
)
'failed to rollback transaction {}: {}'.format(self._id, err))

0 comments on commit e31194e

Please sign in to comment.