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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

TheDeadOne
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/8b487a91-b118-460d-b5e9-57fabf56fa43%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

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

Poitras Christian
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: <[hidden email]> on behalf of TheDeadOne <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Thursday, December 12, 2019 at 9:37 AM
To: mybatis-user <[hidden email]>
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 [hidden email]<mailto:[hidden email]>.
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>.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/3B2C774A-8012-409D-9359-9C089F741DDF%40ircm.qc.ca.
Reply | Threaded
Open this post in threaded view
|

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

TheDeadOne
It's wonderful! Thank you very much.

четверг, 12 декабря 2019 г., 22:58:11 UTC+8 пользователь [hidden email] написал:
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: <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="DTrl5nrXAAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mybati...@...> on behalf of TheDeadOne <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="DTrl5nrXAAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">Sputte...@...>
Reply-To: "<a href="javascript:" target="_blank" gdf-obfuscated-mailto="DTrl5nrXAAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mybati...@..." <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="DTrl5nrXAAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mybati...@...>
Date: Thursday, December 12, 2019 at 9:37 AM
To: mybatis-user <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="DTrl5nrXAAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mybati...@...>
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="DTrl5nrXAAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mybati...@googlegroups.com<mailto:<a href="javascript:" target="_blank" gdf-obfuscated-mailto="DTrl5nrXAAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mybatis-user+unsubscribe@googlegroups.com>.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/mybatis-user/8b487a91-b118-460d-b5e9-57fabf56fa43%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/mybatis-user/8b487a91-b118-460d-b5e9-57fabf56fa43%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/mybatis-user/8b487a91-b118-460d-b5e9-57fabf56fa43%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/mybatis-user/8b487a91-b118-460d-b5e9-57fabf56fa43%40googlegroups.com<<a href="https://groups.google.com/d/msgid/mybatis-user/8b487a91-b118-460d-b5e9-57fabf56fa43%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/mybatis-user/8b487a91-b118-460d-b5e9-57fabf56fa43%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/mybatis-user/8b487a91-b118-460d-b5e9-57fabf56fa43%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/mybatis-user/8b487a91-b118-460d-b5e9-57fabf56fa43%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/9f336740-af7e-41ec-89a2-6da42a9fa75e%40googlegroups.com.