Skip to content

Commit 42f5008

Browse files
committed
Re-raising exceptions from failed operations
1 parent 488ab73 commit 42f5008

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

rdflib_sqlalchemy/store.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from rdflib.plugins.stores.regexmatching import PYTHON_REGEX, REGEXTerm
1515
from rdflib.store import CORRUPTED_STORE, VALID_STORE, NodePickler, Store
1616
from six import text_type
17-
from six.moves import reduce
1817
from sqlalchemy import MetaData
1918
from sqlalchemy.sql import expression, select
2019

@@ -305,14 +304,12 @@ def destroy(self, configuration):
305304
if self.engine is None:
306305
self.engine = self.open(configuration, create=False)
307306

308-
with self.engine.connect() as connection:
309-
trans = connection.begin()
307+
with self.engine.begin():
310308
try:
311309
self.metadata.drop_all(self.engine)
312-
trans.commit()
313310
except Exception:
314311
_logger.exception("unable to drop table.")
315-
trans.rollback()
312+
raise
316313

317314
# Triple Methods
318315

@@ -325,7 +322,7 @@ def add(self, triple, context=None, quoted=False):
325322
)
326323

327324
statement = self._add_ignore_on_conflict(statement)
328-
with self.engine.connect() as connection:
325+
with self.engine.begin() as connection:
329326
try:
330327
connection.execute(statement, params)
331328
except Exception:
@@ -349,16 +346,13 @@ def addN(self, quads):
349346
command_dict.setdefault("statement", statement)
350347
command_dict.setdefault("params", []).append(params)
351348

352-
with self.engine.connect() as connection:
353-
trans = connection.begin()
349+
with self.engine.begin() as connection:
354350
try:
355351
for command in commands_dict.values():
356352
statement = self._add_ignore_on_conflict(command['statement'])
357353
connection.execute(statement, command["params"])
358-
trans.commit()
359354
except Exception:
360355
_logger.exception("AddN failed.")
361-
trans.rollback()
362356
raise
363357

364358
def _add_ignore_on_conflict(self, statement):
@@ -385,8 +379,7 @@ def remove(self, triple, context):
385379
asserted_type_table = self.tables["type_statements"]
386380
literal_table = self.tables["literal_statements"]
387381

388-
with self.engine.connect() as connection:
389-
trans = connection.begin()
382+
with self.engine.begin() as connection:
390383
try:
391384
if not predicate or predicate != RDF.type:
392385
# Need to remove predicates other than rdf:type
@@ -413,11 +406,9 @@ def remove(self, triple, context):
413406

414407
clause = self.build_clause(quoted_table, subject, predicate, obj, context)
415408
connection.execute(quoted_table.delete(clause))
416-
417-
trans.commit()
418409
except Exception:
419410
_logger.exception("Removal failed.")
420-
trans.rollback()
411+
raise
421412

422413
def _triples_helper(self, triple, context=None):
423414
subject, predicate, obj = triple
@@ -716,6 +707,7 @@ def bind(self, prefix, namespace):
716707
connection.execute(binds_table.insert().values(prefix=prefix, uri=namespace))
717708
except Exception:
718709
_logger.exception("Namespace binding failed.")
710+
raise
719711

720712
def prefix(self, namespace):
721713
"""Prefix."""
@@ -817,17 +809,15 @@ def _remove_context(self, context):
817809
asserted_type_table = self.tables["type_statements"]
818810
literal_table = self.tables["literal_statements"]
819811

820-
with self.engine.connect() as connection:
821-
trans = connection.begin()
812+
with self.engine.begin() as connection:
822813
try:
823814
for table in [quoted_table, asserted_table,
824815
asserted_type_table, literal_table]:
825816
clause = self.build_context_clause(context, table)
826817
connection.execute(table.delete(clause))
827-
trans.commit()
828818
except Exception:
829819
_logger.exception("Context removal failed.")
830-
trans.rollback()
820+
raise
831821

832822
def _verify_store_exists(self):
833823
"""

0 commit comments

Comments
 (0)