Re: How to use jooq in multi-threading? Should I create a DSLContext for each thread?

507 views
Skip to first unread message
Message has been deleted

Marshall Pierce

unread,
Jul 30, 2021, 4:07:56 PM7/30/21
to jooq...@googlegroups.com
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
Message has been deleted

Lukas Eder

unread,
Aug 2, 2021, 4:21:30 PM8/2/21
to jOOQ User Group

On Fri, Jul 30, 2021 at 10:30 AM 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!

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/83e1056f-0b3f-441d-adf6-afc711e20701n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages