Skip to content

Commit

Permalink
feat: fix lock-and-fetch for MySQL (kagkarlsson#537)
Browse files Browse the repository at this point in the history
## Brief, plain english overview of your changes here
The support was nearly finished in kagkarlsson#371 but later commented out due to
"some issue [..] and limited time".

I tried to find out what those issues were and discovered that the
syntax of the select was wrong. In MySQL the `LIMIT` comes before the
`FOR UPDATE SKIP LOCKED`.

Using that fix `Mysql8CompatibilityTest` passes, I have done no further
testing yet.

## Fixes
kagkarlsson#264


## Reminders
- [x] Added/ran automated tests
- [ ] Update README and/or examples (not applicable?)
- [x] Ran `mvn spotless:apply`
  • Loading branch information
felixscheinost authored Nov 20, 2024
1 parent 2704171 commit ddfa556
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public String createGenericSelectForUpdateQuery(
Queries.postgresSqlLimitPart(limit),
requiredAndCondition,
" FOR UPDATE SKIP LOCKED ",
null,
null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public String createGenericSelectForUpdateQuery(
Queries.ansiSqlLimitPart(limit),
requiredAndCondition,
null,
" WITH (READPAST,ROWLOCK) ");
" WITH (READPAST,ROWLOCK) ",
null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public String getQueryLimitPart(int limit) {

@Override
public boolean supportsGenericLockAndFetch() {
// FIXLATER: fix syntax and enable
return false;
return true;
}

@Override
Expand All @@ -49,7 +48,8 @@ public String createGenericSelectForUpdateQuery(
Queries.ansiSqlOrderPart(orderByPriority),
Queries.postgresSqlLimitPart(limit),
requiredAndCondition,
" FOR UPDATE SKIP LOCKED ",
null);
null,
null,
" FOR UPDATE SKIP LOCKED ");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public String createGenericSelectForUpdateQuery(
Queries.ansiSqlLimitPart(limit),
requiredAndCondition,
" FOR UPDATE SKIP LOCKED ",
null,
null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public String createGenericSelectForUpdateQuery(
getQueryLimitPart(limit),
requiredAndCondition,
" FOR UPDATE SKIP LOCKED ",
null,
null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ public static String selectForUpdate(
String limitPart,
String requiredAndCondition,
String postgresOracleStyleForUpdate,
String sqlServerStyleForUpdate) {
String sqlServerStyleForUpdate,
String mysqlOracleStyleForUpdate) {
return "SELECT * FROM "
+ tableName
+ Optional.ofNullable(sqlServerStyleForUpdate).orElse("")
+ " WHERE picked = ? AND execution_time <= ? "
+ requiredAndCondition
+ orderPart
+ Optional.ofNullable(postgresOracleStyleForUpdate).orElse("")
+ limitPart;
+ limitPart
// Note: Compared to Postgres, MySQL expects `FOR UPDATE SKIP LOCKED` after `LIMIT`
+ Optional.ofNullable(mysqlOracleStyleForUpdate).orElse("");
}

public static String postgresSqlLimitPart(int limit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Mysql8CompatibilityTest extends CompatibilityTest {
private static HikariDataSource pooledDatasource;

public Mysql8CompatibilityTest() {
super(false, false); // FIXLATER: fix syntax and enable
super(true, false);
}

@BeforeAll
Expand Down

0 comments on commit ddfa556

Please sign in to comment.