You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by bv...@apache.org on 2012/08/04 23:20:05 UTC
svn commit: r1369457 - in /camel/trunk/components/camel-mybatis/src:
main/java/org/apache/camel/component/mybatis/
test/java/org/apache/camel/component/mybatis/
Author: bvahdat
Date: Sat Aug 4 21:20:05 2012
New Revision: 1369457
URL: http://svn.apache.org/viewvc?rev=1369457&view=rev
Log:
CAMEL-5485: camel-mybatis should do a proper transaction demarcation while reading/writing from/to database.
Added:
camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertWithRollbackTest.java (with props)
Modified:
camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/DefaultMyBatisProcessingStrategy.java
camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
Modified: camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/DefaultMyBatisProcessingStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/DefaultMyBatisProcessingStrategy.java?rev=1369457&r1=1369456&r2=1369457&view=diff
==============================================================================
--- camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/DefaultMyBatisProcessingStrategy.java (original)
+++ camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/DefaultMyBatisProcessingStrategy.java Sat Aug 4 21:20:05 2012
@@ -28,14 +28,16 @@ public class DefaultMyBatisProcessingStr
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();
+ } catch (Exception e) {
+ session.rollback();
+ throw e;
+ } finally {
session.close();
}
}
@@ -43,7 +45,12 @@ public class DefaultMyBatisProcessingStr
public List<?> poll(MyBatisConsumer consumer, MyBatisEndpoint endpoint) throws Exception {
SqlSession session = endpoint.getSqlSessionFactory().openSession();
try {
- return session.selectList(endpoint.getStatement(), null);
+ List<Object> objects = session.selectList(endpoint.getStatement(), null);
+ session.commit();
+ return objects;
+ } catch (Exception e) {
+ session.rollback();
+ throw e;
} finally {
session.close();
}
Modified: camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java?rev=1369457&r1=1369456&r2=1369457&view=diff
==============================================================================
--- camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java (original)
+++ camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java Sat Aug 4 21:20:05 2012
@@ -77,6 +77,10 @@ public class MyBatisProducer extends Def
}
doProcessResult(exchange, result);
+ session.commit();
+ } catch (Exception e) {
+ session.rollback();
+ throw e;
} finally {
session.close();
}
@@ -97,6 +101,10 @@ public class MyBatisProducer extends Def
}
doProcessResult(exchange, result);
+ session.commit();
+ } catch (Exception e) {
+ session.rollback();
+ throw e;
} finally {
session.close();
}
@@ -122,12 +130,13 @@ public class MyBatisProducer extends Def
result = session.insert(statement);
doProcessResult(exchange, result);
}
+
+ session.commit();
+ } catch (Exception e) {
+ session.rollback();
+ throw e;
} finally {
- try {
- session.commit();
- } finally {
- session.close();
- }
+ session.close();
}
}
@@ -147,12 +156,13 @@ public class MyBatisProducer extends Def
result = session.insert(statement);
doProcessResult(exchange, result);
}
+
+ session.commit();
+ } catch (Exception e) {
+ session.rollback();
+ throw e;
} finally {
- try {
- session.commit();
- } finally {
- session.close();
- }
+ session.close();
}
}
@@ -176,12 +186,13 @@ public class MyBatisProducer extends Def
result = session.update(statement);
doProcessResult(exchange, result);
}
+
+ session.commit();
+ } catch (Exception e) {
+ session.rollback();
+ throw e;
} finally {
- try {
- session.commit();
- } finally {
- session.close();
- }
+ session.close();
}
}
@@ -205,12 +216,13 @@ public class MyBatisProducer extends Def
result = session.delete(statement);
doProcessResult(exchange, result);
}
+
+ session.commit();
+ } catch (Exception e) {
+ session.rollback();
+ throw e;
} finally {
- try {
- session.commit();
- } finally {
- session.close();
- }
+ session.close();
}
}
Added: camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertWithRollbackTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertWithRollbackTest.java?rev=1369457&view=auto
==============================================================================
--- camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertWithRollbackTest.java (added)
+++ camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertWithRollbackTest.java Sat Aug 4 21:20:05 2012
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.mybatis;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.ibatis.exceptions.PersistenceException;
+import org.junit.Test;
+
+public class MyBatisInsertWithRollbackTest extends MyBatisTestSupport {
+
+ @Test
+ public void testInsert() throws Exception {
+ getMockEndpoint("mock:commit").expectedMessageCount(0);
+ getMockEndpoint("mock:rollback").expectedMessageCount(1);
+ getMockEndpoint("mock:rollback").message(0).body().isEqualTo(null);
+ getMockEndpoint("mock:rollback").message(0).header(Exchange.EXCEPTION_CAUGHT).isInstanceOf(PersistenceException.class);
+
+ template.sendBody("direct:start", null);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ onException(PersistenceException.class).handled(true)
+ .to("mock:rollback");
+
+ from("direct:start")
+ .to("mybatis:insertAccount?statementType=Insert")
+ .to("mock:commit");
+ }
+ };
+ }
+
+}
Propchange: camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertWithRollbackTest.java
------------------------------------------------------------------------------
svn:eol-style = native