diff --git metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java index 574141c..5ebb2bf 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; } @@ -165,6 +166,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,9 +185,14 @@ 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 { + doDbSpecificInitializationsBeforeQuery(); pm.newQuery("javax.jdo.query.SQL", selfTestQuery).execute(); tx.commit(); return true; @@ -224,23 +234,6 @@ private void executeNoResult(final String queryText) throws SQLException { } } - private boolean runDbCheck(String queryText, String name) { - Transaction tx = pm.currentTransaction(); - if (!tx.isActive()) { - tx.begin(); - } - try { - executeNoResult(queryText); - return true; - } catch (Throwable t) { - LOG.debug(name + " check failed, assuming we are not on " + name + ": " + t.getMessage()); - tx.rollback(); - tx = pm.currentTransaction(); - tx.begin(); - return false; - } - } - public Database getDatabase(String dbName) throws MetaException{ Query queryDbSelector = null; Query queryDbParams = null;