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>.