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

多数据源切换的一个建议. #47

Closed
JsonSong89 opened this issue Sep 20, 2019 · 9 comments
Closed

多数据源切换的一个建议. #47

JsonSong89 opened this issue Sep 20, 2019 · 9 comments
Labels

Comments

@JsonSong89
Copy link

@JsonSong89 JsonSong89 commented Sep 20, 2019

ktorm是支持多数据源的,但是感觉用起来有点不太方便.
比如我一直用的是单一数据源(primary)
但有一天我需要加一个数据源,访问一个配置数据库(second).
结果是我原来的所有primary的dao方法都受到影响了,
现在只要访问一次second,我得将primary invoke一次,不然primary对应的方法就会使second数据源.

有两个想法:
1.可以给Database设置生效的aspect,Database只对这个aspect下面的table objects生效
2.可以为table objects设置Database,优先使用自己的db,而不是全部使用Database.global

@vincentlauvlwj

This comment has been minimized.

Copy link
Owner

@vincentlauvlwj vincentlauvlwj commented Sep 20, 2019

谢谢,类似的建议 #41

@try520

This comment has been minimized.

Copy link

@try520 try520 commented Dec 27, 2019

为什么不能一个数据库是一个对象,对象下该数据库的表,然后是该表的access
类似 db1.tableName.findAll(),db2.tableName.findAll() 不是更清晰么

@vincentlauvlwj

This comment has been minimized.

Copy link
Owner

@vincentlauvlwj vincentlauvlwj commented Dec 27, 2019

@try520

谢谢,我正在做这个改进,现在已经完成了,等完善文档之后就会发布 2.7 版本,有兴趣的话可以先看看 rm-global-database-object 分支里面的测试用例,下面是重构之后的使用示例:

Ktorm Example

@JsonSong89

This comment has been minimized.

Copy link
Author

@JsonSong89 JsonSong89 commented Dec 27, 2019

@vincentlauvlwj
感谢作者的付出.

是完全移除global db吗?
但是感觉如果是单数据源,之前的方式还是很方便的.
如果按照2.7的话,又得每次都写一点样板代码了

@vincentlauvlwj

This comment has been minimized.

Copy link
Owner

@vincentlauvlwj vincentlauvlwj commented Dec 27, 2019

使用到 global db 的地方不会直接移除,会标记为 @Deprecated

至于样板代码,现在的改变只是把 Employees.select(..) 改成 db.from(Employees).select(..),把 Employees.asSequence() 改成 db.sequenceOf(Employees),我感觉并没有太大影响

@JsonSong89

This comment has been minimized.

Copy link
Author

@JsonSong89 JsonSong89 commented Dec 27, 2019

主要是db需要引入下,如果是用spring注入的,还得再从beanFactory拿出来.

@vincentlauvlwj

This comment has been minimized.

Copy link
Owner

@vincentlauvlwj vincentlauvlwj commented Dec 27, 2019

那也仅仅是一个 @Autowired 而已,对于使用 Spring 的人来说,这不是正常操作吗 😀

从这段时间的经验看来,在框架内部保存一个全局的数据库对象,除了 API 设计方便一点点之外,真的是弊大于利,多数据源切换不方便是一个,还有因为使用 ThreadLocal 给代码引入了一个隐式的变量,即不直观也容易造成误解

@JsonSong89

This comment has been minimized.

Copy link
Author

@JsonSong89 JsonSong89 commented Dec 27, 2019

嗯 不直观倒是真的,毕竟全局变量本身就容易出问题.也带来很多复杂度.
我之前是想到mybatis也是用的全局的,不过这里到没必要类比.
用db打头也没什么.

@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
3 participants
You can’t perform that action at this time.