Re: Is MyBatis(3.0) mapper thread safe

451 views
Skip to first unread message

Tim

unread,
Oct 3, 2012, 10:33:40 AM10/3/12
to mybati...@googlegroups.com
In normal usages, yes. For a better/more complete answer please refer to http://www.mybatis.org/spring/sqlsession.html

On Wed, Oct 3, 2012 at 7:57 AM, Prabu Uthirapathi <u.p...@gmail.com> wrote:
Hi,
     I am working on mybats3.0+spring 3.0. I am working on spring services. spring service has a dependency on MyBatis mapper class. I would like to know whether MyBatis3.0 mapper is thread safe. 



Duc Trung TRAN

unread,
Oct 3, 2012, 11:34:58 AM10/3/12
to mybati...@googlegroups.com
Hello,

The mapped statements build up is not thread safe. In our project, we had to preload all statements when building sqlSessionFactory. But it was more than 1 year ago. The problem could be fixed since then.

Trung

2012/10/3 Tim <che...@gmail.com>

Prabu Uthirapathi

unread,
Oct 5, 2012, 8:34:30 AM10/5/12
to mybati...@googlegroups.com
For example: 

                     public class SpringServiceImpl implements SpringService
                     {

                               @Autowired
                               UserMapper userMapper; 


                                     
                     }


Is userMapper thread safe (I am using spring-mybatis)? 

Eduardo Macarron

unread,
Oct 5, 2012, 10:24:24 AM10/5/12
to mybati...@googlegroups.com
yes, it is.

Duc Trung TRAN

unread,
Oct 5, 2012, 12:03:45 PM10/5/12
to mybati...@googlegroups.com
I don't know how UserMapper is implemented but i suppose it use sqlSession internally. In this case it is not thread safe when mapped statements are not loaded. You can test by creating some threads which concurrently execute the same method in UserMapper. There will be exceptions raised.

But once the mapped statement is loaded, it's thread safe.


2012/10/5 Eduardo Macarron <eduardo....@gmail.com>

Eduardo Macarron

unread,
Oct 5, 2012, 1:34:21 PM10/5/12
to mybati...@googlegroups.com
A SqlSession got from SqlSessionFactory is not thread safe as the manual says


But when using Spring, mappers use a different SqlSession that is both transactional and thread safe.



2012/10/5 Duc Trung TRAN <ductru...@gmail.com>

Duc Trung TRAN

unread,
Oct 5, 2012, 6:05:55 PM10/5/12
to mybati...@googlegroups.com
Eduardo,

Yes, I've been using SqlSessionTemplate And it does not work.  The thread safety problem is from within Configuration class of Ibatis which is used by SqlSession. 

It's possible that i did not use spring mybatis api correctly. But can you first try to do some tests as I suggest?

 

2012/10/5 Eduardo Macarron <eduardo....@gmail.com>

Eduardo Macarron

unread,
Oct 6, 2012, 12:59:33 AM10/6/12
to mybati...@googlegroups.com
Configuration is a singleton both when using MyBatis alone and also when using it with Spring. 

Please first check your config againts the mybatis-spring manual.


2012/10/6 Duc Trung TRAN <ductru...@gmail.com>

Eduardo Macarron

unread,
Oct 6, 2012, 1:03:12 AM10/6/12
to mybati...@googlegroups.com
BTW the spring application context will not be started until all mapped statements are loaded. That loading process is not thread safe and it is expected to be done by the startup thread.

If you are loading mappers lazily, thread safety problems may arise. Not sure about that.

2012/10/6 Eduardo Macarron <eduardo....@gmail.com>

Prabu Uthirapathi

unread,
Oct 7, 2012, 4:11:20 AM10/7/12
to mybati...@googlegroups.com
currently all the mapped statements are loaded via the following config 

          <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xyz.persistence" />
    </bean>


Is that OK? 

Prabu Uthirapathi

unread,
Oct 7, 2012, 4:19:54 AM10/7/12
to mybati...@googlegroups.com
Please see the following code for the implementation of UserMapper

public interface UserMapper
{

@Select({ "select * from USER"})
List<User> getAllUsers()

Eduardo Macarron

unread,
Oct 8, 2012, 8:15:18 AM10/8/12
to mybati...@googlegroups.com
Yes, that is ok.

2012/10/7 Prabu Uthirapathi <u.p...@gmail.com>

Prabu Uthirapathi

unread,
Oct 8, 2012, 10:13:34 AM10/8/12
to mybati...@googlegroups.com
can you please tell me how to create a start up thread to load the mapped statements. 


On Saturday, October 6, 2012 10:33:14 AM UTC+5:30, Eduardo wrote:
Reply all
Reply to author
Forward
0 new messages