Skip to content

Commit 4ee4fc7

Browse files
author
James Leigh
authored
Merge pull request #856 from jamesrdf/issues/#25-atomic-update
Fix #25: Execute each sequence of update operations in a single transaction
2 parents db177ce + e4738a9 commit 4ee4fc7

1 file changed

Lines changed: 38 additions & 28 deletions

File tree

  • core/repository/sail/src/main/java/org/eclipse/rdf4j/repository/sail

core/repository/sail/src/main/java/org/eclipse/rdf4j/repository/sail/SailUpdate.java

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
/**
2626
* @author Jeen Broekstra
27+
* @author James Leigh
2728
*/
2829
public class SailUpdate extends AbstractParserUpdate {
2930

@@ -51,42 +52,44 @@ public void execute()
5152
SailUpdateExecutor executor = new SailUpdateExecutor(con.getSailConnection(), con.getValueFactory(),
5253
con.getParserConfig());
5354

54-
for (UpdateExpr updateExpr : updateExprs) {
55+
boolean localTransaction = false;
56+
try {
57+
if (!getConnection().isActive()) {
58+
localTransaction = true;
59+
beginLocalTransaction();
60+
}
61+
for (UpdateExpr updateExpr : updateExprs) {
5562

56-
Dataset activeDataset = getMergedDataset(datasetMapping.get(updateExpr));
63+
Dataset activeDataset = getMergedDataset(datasetMapping.get(updateExpr));
5764

58-
try {
59-
boolean localTransaction = isLocalTransaction();
60-
if (localTransaction) {
61-
beginLocalTransaction();
65+
try {
66+
executor.executeUpdate(updateExpr, activeDataset, getBindings(), getIncludeInferred(),
67+
getMaxExecutionTime());
6268
}
63-
64-
executor.executeUpdate(updateExpr, activeDataset, getBindings(), getIncludeInferred(),
65-
getMaxExecutionTime());
66-
67-
if (localTransaction) {
68-
commitLocalTransaction();
69+
catch (RDF4JException e) {
70+
logger.warn("exception during update execution: ", e);
71+
if (!updateExpr.isSilent()) {
72+
throw new UpdateExecutionException(e);
73+
}
6974
}
70-
}
71-
catch (RDF4JException e) {
72-
logger.warn("exception during update execution: ", e);
73-
if (!updateExpr.isSilent()) {
74-
throw new UpdateExecutionException(e);
75+
catch (IOException e) {
76+
logger.warn("exception during update execution: ", e);
77+
if (!updateExpr.isSilent()) {
78+
throw new UpdateExecutionException(e);
79+
}
7580
}
7681
}
77-
catch (IOException e) {
78-
logger.warn("exception during update execution: ", e);
79-
if (!updateExpr.isSilent()) {
80-
throw new UpdateExecutionException(e);
81-
}
82+
83+
if (localTransaction) {
84+
commitLocalTransaction();
85+
localTransaction = false;
86+
}
87+
}
88+
finally {
89+
if (localTransaction) {
90+
rollbackLocalTransaction();
8291
}
8392
}
84-
}
85-
86-
private boolean isLocalTransaction()
87-
throws RepositoryException
88-
{
89-
return !getConnection().isActive();
9093
}
9194

9295
private void beginLocalTransaction()
@@ -101,4 +104,11 @@ private void commitLocalTransaction()
101104
getConnection().commit();
102105

103106
}
107+
108+
private void rollbackLocalTransaction()
109+
throws RepositoryException
110+
{
111+
getConnection().rollback();
112+
113+
}
104114
}

0 commit comments

Comments
 (0)