Does PostgreSQL supports "SELECT... FOR UPDATE OF..." SQL?
I am currently using "PostgreSQL 9.0.4" database with JDBC driver: "postgresql-9.0-801.jdbc4.jar".
I have the following JDBC SQL using "SELECT... FOR UPDATE OF..." clause which retrieves a column value (long) and updates the value by incrementing in same transaction.
I need to return the long value, thus I need to use SELECT Sql.final PreparedStatement preparedStatement = _connection.prepareState开发者_开发知识库ment(
"select value from sequence where table_name='JOB' for update of value",
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
long ret;
try {
final ResultSet resultSet = preparedStatement.executeQuery();
resultSet.next();
ret = resultSet.getLong(1);
resultSet.updateLong(1, ret + 1);
resultSet.updateRow();
} catch (SQLException ex) {
ex.printStackTrace();
}
finally {
resultSet.close();
preparedStatement.close();
}
return ret;
All other databases e.g. MySQL, Oracle, MS-SQL, Derby work fine with such SQL - but PostgreSQL always throws following exception.
org.postgresql.util.PSQLException: ERROR: relation "value" in FOR UPDATE/SHARE clause not found in FROM clause
Position: 68
I do have the table "sequence" with the "value" column properly created - so this may not be the infamous case-sensitivity issue.
Does this mean Postgres does not support "SELECT ...FOR UPDATE OF.." SQL syntax?
In that case what will be the best way to achieve the same effect (i.e. select and update with the same transaction)?Many thanks in advance,
Yes. It is supported. BUT the syntax is
FOR UPDATE [ OF table_name [, ...] ] [ NOWAIT ]
You can see more here:
http://www.postgresql.org/docs/8.2/static/sql-select.html#SQL-FOR-UPDATE-SHARE
精彩评论