diff --git metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java index 574141c..276854a 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,18 @@ 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")) { + setDbANSIMode(); + } 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; } @@ -165,6 +167,10 @@ private String getProductName() { private boolean ensureDbInit() { Transaction tx = pm.currentTransaction(); + if (!tx.isActive()) { + tx.begin(); + } + try { // Force the underlying db to initialize. pm.newQuery(MDatabase.class, "name == ''").execute(); @@ -180,6 +186,10 @@ 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 +234,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; } }