You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by cl...@apache.org on 2018/04/18 13:53:22 UTC

[3/3] activemq-artemis git commit: ARTEMIS-1653 Allow database tables to be created externally

ARTEMIS-1653 Allow database tables to be created externally

(cherry picked from commit eab498456762c3df0f786b1f9ae4e372fdbbfa32)


Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/af15fc43
Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/af15fc43
Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/af15fc43

Branch: refs/heads/1.x
Commit: af15fc43c663977be3cf2f82865936e3f5d02cda
Parents: 9b534f8
Author: Niels Lippke <nl...@gmx.de>
Authored: Sun Jan 28 17:53:35 2018 +0100
Committer: Clebert Suconic <cl...@apache.org>
Committed: Wed Apr 18 09:53:17 2018 -0400

----------------------------------------------------------------------
 .../jdbc/store/drivers/AbstractJDBCDriver.java  | 30 +++++++-----
 .../server/impl/jdbc/JdbcLeaseLockTest.java     | 33 ++++++++++++-
 .../core/server/impl/jdbc/TestJDBCDriver.java   | 50 ++++++++++++++++++++
 3 files changed, 101 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/af15fc43/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/AbstractJDBCDriver.java
----------------------------------------------------------------------
diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/AbstractJDBCDriver.java b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/AbstractJDBCDriver.java
index d34b31f..b0be3ae 100644
--- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/AbstractJDBCDriver.java
+++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/AbstractJDBCDriver.java
@@ -110,7 +110,7 @@ public abstract class AbstractJDBCDriver {
    protected abstract void createSchema() throws SQLException;
 
    protected final void createTable(String... schemaSqls) throws SQLException {
-      createTableIfNotExists(connection, sqlProvider.getTableName(), schemaSqls);
+      createTableIfNotExists(sqlProvider.getTableName(), schemaSqls);
    }
 
    private void connect() throws SQLException {
@@ -175,9 +175,7 @@ public abstract class AbstractJDBCDriver {
       }
    }
 
-   private static void createTableIfNotExists(Connection connection,
-                                              String tableName,
-                                              String... sqls) throws SQLException {
+   private void createTableIfNotExists(String tableName, String... sqls) throws SQLException {
       logger.tracef("Validating if table %s didn't exist before creating", tableName);
       try {
          connection.setAutoCommit(false);
@@ -190,17 +188,27 @@ public abstract class AbstractJDBCDriver {
                if (sqlWarning != null) {
                   logger.warn(JDBCUtils.appendSQLExceptionDetails(new StringBuilder(), sqlWarning));
                }
-               try (Statement statement = connection.createStatement()) {
-                  for (String sql : sqls) {
-                     statement.executeUpdate(sql);
-                     final SQLWarning statementSqlWarning = statement.getWarnings();
-                     if (statementSqlWarning != null) {
-                        logger.warn(JDBCUtils.appendSQLExceptionDetails(new StringBuilder(), statementSqlWarning, sql));
-                     }
+            } else {
+               try (Statement statement = connection.createStatement();
+                     ResultSet cntRs = statement.executeQuery(sqlProvider.getCountJournalRecordsSQL())) {
+                  if (rs.next() && rs.getInt(1) > 0) {
+                     logger.tracef("Table %s did exist but is not empty. Skipping initialization.", tableName);
+                  } else {
+                     sqls = Arrays.copyOfRange(sqls, 1, sqls.length);
+                  }
+               }
+            }
+            try (Statement statement = connection.createStatement()) {
+               for (String sql : sqls) {
+                  statement.executeUpdate(sql);
+                  final SQLWarning statementSqlWarning = statement.getWarnings();
+                  if (statementSqlWarning != null) {
+                     logger.warn(JDBCUtils.appendSQLExceptionDetails(new StringBuilder(), statementSqlWarning, sql));
                   }
                }
             }
          }
+
          connection.commit();
       } catch (SQLException e) {
          final String sqlStatements = Stream.of(sqls).collect(Collectors.joining("\n"));

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/af15fc43/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/impl/jdbc/JdbcLeaseLockTest.java
----------------------------------------------------------------------
diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/impl/jdbc/JdbcLeaseLockTest.java b/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/impl/jdbc/JdbcLeaseLockTest.java
index 3c8de45..2ca08d4 100644
--- a/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/impl/jdbc/JdbcLeaseLockTest.java
+++ b/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/impl/jdbc/JdbcLeaseLockTest.java
@@ -18,6 +18,8 @@
 package org.apache.activemq.artemis.core.server.impl.jdbc;
 
 import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.List;
 import java.util.UUID;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
@@ -33,13 +35,31 @@ import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
 
+@RunWith(Parameterized.class)
 public class JdbcLeaseLockTest extends ActiveMQTestBase {
 
    private JdbcSharedStateManager jdbcSharedStateManager;
    private DatabaseStorageConfiguration dbConf;
    private SQLProvider sqlProvider;
 
+   @Parameterized.Parameters(name = "create_tables_prior_test")
+   public static List<Object[]> data() {
+      return Arrays.asList(new Object[][] {
+         {true, null},
+         {false, null}
+      });
+   }
+
+   @Parameter(0)
+   public boolean withExistingTable;
+   @Parameter(1)
+   public Object result;
+
+
    private LeaseLock lock() {
       return lock(dbConf.getJdbcLockExpirationMillis());
    }
@@ -59,12 +79,23 @@ public class JdbcLeaseLockTest extends ActiveMQTestBase {
    }
 
    @Before
-   public void createLockTable() {
+   public void createLockTable() throws Exception {
       dbConf = createDefaultDatabaseStorageConfiguration();
       sqlProvider = JDBCUtils.getSQLProvider(
          dbConf.getJdbcDriverClassName(),
          dbConf.getNodeManagerStoreTableName(),
          SQLProvider.DatabaseStoreType.NODE_MANAGER);
+
+      if (withExistingTable) {
+         TestJDBCDriver testDriver = TestJDBCDriver
+            .usingConnectionUrl(
+                dbConf.getJdbcConnectionUrl(),
+                dbConf.getJdbcDriverClassName(),
+                sqlProvider);
+         testDriver.start();
+         testDriver.stop();
+      }
+
       jdbcSharedStateManager = JdbcSharedStateManager
          .usingConnectionUrl(
             UUID.randomUUID().toString(),

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/af15fc43/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/impl/jdbc/TestJDBCDriver.java
----------------------------------------------------------------------
diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/impl/jdbc/TestJDBCDriver.java b/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/impl/jdbc/TestJDBCDriver.java
new file mode 100644
index 0000000..52b497a
--- /dev/null
+++ b/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/impl/jdbc/TestJDBCDriver.java
@@ -0,0 +1,50 @@
+/*
+ * 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.activemq.artemis.core.server.impl.jdbc;
+
+import java.sql.SQLException;
+
+import org.apache.activemq.artemis.jdbc.store.drivers.AbstractJDBCDriver;
+import org.apache.activemq.artemis.jdbc.store.sql.SQLProvider;
+
+public class TestJDBCDriver extends AbstractJDBCDriver {
+
+
+   public static TestJDBCDriver usingConnectionUrl(
+         String jdbcConnectionUrl,
+         String jdbcDriverClass,
+         SQLProvider provider) {
+      TestJDBCDriver driver = new TestJDBCDriver();
+      driver.setSqlProvider(provider);
+      driver.setJdbcConnectionUrl(jdbcConnectionUrl);
+      driver.setJdbcDriverClass(jdbcDriverClass);
+      return driver;
+   }
+
+   @Override
+   protected void prepareStatements() throws SQLException {
+   }
+
+   @Override
+   protected void createSchema() throws SQLException {
+      try {
+         connection.createStatement().execute(sqlProvider.createNodeManagerStoreTableSQL());
+      } catch (SQLException e) {
+      }
+   }
+
+}