MyBatis 3 -- ensuring batch inserts?

1,482 views
Skip to first unread message

Denis Haskin

unread,
Aug 5, 2010, 1:52:55 PM8/5/10
to mybatis-user
I'd like a sanity check on whether I've got MyBatis 3 set up correctly
to be doing batched inserts. I think I do, but db behavior makes me
think maybe it isn't happening. Db is MySQL, by the way.

My configuration xml has:

<settings>
<setting name="useGeneratedKeys" value="true"/>
<setting name="defaultExecutorType" value="BATCH"/>
</settings>
<environments default="all">
<environment id="all">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
...etc...

I'm acquiring the mapper in the constructor of a singleton:

String resource = "META-INF/ibatis-configuration.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMapper = new SqlSessionFactoryBuilder().build(reader);

and then to do the actual inserting:

SqlSession session = sqlMapper.openSession();
...
lots of:
session.insert("PosMessageInsert", decodedPosMsg);
...
then:
session.close();

My mapper XML for that insert is:

<insert
id="PosMessageInsert"
parameterType="com.globalflows.model.PosMessage"
flushCache="true"
statementType="PREPARED"
keyProperty="id"
useGeneratedKeys="true"
timeout="20000">
insert into pos_messages
(
msg_timestamp,
...etc...

Does this all look kosher? Sniffing traffic to the db server, we're
definitely seeing some connections that appear NOT to be setting
autocommit=0

Thanks!

dwh

Denis Haskin

unread,
Aug 5, 2010, 2:54:49 PM8/5/10
to mybatis-user
We've actually confirmed that insertions are not being batched, and
that we're not getting multi-row inserts. Can someone give me some
recommendations on what configuration changes I should make?

It's unclear from reading the doc whether I need to specify
ExecutorType.BATCH in my openSession(), or whether what I have here
will make that be the default.

Suggestions?

Thanks,

dwh

Nathan Maves

unread,
Aug 5, 2010, 3:29:15 PM8/5/10
to mybati...@googlegroups.com
yes you do :)  

The default is set to ExecutorType.SIMPLE which will not batch for you.  You can set it for that SqlSession and the default will remain as SIMPLE.  You can also override the default if you want/need.

Nathan

Denis Haskin

unread,
Aug 5, 2010, 4:06:40 PM8/5/10
to mybati...@googlegroups.com
So why did I need to set

<setting name="defaultExecutorType" value="BATCH"/>
?

And is there anyway to control the batch size?

Also... can MyBatis do multi-row INSERT statements?

dwh

Nathan Maves

unread,
Aug 5, 2010, 4:28:27 PM8/5/10
to mybati...@googlegroups.com
you should not have to set the default if you use the following...

openSession(ExecutorType.BATCH);

Senthil

unread,
Aug 6, 2010, 5:29:22 AM8/6/10
to mybatis-user
Hi,
When you say the executorType to "Batch", then it means that all
the inserts will be done in a single hit to db?

Actually i have a list of objects which i need to insert.

Now ill iterate , update the vo and call insert() as mentioned by
Denis.This will ensure that all these inserts will be done by hitting
the db once?

Please confirm.

Thanks.

Regards
Senthil Kumar Sekar


On Aug 6, 1:28 am, Nathan Maves <nathan.ma...@gmail.com> wrote:
> you should not have to set the default if you use the following...
>
> openSession(ExecutorType.BATCH);
>
> On Thu, Aug 5, 2010 at 2:06 PM, Denis Haskin <de...@haskinferguson.net>wrote:
>
>
>
> > So why did I need to set
> >  <setting name="defaultExecutorType" value="BATCH"/>
> > ?
>
> > And is there anyway to control the batch size?
>
> > Also... can MyBatis do multi-row INSERT statements?
>
> > dwh
>
> > On Thu, Aug 5, 2010 at 3:29 PM, Nathan Maves <nathan.ma...@gmail.com>
> > >> > dwh- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages