How to properly use MyBatis in a multi-threaded environment and with DI?

20 views
Skip to first unread message

TheDeadOne

unread,
Dec 12, 2019, 9:37:13 AM12/12/19
to mybatis-user
Hello, I'm using MyBatis with Guice and Play Framework. Everything works fine. But I can't understand why. Aren't mappers strongly tied to sessions? What will happen when I try to use an injected mapper whose session was closed for any reason? How long can a session remain open? And the documentation says that the mappers are not thread-safe but in my project, they are injected in singletons running in an extremely concurrent environment. What kind of magic is this? Should I worry about these concerns?

Poitras Christian

unread,
Dec 12, 2019, 9:58:11 AM12/12/19
to mybati...@googlegroups.com
Hi,

Mappers work differently with MyBatis-Guice than they do with MyBatis alone.

In MyBatis, you would obtain a Mapper from the session and so the Mapper is tied to the session.

In MyBatis-Guice, you inject a Mapper that is tied to an SqlSessionManager that contains a ThreadLocal field that is tied to the current session, if any.
When the MyBatis-Guice intercepts a call to a method with @Transactional annotation, it will create a new session and bind it to the SqlSessionManager so you mappers end up working “magically”. If a mapper’s method is called outside of a session, a new session is created for the current method only. A commit will be done after the call unless an exception is caught in which case, a rollback is done.
So injected Mappers are thread safe with MyBatis-Guice.

Best,
Christian
--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com<mailto:mybatis-user...@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/8b487a91-b118-460d-b5e9-57fabf56fa43%40googlegroups.com<https://groups.google.com/d/msgid/mybatis-user/8b487a91-b118-460d-b5e9-57fabf56fa43%40googlegroups.com?utm_medium=email&utm_source=footer>.

TheDeadOne

unread,
Dec 12, 2019, 10:13:17 AM12/12/19
to mybatis-user
It's wonderful! Thank you very much.

четверг, 12 декабря 2019 г., 22:58:11 UTC+8 пользователь christia...@ircm.qc.ca написал:
Hi,

Mappers work differently with MyBatis-Guice than they do with MyBatis alone.

In MyBatis, you would obtain a Mapper from the session and so the Mapper is tied to the session.

In MyBatis-Guice, you inject a Mapper that is tied to an SqlSessionManager that contains a ThreadLocal field that is tied to the current session, if any.
When the MyBatis-Guice intercepts a call to a method with @Transactional annotation, it will create a new session and bind it to the SqlSessionManager so you mappers end up working “magically”. If a mapper’s method is called outside of a session, a new session is created for the current method only. A commit will be done after the call unless an exception is caught in which case, a rollback is done.
So injected Mappers are thread safe with MyBatis-Guice.

Best,
Christian


From: <mybati...@googlegroups.com> on behalf of TheDeadOne <Sputte...@gmail.com>
Reply-To: "mybati...@googlegroups.com" <mybati...@googlegroups.com>
Date: Thursday, December 12, 2019 at 9:37 AM
To: mybatis-user <mybati...@googlegroups.com>
Subject: How to properly use MyBatis in a multi-threaded environment and with DI?

Hello, I'm using MyBatis with Guice and Play Framework. Everything works fine. But I can't understand why. Aren't mappers strongly tied to sessions? What will happen when I try to use an injected mapper whose session was closed for any reason? How long can a session remain open? And the documentation says that the mappers are not thread-safe but in my project, they are injected in singletons running in an extremely concurrent environment. What kind of magic is this? Should I worry about these concerns?
--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybati...@googlegroups.com<mailto:mybatis-user+unsubscribe@googlegroups.com>.
Reply all
Reply to author
Forward
0 new messages