以下 YSQL 文档:doc最后一段提到:
...that at times, it can be tricky to use the SELECT LAST_INSERT_ID() query, as that function's value is scoped to a connection. So, if some other query happens on the same connection, the value will be overwritten. On the other hand, the getGeneratedKeys() method is scoped by the Statement instance, so it can be used even if other queries happen on the same connection, but not on the same Statement instance.
当我在完全分布式的环境中工作时,不要碰巧从数据库中获取错误的 key 对我来说非常重要......
springJDBC正在使用这些方法中的哪一个
我正在使用 spring 版本 3.2.1.RELEASE
但我不打算升级到最新版本。
最佳答案
嗯。按照建议,我仔细研究了代码,直到到达这一行。
ResultSet keys = ps.getGeneratedKeys();
从 PreparedStatementCallback
匿名类内部调用。
因此,出于所有意图和目的,该函数:
int org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder) 抛出 DataAccessException
这是一个线程/进程安全的操作
关于java - Spring JDBC使用什么方法获取最新的key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26083167/