Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDBC lock not working with PSQL due to SQL syntax #26

Closed
VincentXiao-jpg opened this issue Sep 12, 2022 · 9 comments
Closed

JDBC lock not working with PSQL due to SQL syntax #26

VincentXiao-jpg opened this issue Sep 12, 2022 · 9 comments

Comments

@VincentXiao-jpg
Copy link

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.

@ssiahetiong
Copy link

ssiahetiong commented Sep 26, 2022

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)

@alturkovic
Copy link
Owner

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.

@cipianpascu
Copy link

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

@alturkovic
Copy link
Owner

alturkovic commented Nov 8, 2022

@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.

@cipianpascu
Copy link

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.

@alturkovic
Copy link
Owner

Should be fixed in 1.5.0, I decided to change the default table name as suggested to avoid further complications

@cipianpascu
Copy link

not yet. sadly I get ORA-00933: SQL command not properly ended in this moment

@alturkovic
Copy link
Owner

It seems like it might be due to the semicolon at the end? I released a new fix version, could you try with that one? (1.5.1)

@alturkovic alturkovic reopened this Feb 1, 2023
@cipianpascu
Copy link

Hey Alen,
All good now, thank you
Best Regards,
Ciprian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants