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

database threadload解耦 #27

Closed
jiaojing opened this issue Jul 11, 2019 · 4 comments
Closed

database threadload解耦 #27

jiaojing opened this issue Jul 11, 2019 · 4 comments

Comments

@jiaojing
Copy link

@jiaojing jiaojing commented Jul 11, 2019

image

Employees.findOne 最终会变成一个Query,Query使用DataBase.global ,以及threadLocal来切换。多数据源的场景下,需要在一个context下执行 ,比如示例中的mysql {Employees.findOne}.
建议在Table定义的时候指定DataBase。 另外,在一些协程场景下,比如ktor框架内,用ThreadLocal,可能会产生一些隐含的bug。

@vincentlauvlwj

This comment has been minimized.

Copy link
Owner

@vincentlauvlwj vincentlauvlwj commented Jul 11, 2019

谢谢你的建议,我确实有考虑过不使用 ThreadLocal

在一些协程场景下,比如ktor框架内,用ThreadLocal,可能会产生一些隐含的bug。

但是,在协程场景下可能产生 bug 这个可以举具体一个例子吗。因为现在 Ktorm 是不支持协程的,Database.invoke 这个函数所接收的闭包并不是 suspend 的,所以你也无法在闭包里面调用 suspend 函数。

val mysql = Database.connect("jdbc:mysql://localhost:3306/ktorm", driver = "com.mysql.jdbc.Driver")

mysql {
    // 这里不能调用 suspend 函数,所以不会出现线程切换
}

比如这个例子,因为闭包中不能调用 suspend 函数,所以里面不会出现线程切换,因此使用 ThreadLocal 并没有问题。如果我的理解有误,麻烦指正。

@jiaojing

This comment has been minimized.

Copy link
Author

@jiaojing jiaojing commented Jul 11, 2019

kotlin 的corotine不熟悉,之前的判断是基于猜测。之后去看了一下,corotine有自己的执行环境隔离,suspend函数在那个点确实不能执行。

@vincentlauvlwj

This comment has been minimized.

Copy link
Owner

@vincentlauvlwj vincentlauvlwj commented Dec 12, 2019

Reopen this issue because there is a bug (#65) found caused by the Database.global design.

@vincentlauvlwj

This comment has been minimized.

Copy link
Owner

@vincentlauvlwj vincentlauvlwj commented Feb 2, 2020

Ktorm 2.7 删除了全局变量和 thread local,修复了这个问题,请参见 https://ktorm.liuwj.me/zh-cn/about-deprecating-database-global.html

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.