You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2011/09/28 08:11:24 UTC

svn commit: r1176742 - in /camel/trunk/components/camel-ibatis: ./ src/main/java/org/apache/camel/component/ibatis/ src/main/java/org/apache/camel/component/ibatis/strategy/ src/test/java/org/apache/camel/component/ibatis/

Author: davsclaus
Date: Wed Sep 28 06:11:24 2011
New Revision: 1176742

URL: http://svn.apache.org/viewvc?rev=1176742&view=rev
Log:
CAMEL-4387: Isolation level can now be configured on iBatis component. Thanks to Ioannis for the patch.

Added:
    camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/TransactionIsolationLevel.java   (with props)
    camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisBatchConsumerIsolationLevelTest.java   (with props)
Modified:
    camel/trunk/components/camel-ibatis/pom.xml
    camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisEndpoint.java
    camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/DefaultIBatisProcessingStategy.java
    camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/IBatisProcessingStrategy.java

Modified: camel/trunk/components/camel-ibatis/pom.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ibatis/pom.xml?rev=1176742&r1=1176741&r2=1176742&view=diff
==============================================================================
--- camel/trunk/components/camel-ibatis/pom.xml (original)
+++ camel/trunk/components/camel-ibatis/pom.xml Wed Sep 28 06:11:24 2011
@@ -68,6 +68,11 @@
       <artifactId>junit</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.easymock</groupId>
+      <artifactId>easymock</artifactId>
+      <scope>test</scope>
+    </dependency>
 
   </dependencies>
 

Modified: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisEndpoint.java?rev=1176742&r1=1176741&r2=1176742&view=diff
==============================================================================
--- camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisEndpoint.java (original)
+++ camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisEndpoint.java Wed Sep 28 06:11:24 2011
@@ -17,20 +17,18 @@
 package org.apache.camel.component.ibatis;
 
 import java.io.IOException;
-
 import com.ibatis.sqlmap.client.SqlMapClient;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.component.ibatis.strategy.DefaultIBatisProcessingStategy;
 import org.apache.camel.component.ibatis.strategy.IBatisProcessingStrategy;
+import org.apache.camel.component.ibatis.strategy.TransactionIsolationLevel;
 import org.apache.camel.impl.DefaultPollingEndpoint;
 import org.apache.camel.util.ObjectHelper;
 
 /**
  * An <a href="http://camel.apache.org/ibatis.html>iBatis Endpoint</a>
  * for performing SQL operations using an XML mapping file to abstract away the SQL
- *
- * @version 
  */
 public class IBatisEndpoint extends DefaultPollingEndpoint {
     private IBatisProcessingStrategy strategy;
@@ -52,7 +50,7 @@ public class IBatisEndpoint extends Defa
     public IBatisComponent getComponent() {
         return (IBatisComponent) super.getComponent();
     }
-    
+
     public boolean isSingleton() {
         return true;
     }
@@ -93,11 +91,11 @@ public class IBatisEndpoint extends Defa
 
     /**
      * Statement to run when polling or processing
-    */
+     */
     public String getStatement() {
         return statement;
     }
-    
+
     /**
      * Statement to run when polling or processing
      */
@@ -135,4 +133,13 @@ public class IBatisEndpoint extends Defa
     public void setMaxMessagesPerPoll(int maxMessagesPerPoll) {
         this.maxMessagesPerPoll = maxMessagesPerPoll;
     }
+
+
+    public String getIsolation() throws Exception {
+        return TransactionIsolationLevel.nameOf(strategy.getIsolation());
+    }
+
+    public void setIsolation(String isolation) throws Exception {
+        strategy.setIsolation(TransactionIsolationLevel.intValueOf(isolation));
+    }
 }

Modified: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/DefaultIBatisProcessingStategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/DefaultIBatisProcessingStategy.java?rev=1176742&r1=1176741&r2=1176742&view=diff
==============================================================================
--- camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/DefaultIBatisProcessingStategy.java (original)
+++ camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/DefaultIBatisProcessingStategy.java Wed Sep 28 06:11:24 2011
@@ -29,13 +29,15 @@ import org.apache.camel.component.ibatis
  */
 public class DefaultIBatisProcessingStategy implements IBatisProcessingStrategy {
 
+    private int isolation = Connection.TRANSACTION_REPEATABLE_READ;
+
     public void commit(IBatisEndpoint endpoint, Exchange exchange, Object data, String consumeStatements) throws Exception {
         SqlMapClient client = endpoint.getSqlMapClient();
         boolean useTrans = endpoint.isUseTransactions();
         String[] statements = consumeStatements.split(",");
         try {
             if (useTrans) {
-                client.startTransaction(Connection.TRANSACTION_REPEATABLE_READ);
+                client.startTransaction(isolation);
             }
             for (String statement : statements) {
                 client.update(statement.trim(), data);
@@ -54,4 +56,12 @@ public class DefaultIBatisProcessingStat
         SqlMapClient client = endpoint.getSqlMapClient();
         return client.queryForList(endpoint.getStatement(), null);
     }
+
+    public int getIsolation() {
+        return isolation;
+    }
+
+    public void setIsolation(int isolation) {
+        this.isolation = isolation;
+    }
 }

Modified: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/IBatisProcessingStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/IBatisProcessingStrategy.java?rev=1176742&r1=1176741&r2=1176742&view=diff
==============================================================================
--- camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/IBatisProcessingStrategy.java (original)
+++ camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/IBatisProcessingStrategy.java Wed Sep 28 06:11:24 2011
@@ -47,4 +47,18 @@ public interface IBatisProcessingStrateg
      * @throws Exception can be thrown in case of error
      */
     void commit(IBatisEndpoint endpoint, Exchange exchange, Object data, String consumeStatements) throws Exception;
+
+    /**
+     * Returns the transaction isolation level set on the processing strategy.
+     *
+     * @return the transaction isolation level.
+     */
+    int getIsolation();
+
+    /**
+     * Sets the transaction isolation level on the processing strategy.
+     *
+     * @param isolation the transaction isolation level.
+     */
+    void setIsolation(int isolation);
 }

Added: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/TransactionIsolationLevel.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/TransactionIsolationLevel.java?rev=1176742&view=auto
==============================================================================
--- camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/TransactionIsolationLevel.java (added)
+++ camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/TransactionIsolationLevel.java Wed Sep 28 06:11:24 2011
@@ -0,0 +1,56 @@
+/**
+ * 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.ibatis.strategy;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public enum TransactionIsolationLevel {
+
+    TRANSACTION_NONE(0),
+    TRANSACTION_READ_UNCOMMITTED(1),
+    TRANSACTION_READ_COMMITTED(2),
+    TRANSACTION_REPEATABLE_READ(4),
+    TRANSACTION_SERIALIZABLE(8);
+
+    protected static final Map<Integer, TransactionIsolationLevel> MAP = new HashMap<Integer, TransactionIsolationLevel>();
+
+    static {
+        for (TransactionIsolationLevel transactionIsolationLevel : values()) {
+            MAP.put(transactionIsolationLevel.getValue(), transactionIsolationLevel);
+        }
+    }
+
+    int value;
+
+    TransactionIsolationLevel(int value) {
+        this.value = value;
+    }
+
+    public static int intValueOf(String name) {
+        return valueOf(name).getValue();
+    }
+
+    public static String nameOf(int level) {
+        return MAP.get(level).name();
+    }
+
+    public int getValue() {
+        return value;
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/TransactionIsolationLevel.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/TransactionIsolationLevel.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisBatchConsumerIsolationLevelTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisBatchConsumerIsolationLevelTest.java?rev=1176742&view=auto
==============================================================================
--- camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisBatchConsumerIsolationLevelTest.java (added)
+++ camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisBatchConsumerIsolationLevelTest.java Wed Sep 28 06:11:24 2011
@@ -0,0 +1,122 @@
+/**
+ * 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.ibatis;
+
+import java.sql.Connection;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.ibatis.strategy.IBatisProcessingStrategy;
+import org.apache.camel.component.ibatis.strategy.TransactionIsolationLevel;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.easymock.EasyMock;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+
+public class IBatisBatchConsumerIsolationLevelTest extends CamelTestSupport {
+
+    IBatisProcessingStrategy strategyMock = createMock(IBatisProcessingStrategy.class);
+
+
+    @Test
+    public void testConsumeWithIsolation() throws Exception {
+        Account account1 = new Account();
+        account1.setId(1);
+        account1.setFirstName("Bob");
+        account1.setLastName("Denver");
+        account1.setEmailAddress("TryGuessingGilligan@gmail.com");
+
+        Account account2 = new Account();
+        account2.setId(2);
+        account2.setFirstName("Alan");
+        account2.setLastName("Hale");
+        account2.setEmailAddress("TryGuessingSkipper@gmail.com");
+
+        List<Account> accounts = new ArrayList<Account>();
+        accounts.add(account1);
+        accounts.add(account2);
+
+        strategyMock.setIsolation(TransactionIsolationLevel.TRANSACTION_READ_COMMITTED.getValue());
+        expectLastCall().once();
+
+        expect(strategyMock.poll(EasyMock.<IBatisConsumer>anyObject(), EasyMock.<IBatisEndpoint>anyObject())).andReturn(accounts).atLeastOnce();
+
+        strategyMock.commit(EasyMock.<IBatisEndpoint>anyObject(), EasyMock.<Exchange>anyObject(), anyObject(), EasyMock.<String>anyObject());
+        expectLastCall().atLeastOnce();
+        replay(strategyMock);
+
+        IBatisEndpoint iBatisEndpoint = resolveMandatoryEndpoint("ibatis:selectUnprocessedAccounts?consumer.onConsume=consumeAccount", IBatisEndpoint.class);
+        iBatisEndpoint.setStrategy(strategyMock);
+        iBatisEndpoint.setIsolation("TRANSACTION_READ_COMMITTED");
+
+
+        template.sendBody("direct:start", account1);
+        template.sendBody("direct:start", account2);
+        //We need to wait for the batch process to complete.
+        Thread.sleep(1000);
+        verify(strategyMock);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                // START SNIPPET: e1
+                from("ibatis:selectUnprocessedAccounts?consumer.onConsume=consumeAccount").to("mock:results");
+                // END SNIPPET: e1
+
+                from("direct:start").to("ibatis:insertAccount?statementType=Insert");
+            }
+        };
+    }
+
+    @Override
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+
+        // lets create the database...
+        IBatisEndpoint endpoint = resolveMandatoryEndpoint("ibatis:Account", IBatisEndpoint.class);
+        Connection connection = endpoint.getSqlMapClient().getDataSource().getConnection();
+        Statement statement = connection.createStatement();
+        statement.execute("create table ACCOUNT ( ACC_ID INTEGER , ACC_FIRST_NAME VARCHAR(255), ACC_LAST_NAME VARCHAR(255), ACC_EMAIL VARCHAR(255), PROCESSED BOOLEAN DEFAULT false)");
+        connection.close();
+    }
+
+    @Override
+    @After
+    public void tearDown() throws Exception {
+        IBatisEndpoint endpoint = resolveMandatoryEndpoint("ibatis:Account", IBatisEndpoint.class);
+        Connection connection = endpoint.getSqlMapClient().getDataSource().getConnection();
+        Statement statement = connection.createStatement();
+        statement.execute("drop table ACCOUNT");
+        connection.close();
+
+        super.tearDown();
+    }
+}

Propchange: camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisBatchConsumerIsolationLevelTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisBatchConsumerIsolationLevelTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date