开发者

statement.getGeneratedKeys() and MySQL

开发者 https://www.devze.com 2023-01-17 22:23 出处:网络
I开发者_Python百科\'ve learned it the hard way that last_insert_id in mysql is not pool-safe. i.e. if you are pooling connections, you\'ll get messed up insert_ids.

I开发者_Python百科've learned it the hard way that last_insert_id in mysql is not pool-safe. i.e. if you are pooling connections, you'll get messed up insert_ids. How does java's statement.getGeneratedKeys() get the key on inserts? Is it pool-safe?


I am quoting the relevant text from the MySQL Connector/J internals here:

You should be aware, 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消