Using MyBatis unable to rollback data inserted by stored procedure

596 views
Skip to first unread message

Kiran

unread,
Apr 9, 2014, 10:58:46 AM4/9/14
to mybati...@googlegroups.com
Hi Experts,
I am beginner to MyBatis. I followed http://loianegroner.com/2011/03/ibatis-mybatis-working-with-stored-procedures/ to call stored prcoedure using Mybatis. Below is part of my code.
Record is commiting in databse even i say session.rollback(). I am not sure it is my code issue or something else. Please help me ? 
 
Jar : mybatis-3.2.4.jar
 
SqlMapConfig XML :
  <environments default="development">
  <environment id="development">
    <transactionManager type="JDBC"/>
   <dataSource type="POOLED">
    <property name="driver" value="com.ibm.db2.jcc.DB2Driver"/>
    <property name="url" value="jdbc:db2://abc:123/DB2ABCD:retrieveMessagesFromServerOnGetMessage=true;"/>
    <property name="username" value="****"/>
    <property name="password" value="****"/>
   </dataSource>
    </environment>
 </environments>
 
Code:
SqlSessionFactory sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
SqlSession session = sqlSessionFactory.openSession(false);
SPMapper spMapper = session.getMapper(SPMapper.class);
try {
 Param param = new Param();
 spMapper.callInsertCity(param);
 return param.getCityName();
} finally {
 session.rollback();
 session.close();
}
 
Thanks in advance...

Eduardo Macarron

unread,
Apr 9, 2014, 1:33:11 PM4/9/14
to mybati...@googlegroups.com
May you have a commit inside the procedure?


--
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 mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kiran

unread,
Apr 10, 2014, 11:15:43 AM4/10/14
to mybati...@googlegroups.com
Thank you Edurdo for your reply.
I do not have commit in stored procedure. I tried to rollback using Spring transaction manager (No My Batis), and data was rollbacked successfully.
Please let me know thoughts ?

Eduardo Macarron

unread,
Apr 10, 2014, 12:08:28 PM4/10/14
to mybati...@googlegroups.com
Enabling logging for org.apache.ibatis.transaction.jdbc.JdbcTransaction should bring some light.


--

Jose María Zaragoza

unread,
Apr 10, 2014, 12:12:31 PM4/10/14
to mybati...@googlegroups.com
2014-04-10 18:08 GMT+02:00 Eduardo Macarron <eduardo....@gmail.com>:
> Enabling logging for org.apache.ibatis.transaction.jdbc.JdbcTransaction
> should bring some light.

One question

Does MyBatis need to set autocommit to false ( in openSession() ) if
you want to use commit() and rollback() ?

Regards

Jose María Zaragoza

unread,
Apr 10, 2014, 12:15:18 PM4/10/14
to mybati...@googlegroups.com
2014-04-10 18:12 GMT+02:00 Jose María Zaragoza <demab...@gmail.com>:
> 2014-04-10 18:08 GMT+02:00 Eduardo Macarron <eduardo....@gmail.com>:
>> Enabling logging for org.apache.ibatis.transaction.jdbc.JdbcTransaction
>> should bring some light.
>
> One question
>
> Does MyBatis need to set autocommit to false ( in openSession() ) if
> you want to use commit() and rollback() ?
>
> Regards

I answer myself: it doesn't do it ( according to documentation )
By default it's set to true

Eduardo Macarron

unread,
Apr 10, 2014, 12:19:28 PM4/10/14
to mybati...@googlegroups.com
Hi Jose, autocommit is set to false by default, so there is no need to pass false.

The doc says "To enable auto-commit, pass a value of true to the optional autoCommit parameter." Is there any other reference to autocommit that says something different?

Jose María Zaragoza

unread,
Apr 10, 2014, 1:19:41 PM4/10/14
to mybati...@googlegroups.com
2014-04-10 18:19 GMT+02:00 Eduardo Macarron <eduardo....@gmail.com>:
> Hi Jose, autocommit is set to false by default, so there is no need to pass
> false.
>
> The doc says "To enable auto-commit, pass a value of true to the optional
> autoCommit parameter." Is there any other reference to autocommit that says
> something different?
>

Well, I read "Transaction Control Methods" paragraph in online
documentation (http://mybatis.github.io/mybatis-3/java-api.html)

and it says

"There are four methods for controlling the scope of a transaction. Of
course, these have no effect if you've chosen to use auto-commit or if
you're using an external transaction manager. "

If you don't have read "SqlSessionFactory" paragraph before, you can
think that it's used the default JDBC driver autocommit value (
usually is true)

But it's ok. Thanks

Kiran

unread,
Apr 11, 2014, 2:21:09 PM4/11/14
to mybati...@googlegroups.com
Thank you very much for responses.
 
I also tried to call with passing auto commit value, but still same issue.

SqlSession session = sqlSessionFactory.openSession();

With the same code i tried to insert directly to the table (instead of stored procedure), i was to rollback data successfully!
 
So i thought may be problem with stored procedure. As i mentioned earlier I tried to rollback using Spring transaction manager (No My Batis), and data was rollbacked successfully.
 
I am not getting any clue except moving to spring transaction manager, Per Eduardo suggestion i will to enable enablelogging for org.apache.ibatis.transaction.jdbc.JdbcTransaction.
 
Please share your thoughts ???

On Wednesday, April 9, 2014 10:58:46 AM UTC-4, Kiran wrote:

Kiran

unread,
Apr 11, 2014, 3:19:34 PM4/11/14
to mybati...@googlegroups.com
Sorry correction on my statement ***********
 
I also tried to call session without passing auto commit value, but still same issue.

SqlSession session = sqlSessionFactory.openSession();

Reply all
Reply to author
Forward
0 new messages