You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by ganesan <sa...@yahoo.com> on 2012/08/03 17:58:20 UTC

MyBatis Component Bug: consumer.onConsume hits error, transactions are committed instead of rollback

Hi
Camel 2.10 version:

When the onCosume , sql hits error , instead of rolling back, the
transaction gets committed. This results in unwarranted transaction
integrity issues. if you multiple comma separted statements, if the error is
hit , the earlier statement partial updates are committed. 
Worst there is no error thrown back, so the routes do not know what's going
on


Looking at the MyBatis component source code:
DefaultMyBatisProcessingStrategy 


The root cause there is a session.commit on finally block

public void commit(MyBatisEndpoint endpoint, Exchange exchange, Object data,
String consumeStatements) throws Exception {
        SqlSession session = endpoint.getSqlSessionFactory().openSession();

        String[] statements = consumeStatements.split(",");
        try {
            for (String statement : statements) {
                session.update(statement.trim(), data);
            }
        } finally {
            session.commit();
            session.close();
        }
    }
        
Suggested Fix:(Move the commit  after successful updates in try block.
Rollback any updates in catch block. finally just closes the session. It
will also automatically throws back the error)

public void commit(MyBatisEndpoint endpoint, Exchange exchange, Object data,
String consumeStatements) throws Exception {
        SqlSession session = endpoint.getSqlSessionFactory().openSession();

        String[] statements = consumeStatements.split(",");
        try {
            for (String statement : statements) {
                session.update(statement.trim(), data);
            }
            session.commit(); // CHANGE: COMMIT IF ALL ARE SUCCESSFUL
       
          } catch{

            session.rollback(); // ANY ERROR, ROLLBACK INTERMEDIATE
TRANSACTION CHANGES
        }

        } finally {

            session.close();
        }
    }




--
View this message in context: http://camel.465427.n5.nabble.com/MyBatis-Component-Bug-consumer-onConsume-hits-error-transactions-are-committed-instead-of-rollback-tp5716774.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: MyBatis Component Bug: consumer.onConsume hits error, transactions are committed instead of rollback

Posted by Ganesan Sankara <sa...@yahoo.com>.
Hi Babak Vahdat,
Thanks for the fix. Will look into contributing to camel. 



--
View this message in context: http://camel.465427.n5.nabble.com/MyBatis-Component-Bug-consumer-onConsume-hits-error-transactions-are-committed-instead-of-rollback-tp5716774p5717003.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: MyBatis Component Bug: consumer.onConsume hits error, transactions are committed instead of rollback

Posted by Babak Vahdat <ba...@swissonline.ch>.
Hi

Thanks for spotting & reporting this, I opened a JIRA to track this:

https://issues.apache.org/jira/browse/CAMEL-5485

In the future you're welcome to contribute to Apache Camel if you like:

http://camel.apache.org/contributing.html

Babak



--
View this message in context: http://camel.465427.n5.nabble.com/MyBatis-Component-Bug-consumer-onConsume-hits-error-transactions-are-committed-instead-of-rollback-tp5716774p5716838.html
Sent from the Camel - Users mailing list archive at Nabble.com.