On Jul 29, 2021, at 8:02 PM, jiehao luo <
powers...@gmail.com> wrote:
>
> Hi everyone~
>
> Regarding multi-threading, I found that in this question, lukaseder answered that DSLContext should be singleton assuming the SPI it uses is thread safe.
>
> So, is the recommended approach to share one single DSLContext object across all threads? Does that mean that we are sharing one single database connection between threads as well?
>
> If yes, I think it is not the best practice, as said in this stackoverflow question, that we should use a connections pool, instead of sharing one connection between threads.
>
> So I am confused about how to use DSLContext in multi-threading.
>
> Currently my company just creates one new DSLContext object for each thread, which is not aligned with the suggestion made by lukaseder in the question mentioned above.
>
> Hope I can learn from more experience devs.
>
> Thanks in advance!
Use a thread safe connection pool to create a single DSLContext that is then shared. I’ve had no issues using HikariCP, which you can see in use in a small demo project [1] I maintain just to answer these sorts of questions. :)
-Marshall
[1]
https://bitbucket.org/marshallpierce/ktor-demo/src/268d496b30d3e8edb44371e8d8c4fcee0f80e25a/src/main/kotlin/org/mpierce/ktordemo/cli/RunServer.kt#lines-123