MyBatis + Spring + Batch Inserts

7,414 views
Skip to first unread message

VegHead

unread,
Aug 6, 2010, 7:51:29 PM8/6/10
to mybatis-user
Has anyone had any success getting MyBatis to do batch inserts with
Spring? If so, could you share some sample code?

I'm currently using the work Putthibong Boonbong did to support
MyBatis in Spring (see
https://jira.springsource.org/browse/SPR-5991?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#issue-tabs).

Thanks.

-Sean

VegHead

unread,
Aug 17, 2010, 5:38:14 PM8/17/10
to mybatis-user
Anyone? Bueller? Bueller?

On Aug 6, 6:51 pm, VegHead <organicveg...@gmail.com> wrote:
> Has anyone had any success getting MyBatis to do batch inserts with
> Spring? If so, could you share some sample code?
>
> I'm currently using the work Putthibong Boonbong did to support
> MyBatis in Spring (seehttps://jira.springsource.org/browse/SPR-5991?page=com.atlassian.jira...).
>
> Thanks.
>
> -Sean

Tim

unread,
Aug 17, 2010, 5:45:14 PM8/17/10
to mybati...@googlegroups.com
What db?

VegHead

unread,
Aug 17, 2010, 6:21:59 PM8/17/10
to mybatis-user
MySQL 5.1

Poitras Christian

unread,
Aug 18, 2010, 8:46:48 AM8/18/10
to mybati...@googlegroups.com
I've done this with Spring 2.5.

Simply create an SqlSession with BATCH as ExecutorType.
@Transactional
public void insert(Collection<DatasetData> datas) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
try {
DatasetDataMapper datasetDataMapper = sqlSession.getMapper(DatasetDataMapper.class);
for (DatasetData data: datas) {
datasetDataMapper.insertBatch(data);
}
sqlSession.commit();
} finally {
sqlSession.close();
}
}

Christian

Roy Bailey

unread,
Nov 3, 2010, 4:14:47 PM11/3/10
to mybati...@googlegroups.com
Having trouble getting BATCH performance improvement over SIMPLE
statements with MyBatis v3.0.2 and Oracle 10 calling stored procedure
statement. Maybe a general database/experience question as I see
nothing wrong with MyBatis behavior. Would be grateful for any
suggestions of what to look for or little knowledge jems. Here is a
summary of what I've tried:

I have a { call myproc( {#param1, VARCHAR}, {#param2, VARCHAR} ... ) }
statement and mapper function.
I loop through the dataset and call the mapper method for each record.
If I run with SIMPLE I can see multiple prepared statements and executes.
If I run with BATCH I can see no statements, just a pause in the logging
where you would expect.
The time to call with 100 records ~8 seconds, 1000 records ~80 seconds.
The time makes no difference if I run with SIMPLE or BATCH ExecutorType.
I've stepped through MyBatic BatchExecutor code and confirmed the same
statement is reused, and that executeBatch() is called and is where all
the time is spent.
I've also tried with POOLED, apache BasicDataSource, apache
PooledDataSource, C3P0 ComboPooledDataSource, none make any difference.

I'm wondering if this is a limitation of calling stored procedure rather
than insert/update sql statement, though this would seem wrong to me.
But like I said, probably I'm missing something fundamental in my
understanding. Any ideas gratefully received.

thanks...Roy

Reply all
Reply to author
Forward
0 new messages