diff --git metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java index 574141c..6c27d8d 100644 --- metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java +++ metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java @@ -32,7 +32,6 @@ import java.util.Map; import java.util.TreeMap; -import javax.jdo.JDODataStoreException; import javax.jdo.PersistenceManager; import javax.jdo.Query; import javax.jdo.Transaction; @@ -41,7 +40,6 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.derby.iapi.error.StandardException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.conf.HiveConf.ConfVars; @@ -127,7 +125,12 @@ public MetaStoreDirectSql(PersistenceManager pm, Configuration conf) { convertMapNullsToEmptyStrings = HiveConf.getBoolVar(conf, ConfVars.METASTORE_ORM_RETRIEVE_MAPNULLS_AS_EMPTY_STRINGS); - this.isCompatibleDatastore = ensureDbInit() && runTestQuery(); + if (HiveConf.getBoolVar(conf, ConfVars.METASTORE_AUTO_CREATE_SCHEMA)) { + isCompatibleDatastore = ensureDbInit() && runTestQuery(); + } else { + isCompatibleDatastore = true; + } + if (isCompatibleDatastore) { LOG.info("Using direct SQL, underlying DB is " + dbType); } @@ -135,19 +138,17 @@ public MetaStoreDirectSql(PersistenceManager pm, Configuration conf) { private DB determineDbType() { DB dbType = DB.OTHER; - if (runDbCheck("SET @@session.sql_mode=ANSI_QUOTES", "MySql")) { + String productName = getProductName(); + if (productName != null && productName.toLowerCase().contains("mysql")) { dbType = DB.MYSQL; - } else if (runDbCheck("SELECT version FROM v$instance", "Oracle")) { + } else if (productName != null && productName.toLowerCase().contains("oracle")) { dbType = DB.ORACLE; - } else if (runDbCheck("SELECT @@version", "MSSQL")) { + } else if (productName != null && productName.toLowerCase().contains("microsoft sql server")) { dbType = DB.MSSQL; - } else { - // TODO: maybe we should use getProductName to identify all the DBs - String productName = getProductName(); - if (productName != null && productName.toLowerCase().contains("derby")) { - dbType = DB.DERBY; - } - } + } else if (productName != null && productName.toLowerCase().contains("derby")) { + dbType = DB.DERBY; + } else; + return dbType; } @@ -180,6 +181,9 @@ private boolean ensureDbInit() { private boolean runTestQuery() { Transaction tx = pm.currentTransaction(); + if (!tx.isActive()) { + tx.begin(); + } // Run a self-test query. If it doesn't work, we will self-disable. What a PITA... String selfTestQuery = "select \"DB_ID\" from \"DBS\""; try { @@ -224,20 +228,21 @@ private void executeNoResult(final String queryText) throws SQLException { } } - private boolean runDbCheck(String queryText, String name) { + public void setDbANSIMode() { + if (dbType != DB.MYSQL) + return; + Transaction tx = pm.currentTransaction(); if (!tx.isActive()) { tx.begin(); } try { - executeNoResult(queryText); - return true; + executeNoResult("SET @@session.sql_mode=ANSI_QUOTES"); } catch (Throwable t) { - LOG.debug(name + " check failed, assuming we are not on " + name + ": " + t.getMessage()); + LOG.debug("set db ansi mode failed, assuming we are on mysql" + ": " + t.getMessage()); tx.rollback(); tx = pm.currentTransaction(); tx.begin(); - return false; } } diff --git metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java index fcaffc7..1bf59d6 100644 --- metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java +++ metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java @@ -253,6 +253,9 @@ public void setConf(Configuration conf) { } } finally { pmfPropLock.unlock(); + if (isInitialized && directSql != null) { + directSql.setDbANSIMode(); + } } }