Source:
void createCatalog(String catName, String location, String description, boolean ifNotExists) throws HiveMetaException { catName = normalizeIdentifier(catName); System.out.println("Create catalog " + catName + " at location " + location); try (Connection conn = getConnectionToMetastore(false)) { try (Statement stmt = conn.createStatement()) { if (ifNotExists) { String query = "select " + quoteIf("NAME") + " from " + quoteIf("CTLGS") + " where " + quoteIf("NAME") + " = '" + catName + "'"; ResultSet rs = stmt.executeQuery(query); if (rs.next()) { System.out.println("Catalog " + catName + " already exists"); return; } } SQLGenerator sqlGenerator = new SQLGenerator( DatabaseProduct.determineDatabaseProduct( conn.getMetaData().getDatabaseProductName() ), hiveConf); String query = sqlGenerator.addForUpdateClause("select max(" + quoteIf("CTLG_ID") + ") " + "from " + quoteIf("CTLGS")); ResultSet rs = stmt.executeQuery(query); if (!rs.next()) { throw new HiveMetaException("No catalogs found, have you upgraded the database?"); } int catNum = rs.getInt(1) + 1; String update = "insert into " + quoteIf("CTLGS") + "(" + quoteIf("CTLG_ID") + ", " + quoteIf("NAME") + ", " + quoteAlways("DESC") + ", " + quoteIf( "LOCATION_URI") + ") " + " values (" + catNum + ", '" + catName + "', '" + description + "', '" + location + "')"; stmt.execute(update); conn.commit(); } } catch (MetaException|SQLException e) { throw new HiveMetaException("Failed to add catalog", e); } }

Target:
void createCatalog(String catName, String location, String description, boolean ifNotExists) throws HiveMetaException { catName = normalizeIdentifier(catName); System.out.println("Create catalog " + catName + " at location " + location); Connection conn = getConnectionToMetastore(true); boolean success = false; try { conn.setAutoCommit(false); try (Statement stmt = conn.createStatement()) { if (ifNotExists) { String query = "select " + quoteIf("NAME") + " from " + quoteIf("CTLGS") + " where " + quoteIf("NAME") + " = '" + catName + "'"; LOG.debug("Going to run " + query); ResultSet rs = stmt.executeQuery(query); if (rs.next()) { System.out.println("Catalog " + catName + " already exists"); return; } } SQLGenerator sqlGenerator = new SQLGenerator( DatabaseProduct.determineDatabaseProduct( conn.getMetaData().getDatabaseProductName() ), hiveConf); String query = sqlGenerator.addForUpdateClause("select max(" + quoteIf("CTLG_ID") + ") " + "from " + quoteIf("CTLGS")); LOG.debug("Going to run " + query); ResultSet rs = stmt.executeQuery(query); if (!rs.next()) { throw new HiveMetaException("No catalogs found, have you upgraded the database?"); } int catNum = rs.getInt(1) + 1; String update = "insert into " + quoteIf("CTLGS") + "(" + quoteIf("CTLG_ID") + ", " + quoteIf("NAME") + ", " + quoteAlways("DESC") + ", " + quoteIf( "LOCATION_URI") + ") " + " values (" + catNum + ", '" + catName + "', '" + description + "', '" + location + "')"; LOG.debug("Going to run " + update); stmt.execute(update); conn.commit(); success = true; } } catch (MetaException|SQLException e) { throw new HiveMetaException("Failed to add catalog", e); } finally { try { if (!success) conn.rollback(); } catch (SQLException e) { LOG.error("Failed to rollback, everything will probably go bad from here."); } } }