throw new Exception("Unrecognized database system " + databaseProductType);\r
}\r
if (sql != null && ! sql.trim().isEmpty()) {\r
- rows = JDBCTools.executeUpdate(sql);\r
+ // Assumes indicies will only be created at post-init time\r
+ // for the Nuxeo repository.\r
+ // \r
+ // To date, for the CSpace repository, indices have typically been\r
+ // created during the build process, via manual or generated\r
+ // DDL SQL scripts.\r
+ //\r
+ // If this assumption is no longer valid, we might instead\r
+ // identify the relevant repository from the table name here.\r
+ rows = JDBCTools.executeUpdate(sql, JDBCTools.getNuxeoRepositoryName());\r
logger.trace("Index added to column ("+columnName+") on table ("+tableName+")");\r
}\r
return rows;\r
// FIXME: May need to qualify table name by database/catalog,\r
// as table names likely will not be globally unique across same\r
// (although index names *may* be unique within scope).\r
- // - Pass in database name as parameter, retrieved via\r
- // getDatabaseName(field) in onRepositoryInitialized, above.\r
+ //\r
+ // If we do need to do this, we might:\r
+ // - Pass in the database name as a parameter to this method, retrieved\r
+ // via getDatabaseName(field) in onRepositoryInitialized, above.\r
// - Add 'IN databaseName' after tableName in MySQL variant, below.\r
// (PostgreSQL variant, below, uses a view that doesn't include\r
// a foreign key for associating a database/catalog to the index.)\r
}\r
\r
try {\r
- conn = JDBCTools.getConnection(JDBCTools.getDefaultRepositoryName());\r
+ // Assumes indicies will only be created at post-init time\r
+ // for the Nuxeo repository.\r
+ // \r
+ // To date, for the CSpace repository, indices have typically been\r
+ // created during the build process, via manual or generated\r
+ // DDL SQL scripts.\r
+ //\r
+ // If this assumption is no longer valid, we might instead\r
+ // identify the relevant repository from the table name here.\r
+ conn = JDBCTools.getConnection(JDBCTools.getNuxeoRepositoryName());\r
stmt = conn.createStatement();\r
rs = stmt.executeQuery(sql);\r
if (rs.last()) {\r
} else {
throw new Exception("Unrecognized database system.");
}
- rows = JDBCTools.executeUpdate(sql);
+ // Assumes field datatypes will only be modified at post-init
+ // time for the Nuxeo repository.
+ //
+ // To date, for the CSpace repository, field datatypes have
+ // typically been specified during the build process, via
+ // manual or generated DDL SQL scripts.
+ //
+ // If this assumption is no longer valid, we might instead
+ // identify the relevant repository from the table name here.
+ rows = JDBCTools.executeUpdate(sql, JDBCTools.getNuxeoRepositoryName());
}
} catch (Exception e) {
throw e;
}
try {
- conn = JDBCTools.getConnection(JDBCTools.getDefaultRepositoryName());
+ conn = JDBCTools.getConnection(JDBCTools.getNuxeoRepositoryName());
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
import javax.naming.NamingException;\r
import javax.security.auth.login.LoginException;\r
import javax.sql.DataSource;\r
+import java.sql.DatabaseMetaData;\r
import java.sql.Connection;\r
import java.sql.ResultSet;\r
import java.sql.SQLException;\r
final static Logger logger = LoggerFactory.getLogger(JDBCTools.class);\r
\r
public static Connection getConnection(String repositoryName) throws LoginException, SQLException {\r
- if (Tools.isEmpty(repositoryName)) {\r
+ if (Tools.isBlank(repositoryName)) {\r
repositoryName = getDefaultRepositoryName();\r
}\r
InitialContext ctx = null;\r
LoginException le = new LoginException("Error looking up DataSource from: " + repositoryName);\r
le.initCause(ex);\r
throw le;\r
+ } catch (SQLException e) {\r
+ throw e;\r
} finally {\r
if (ctx != null) {\r
try {\r
ctx.close();\r
} catch (Exception e) {\r
+ // Do nothing with exception in 'finally' clause.\r
}\r
}\r
}\r
}\r
-\r
+ \r
public static ResultSet executeQuery(String sql) throws Exception {\r
+ return executeQuery(sql, getDefaultRepositoryName());\r
+ }\r
+\r
+ public static ResultSet executeQuery(String sql, String repositoryName) throws Exception {\r
Connection conn = null;\r
Statement stmt = null;\r
try {\r
- conn = getConnection(getDefaultRepositoryName());\r
+ if (Tools.isBlank(repositoryName)) {\r
+ repositoryName = getDefaultRepositoryName();\r
+ }\r
+ conn = getConnection(repositoryName);\r
stmt = conn.createStatement();\r
ResultSet rs = stmt.executeQuery(sql);\r
stmt.close();\r
}\r
}\r
}\r
-\r
+ \r
public static int executeUpdate(String sql) throws Exception {\r
+ return executeUpdate(sql, getDefaultRepositoryName());\r
+ }\r
+\r
+ public static int executeUpdate(String sql, String repositoryName) throws Exception {\r
Connection conn = null;\r
Statement stmt = null;\r
try {\r
- conn = getConnection(getDefaultRepositoryName());\r
+ if (Tools.isBlank(repositoryName)) {\r
+ repositoryName = getDefaultRepositoryName();\r
+ }\r
+ conn = getConnection(repositoryName);\r
stmt = conn.createStatement();\r
int rows = stmt.executeUpdate(sql);\r
stmt.close();\r
}\r
}\r
\r
+ /**\r
+ * Returns the database product name, from the metadata for a\r
+ * JDBC connection to the default repository.\r
+ * \r
+ * Assumes that the database product name will be the same for the\r
+ * default repository and for all other repositories to which JDBC\r
+ * connections will be made, through the methods of this class.\r
+ * \r
+ * @return the database product name\r
+ */\r
public static String getDatabaseProductName() {\r
String productName = "";\r
Connection conn = null;\r
return productName;\r
}\r
\r
+ /**\r
+ * Returns an enumerated value uniquely identifying the database product type;\r
+ * e.g. MySQL, PostgreSQL, based on the database product name.\r
+ * \r
+ * @return an enumerated value identifying the database product type\r
+ * @throws Exception \r
+ */\r
public static DatabaseProductType getDatabaseProductType() throws Exception {\r
DatabaseProductType productType = DatabaseProductType.UNRECOGNIZED;\r
String productName = getDatabaseProductName();\r
public static String getDefaultRepositoryName() {\r
return ServiceMain.DEFAULT_REPOSITORY_NAME;\r
}\r
+ \r
+ public static String getNuxeoRepositoryName() {\r
+ return ServiceMain.NUXEO_REPOSITORY_NAME;\r
+ }\r
+\r
+ /**\r
+ * Prints metadata, such as database username and connection URL,\r
+ * for an open JDBC connection. This is a utility method for use\r
+ * during debugging.\r
+ * \r
+ * @param conn an open JDBC Connection\r
+ * @throws SQLException \r
+ */\r
+ private static void printConnectionMetaData(Connection conn) throws SQLException {\r
+ if (conn != null) {\r
+ DatabaseMetaData metadata = conn.getMetaData();\r
+ // FIXME: Outputs via System.out, rather than Logger, for\r
+ // cases where this may be called during server startup.\r
+ System.out.println("username=" + metadata.getUserName());\r
+ System.out.println("database url=" + metadata.getURL());\r
+ }\r
+ }\r
}\r