You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2006/06/02 12:39:37 UTC

svn commit: r411116 - in /incubator/servicemix/trunk/servicemix-services: ./ src/main/java/org/apache/servicemix/jdbc/ src/main/java/org/apache/servicemix/jdbc/adapter/ src/main/java/org/apache/servicemix/store/jdbc/ src/test/java/org/apache/servicemix...

Author: gnodet
Date: Fri Jun  2 03:39:37 2006
New Revision: 411116

URL: http://svn.apache.org/viewvc?rev=411116&view=rev
Log:
Add test for JdbcStore on top of managed data sources

Added:
    incubator/servicemix/trunk/servicemix-services/src/test/java/org/apache/servicemix/store/jdbc/JdbcStoreTransactionalTest.java
Modified:
    incubator/servicemix/trunk/servicemix-services/pom.xml
    incubator/servicemix/trunk/servicemix-services/src/main/java/org/apache/servicemix/jdbc/Statements.java
    incubator/servicemix/trunk/servicemix-services/src/main/java/org/apache/servicemix/jdbc/adapter/DefaultJDBCAdapter.java
    incubator/servicemix/trunk/servicemix-services/src/main/java/org/apache/servicemix/store/jdbc/JdbcStore.java

Modified: incubator/servicemix/trunk/servicemix-services/pom.xml
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-services/pom.xml?rev=411116&r1=411115&r2=411116&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-services/pom.xml (original)
+++ incubator/servicemix/trunk/servicemix-services/pom.xml Fri Jun  2 03:39:37 2006
@@ -52,6 +52,31 @@
       <artifactId>hsqldb</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.derby</groupId>
+      <artifactId>derby</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>jencks</groupId>
+      <artifactId>jencks</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>tranql</groupId>
+      <artifactId>tranql-connector</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>geronimo</groupId>
+      <artifactId>geronimo-transaction</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>geronimo</groupId>
+      <artifactId>geronimo-connector</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>

Modified: incubator/servicemix/trunk/servicemix-services/src/main/java/org/apache/servicemix/jdbc/Statements.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-services/src/main/java/org/apache/servicemix/jdbc/Statements.java?rev=411116&r1=411115&r2=411116&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-services/src/main/java/org/apache/servicemix/jdbc/Statements.java (original)
+++ incubator/servicemix/trunk/servicemix-services/src/main/java/org/apache/servicemix/jdbc/Statements.java Fri Jun  2 03:39:37 2006
@@ -27,7 +27,7 @@
     protected String storeTableName = "SM_STORE";
 
     protected String binaryDataType = "BLOB";
-    protected String idDataType = "VARCHAR(32)";
+    protected String idDataType = "VARCHAR(48)";
     private String storeDataStatement;
     private String updateDataStatement;
     private String removeDataStatement;

Modified: incubator/servicemix/trunk/servicemix-services/src/main/java/org/apache/servicemix/jdbc/adapter/DefaultJDBCAdapter.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-services/src/main/java/org/apache/servicemix/jdbc/adapter/DefaultJDBCAdapter.java?rev=411116&r1=411115&r2=411116&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-services/src/main/java/org/apache/servicemix/jdbc/adapter/DefaultJDBCAdapter.java (original)
+++ incubator/servicemix/trunk/servicemix-services/src/main/java/org/apache/servicemix/jdbc/adapter/DefaultJDBCAdapter.java Fri Jun  2 03:39:37 2006
@@ -67,6 +67,7 @@
     public void doCreateTables(Connection connection) throws SQLException, IOException {
         Statement s = null;
         try {
+            connection.setAutoCommit(false);
             
             // Check to see if the table already exists.  If it does, then don't log warnings during startup.
             // Need to run the scripts anyways since they may contain ALTER statements that upgrade a previous version of the table
@@ -78,6 +79,13 @@
             } catch (Throwable ignore) {
             } finally {
                 close(rs);
+            }
+            
+            // If the dataSource is a managed DataSource, executing a statement that throws
+            // an exception will make the connection unusable.
+            // So if the table already exists, do not try to re-create them
+            if (alreadyExists) {
+                return;
             }
             
             s = connection.createStatement();

Modified: incubator/servicemix/trunk/servicemix-services/src/main/java/org/apache/servicemix/store/jdbc/JdbcStore.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-services/src/main/java/org/apache/servicemix/store/jdbc/JdbcStore.java?rev=411116&r1=411115&r2=411116&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-services/src/main/java/org/apache/servicemix/store/jdbc/JdbcStore.java (original)
+++ incubator/servicemix/trunk/servicemix-services/src/main/java/org/apache/servicemix/store/jdbc/JdbcStore.java Fri Jun  2 03:39:37 2006
@@ -54,7 +54,6 @@
             out.close();
             connection = factory.getDataSource().getConnection();
             factory.getAdapter().doStoreData(connection, name + ":" + id, buffer.toByteArray());
-            connection.commit();
         } catch (Exception e) {
             throw (IOException) new IOException("Error storing object").initCause(e);
         } finally {
@@ -86,7 +85,6 @@
                 result = ois.readObject();
                 factory.getAdapter().doRemoveData(connection, name + ":" + id);
             }
-            connection.commit();
             return result;
         } catch (Exception e) {
             throw (IOException) new IOException("Error storing object").initCause(e);

Added: incubator/servicemix/trunk/servicemix-services/src/test/java/org/apache/servicemix/store/jdbc/JdbcStoreTransactionalTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-services/src/test/java/org/apache/servicemix/store/jdbc/JdbcStoreTransactionalTest.java?rev=411116&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-services/src/test/java/org/apache/servicemix/store/jdbc/JdbcStoreTransactionalTest.java (added)
+++ incubator/servicemix/trunk/servicemix-services/src/test/java/org/apache/servicemix/store/jdbc/JdbcStoreTransactionalTest.java Fri Jun  2 03:39:37 2006
@@ -0,0 +1,145 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.servicemix.store.jdbc;
+
+import java.sql.Connection;
+
+import javax.resource.spi.ConnectionManager;
+import javax.resource.spi.ManagedConnectionFactory;
+import javax.sql.DataSource;
+import javax.sql.XADataSource;
+import javax.transaction.TransactionManager;
+
+import junit.framework.TestCase;
+
+import org.apache.derby.jdbc.EmbeddedXADataSource;
+import org.apache.geronimo.connector.outbound.GenericConnectionManager;
+import org.apache.geronimo.connector.outbound.connectionmanagerconfig.NoPool;
+import org.apache.geronimo.connector.outbound.connectionmanagerconfig.XATransactions;
+import org.apache.geronimo.transaction.context.GeronimoTransactionManager;
+import org.apache.geronimo.transaction.context.TransactionContextManager;
+import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
+import org.apache.servicemix.store.Store;
+import org.apache.servicemix.store.StoreFactory;
+import org.tranql.connector.AllExceptionsAreFatalSorter;
+import org.tranql.connector.jdbc.AbstractXADataSourceMCF;
+
+public class JdbcStoreTransactionalTest extends TestCase {
+
+    private DataSource dataSource;
+    private Connection connection;
+    private StoreFactory factory;
+    private TransactionManager tm;
+
+    protected void setUp() throws Exception {
+        TransactionManagerImpl exTransactionManager = new TransactionManagerImpl(600, null, null);
+        TransactionContextManager transactionContextManager = new TransactionContextManager(exTransactionManager, exTransactionManager);
+        tm = (TransactionManager) new GeronimoTransactionManager(transactionContextManager);
+        
+        // Create an embedded database for testing tx results when commit / rollback
+        ConnectionManager cm = new GenericConnectionManager(
+                        new XATransactions(true, true),
+                        new NoPool(),
+                        false,
+                        null,
+                        transactionContextManager,
+                        "connectionManager",
+                        GenericConnectionManager.class.getClassLoader());
+        ManagedConnectionFactory mcf = new DerbyDataSourceMCF("target/testdb");
+        dataSource = (DataSource) mcf.createConnectionFactory(cm);
+        JdbcStoreFactory f = new JdbcStoreFactory();
+        f.setTransactional(true);
+        f.setDataSource(dataSource);
+        factory = f;
+    }
+    
+    protected void tearDown() throws Exception {
+        if (connection != null) {
+            connection.close();
+        }
+    }
+    
+    public void testStoreAndLoad() throws Exception {
+        Store store = factory.open("store");
+        String id = store.store(new Integer(10));
+        Integer i = (Integer) store.load(id);
+        assertEquals(10, i.intValue());
+        assertNull(store.load(id));
+        assertNull(store.load("a"));
+    }
+
+    public void testStoreAndLoadInOneTx() throws Exception {
+        Store store = factory.open("store");
+        tm.begin();
+        String id = store.store(new Integer(10));
+        Integer i = (Integer) store.load(id);
+        assertEquals(10, i.intValue());
+        assertNull(store.load(id));
+        assertNull(store.load("a"));
+        tm.commit();
+    }
+
+    public void testStoreAndLoadInTwoTx() throws Exception {
+        Store store = factory.open("store");
+        tm.begin();
+        String id = store.store(new Integer(10));
+        tm.commit();
+        tm.begin();
+        Integer i = (Integer) store.load(id);
+        assertEquals(10, i.intValue());
+        assertNull(store.load(id));
+        tm.commit();
+        assertNull(store.load("a"));
+    }
+
+    public void testStoreRollbackAndLoad() throws Exception {
+        Store store = factory.open("store");
+        tm.begin();
+        String id = store.store(new Integer(10));
+        tm.rollback();
+        tm.begin();
+        assertNull(store.load(id));
+        tm.commit();
+    }
+
+    public void testStoreRollbackAndLoadNonTx() throws Exception {
+        Store store = factory.open("store");
+        tm.begin();
+        String id = store.store(new Integer(10));
+        tm.rollback();
+        assertNull(store.load(id));
+    }
+
+    public static class DerbyDataSourceMCF extends AbstractXADataSourceMCF {
+        private static final long serialVersionUID = 7971682207810098396L;
+        protected DerbyDataSourceMCF(String dbName) {
+            super(createXADS(dbName), new AllExceptionsAreFatalSorter());
+        }
+        public String getPassword() {
+            return null;
+        }
+        public String getUserName() {
+            return null;
+        }
+        protected static XADataSource createXADS(String dbName) {
+            EmbeddedXADataSource xads = new EmbeddedXADataSource();
+            xads.setDatabaseName(dbName);
+            xads.setCreateDatabase("create");
+            return xads;
+        }
+    }
+    
+}