From: Aron Roberts Date: Fri, 14 Dec 2012 20:51:20 +0000 (-0800) Subject: Merge remote-tracking branch 'upstream/master' into CSPACE-5727 X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=99a34384c81d96b40753dd17064e8c2cc5e95687;p=tmp%2Fjakarta-migration.git Merge remote-tracking branch 'upstream/master' into CSPACE-5727 Conflicts: services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java --- 99a34384c81d96b40753dd17064e8c2cc5e95687 diff --cc services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java index 4325afa11,0d40e4217..377f2777f --- a/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java +++ b/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java @@@ -28,9 -29,6 +29,7 @@@ import javax.naming.NamingException import javax.sql.DataSource; import java.sql.DatabaseMetaData; import java.sql.Connection; - import java.sql.ResultSet; +import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; @@@ -41,9 -39,13 +40,13 @@@ * $LastChangedDate: $ */ public class JDBCTools { - public static HashMap cachedDataSources = new HashMap(); + public static HashMap cachedDataSources = new HashMap(); - public static String CSPACE_REPOSITORY_NAME = "CspaceDS"; - public static String NUXEO_REPOSITORY_NAME = "NuxeoDS"; + public static String CSPACE_DATASOURCE_NAME = "CspaceDS"; + public static String NUXEO_DATASOURCE_NAME = "NuxeoDS"; + // Default database names + public static String DEFAULT_CSPACE_DATABASE_NAME = ConfigUtils.DEFAULT_CSPACE_DATABASE_NAME; + public static String DEFAULT_NUXEO_REPOSITORY_NAME = ConfigUtils.DEFAULT_NUXEO_REPOSITORY_NAME; + public static String DEFAULT_NUXEO_DATABASE_NAME = ConfigUtils.DEFAULT_NUXEO_DATABASE_NAME; // // Private constants // @@@ -101,39 -106,52 +107,57 @@@ return result; } - /* - * This is a wrapper around DataSource's getConnectionMethod -mainly exists modularize all connection related code to JDBCTool class. - */ - public static Connection getConnection(DataSource dataSource) throws SQLException { - Connection result = null; - result = dataSource.getConnection(); - return result; - } - - public static Connection getConnection(String repositoryName) throws NamingException, SQLException { + public static Connection getConnection(String dataSourceName, String repositoryName) throws NamingException, SQLException { Connection result = null; - if (Tools.isEmpty(repositoryName)) { - repositoryName = getDefaultRepositoryName(); - if (logger.isWarnEnabled() == true) { - logger.warn("getConnection() method was called with an empty or null repository name. Using " + repositoryName + " instead."); - } + if (Tools.isEmpty(dataSourceName) || Tools.isEmpty(repositoryName)) { + String errMsg = String.format( + "The getConnection() method was called with an empty or null repository name = '%s' and/or data source name = '%s'.", + dataSourceName, repositoryName); + logger.error(errMsg); + throw new NamingException(errMsg); } - - DataSource ds = getDataSource(repositoryName); - Connection conn = getConnection(ds); - result = conn; - + + /* + * We synch this block as a workaround to not have separate DataSource instances for + * each Nuxeo repo/DB. Ideally, we should replace the need for this synch block by + * registering a separate DataSource for each repo/db at init/start-up time. + * + * We need to sync because we're changing the URL of the datasource inorder to get the correct + * connection. The synch prevents different threads from getting the incorrect connection -i.e., one pointing + * to the wrong URL. + */ + Connection conn = null; + synchronized (JDBCTools.class) { + org.apache.tomcat.dbcp.dbcp.BasicDataSource dataSource = + (org.apache.tomcat.dbcp.dbcp.BasicDataSource)getDataSource(dataSourceName); + // Get the template URL value from the JNDI datasource and substitute the databaseName + String urlTemplate = dataSource.getUrl(); + String databaseName = getDatabaseName(repositoryName); + String connectionUrl = urlTemplate.replace(URL_DATABASE_NAME, databaseName); + dataSource.setUrl(connectionUrl); + + try { + conn = dataSource.getConnection(); + result = conn; + if (logger.isTraceEnabled() == true && conn != null) { + logger.trace(String.format("Connection made to repository = '%s' using datasource = '%s'", repositoryName, dataSourceName)); + } + } finally { + dataSource.setUrl(urlTemplate); // Reset the data source URL value back to the template value + } + } + return result; } + + // Regarding the method below, we might instead identify whether we can + // return a CachedRowSet or equivalent. + // http://docs.oracle.com/javase/1.5.0/docs/api/javax/sql/rowset/CachedRowSet.html + // -- ADR 2012-12-06 /* THIS IS BROKEN - If you close the statement, it closes the ResultSet!!! - public static CachedRowSet executeQuery(String repoName, String sql) throws Exception { + public static ResultSet executeQuery(String repoName, String sql) throws Exception { Connection conn = null; Statement stmt = null; try {