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/03/27 14:29:55 UTC

[1/2] activemq-artemis git commit: This closes #1822

Repository: activemq-artemis
Updated Branches:
  refs/heads/master f36bcb670 -> 5d3ba98a1


This closes #1822


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

Branch: refs/heads/master
Commit: 5d3ba98a111f4c248ecb518fd0cdd0e25acfad2f
Parents: f36bcb6 b95afbf
Author: Clebert Suconic <cl...@apache.org>
Authored: Tue Mar 27 10:12:14 2018 -0400
Committer: Clebert Suconic <cl...@apache.org>
Committed: Tue Mar 27 10:12:14 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(-)
----------------------------------------------------------------------



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

Posted by cl...@apache.org.
ARTEMIS-1653 Allow database tables to be created externally

In some environments it is not allowed to create a schema
by the application itself. With this change the AbstractJDBCDriver
now tests if an existing table is empty and executes further
statements in the same way as if the table does not exist.


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

Branch: refs/heads/master
Commit: b95afbf9363ad57a5c4a0a744220514d2d753908
Parents: f36bcb6
Author: Niels Lippke <nl...@gmx.de>
Authored: Sun Jan 28 17:53:35 2018 +0100
Committer: Clebert Suconic <cl...@apache.org>
Committed: Tue Mar 27 10:12:14 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/b95afbf9/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/b95afbf9/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 d4b63de..bcf560f 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/b95afbf9/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) {
+      }
+   }
+
+}