distributed-lock icon indicating copy to clipboard operation
distributed-lock copied to clipboard

JDBC lock not working with PSQL due to SQL syntax

Open VincentXiao-jpg opened this issue 1 year ago • 2 comments

Hi, the recent changes to the SQL query for MySql is causing PSQL syntax errors.

  public static final String ACQUIRE_FORMATTED_QUERY = "INSERT INTO `%s` (lock_key, token, expireAt) VALUES (?, ?, ?);";
  public static final String RELEASE_FORMATTED_QUERY = "DELETE FROM `%s` WHERE lock_key = ? AND token = ?;";
  public static final String DELETE_EXPIRED_FORMATTED_QUERY = "DELETE FROM `%s` WHERE expireAt < ?;";
  public static final String REFRESH_FORMATTED_QUERY = "UPDATE `%s` SET expireAt = ? WHERE lock_key = ? AND token = ?;";

It seems to be that the inclusion of " ` " is breaking PSQL syntax.

org.postgresql.util.PSQLException: ERROR: syntax error at or near "`"

Not too sure on a fix, but it looks like MySql and PSQL syntax are conflicting, would appreciate if you could look into this. Thanks.

VincentXiao-jpg avatar Sep 12 '22 02:09 VincentXiao-jpg

same issue with sqlserver:

org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [DELETE FROM `locked` WHERE expireAt < ?;]; SQL state [S0001]; error code [102]; Incorrect syntax near '`'.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '`'.
	at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1542)
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:667)
	at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:960)
	at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:1015)
	at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:1025)
	at com.github.alturkovic.lock.jdbc.service.SimpleJdbcLockSingleKeyService.acquire(SimpleJdbcLockSingleKeyService.java:51)

ssiahetiong avatar Sep 26 '22 04:09 ssiahetiong

I currently don't have the time to tackle this, if any of you have time to solve this, I would be happy to merge a PR.

alturkovic avatar Sep 27 '22 13:09 alturkovic

Hi Guys,

I tried to create a pull request, but I've got some errors. Maybe it is because of firewall issues. Please find bellow the queries tested with oracle:

public static final String ACQUIRE_FORMATTED_QUERY = "INSERT INTO %s (lock_key, token, expireAt) VALUES (?, ?, ?)"; public static final String RELEASE_FORMATTED_QUERY = "DELETE FROM %s WHERE lock_key = ? AND token = ?"; public static final String DELETE_EXPIRED_FORMATTED_QUERY = "DELETE FROM %s WHERE expireAt < ?"; public static final String REFRESH_FORMATTED_QUERY = "UPDATE %s SET expireAt = ? WHERE lock_key = ? AND token = ?";

Best Regards, Ciprian

cipianpascu avatar Nov 08 '22 08:11 cipianpascu

@cipianpascu that was the first version of the queries. The issue is that INSERT INTO %s gets resolved into INSERT INTO lock and lock is a keyword in MySQL so it has to be escaped. The current issue is that different databases escape keywords differently. So ` (backtick escape) is breaking PSQL syntax.

alturkovic avatar Nov 08 '22 09:11 alturkovic

Oh, I didn't know. But that is a small/corner case. No one will use the name 'lock' in production for a table. I propose to change the default to something else. For example 'tbl_lock' or 'table_lock' or anything else that is not interfering with the sql sintax.

cipianpascu avatar Nov 08 '22 11:11 cipianpascu