Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
Fix for TaskLock when two processes request the task lock at the same…
Browse files Browse the repository at this point in the history
… second.
  • Loading branch information
mitchellsundt committed Jun 4, 2012
1 parent 2f79ea5 commit 96fa189
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,17 @@ private TaskLockTable doTransaction(TaskLockTable entity, long l)
boolean first;

final List<String> stmts = new ArrayList<String>();


String uri = entity.getUri();

StringBuilder b = new StringBuilder();
String tableName = K_BQ + datastore.getDefaultSchemaName() + K_BQ + "." + K_BQ
+ TaskLockTable.TABLE_NAME + K_BQ;

b.append("'").append(StringEscapeUtils.escapeSql(user.getUriUser())).append("'");
String uriUserInline = b.toString();
b.setLength(0);
b.append("'").append(StringEscapeUtils.escapeSql(entity.getUri())).append("'");
b.append("'").append(StringEscapeUtils.escapeSql(uri)).append("'");
String uriLockInline = b.toString();
b.setLength(0);
b.append("'").append(StringEscapeUtils.escapeSql(entity.getFormId())).append("'");
Expand Down Expand Up @@ -187,8 +189,8 @@ private TaskLockTable doTransaction(TaskLockTable entity, long l)
// task type
dam.recordDeleteUsage(TaskLockTable.TABLE_NAME);
b.append("DELETE FROM ").append(tableName).append(" WHERE ");
b.append(K_BQ).append(entity.formId.getName()).append(K_BQ).append(" = ").append(formIdInline)
.append(" AND ");
b.append(K_BQ).append(entity.formId.getName()).append(K_BQ).append(" = ")
.append(formIdInline).append(" AND ");
b.append(K_BQ).append(entity.taskType.getName()).append(K_BQ).append(" = ")
.append(taskTypeInline).append(" AND ");
b.append(K_BQ).append(entity.expirationDateTime.getName()).append(K_BQ);
Expand All @@ -201,6 +203,24 @@ private TaskLockTable doTransaction(TaskLockTable entity, long l)
.append(taskTypeInline).append(")");
stmts.add(b.toString());
b.setLength(0);
// delete our entry if it collides with another entry with exactly
// this time.
b.append("DELETE FROM ").append(tableName).append(" WHERE ");
b.append(K_BQ).append(entity.formId.getName()).append(K_BQ).append(" = ")
.append(formIdInline).append(" AND ");
b.append(K_BQ).append(entity.taskType.getName()).append(K_BQ).append(" = ")
.append(taskTypeInline).append(" AND ");
b.append(K_BQ).append(entity.primaryKey.getName()).append(K_BQ).append(" = ")
.append(uriLockInline).append(" AND ");
b.append("1 < (SELECT COUNT(t3.").append(K_BQ).append(entity.expirationDateTime.getName())
.append(K_BQ);
b.append(") FROM ").append(tableName).append(" AS t3 WHERE t3.");
b.append(K_BQ).append(entity.formId.getName()).append(K_BQ).append(" = ").append(formIdInline)
.append(" AND t3.");
b.append(K_BQ).append(entity.taskType.getName()).append(K_BQ).append(" = ")
.append(taskTypeInline).append(")");
stmts.add(b.toString());
b.setLength(0);
// assert: only the lock that holds the resource for that task type appears
// in the task lock table
TaskLockTable relation;
Expand Down

0 comments on commit 96fa189

Please sign in to comment.