如何在Mysql中使用用户定义的@variable作为线程安全

时间:2016-11-28 05:43:33

标签: mysql spring hibernate thread-safety

使用spring框架中的相同连接对象访问mysql中的@variable时。它对所有用户都很常见,因此看起来不是在查询中使用的线程安全变量。如何在mysql中使用@variable替换为用户定义的变量。

在同一连接对象中具有线程安全性的下面一个的替代查询。

select
@num := (@num + 1) as row_number
from
user u,
(select @num := 0);

1 个答案:

答案 0 :(得分:1)

包括(select @num := 0)在查询开头初始化变量。用户定义的变量的范围限定为单个连接,连接一次只能运行一个查询,因此这种特定情况完全是线程安全的。"

然而,它也有点像黑客。

请参阅http://dev.mysql.com/doc/refman/5.7/en/user-variables.html