import javax.sql.DataSource;\r
import java.sql.DatabaseMetaData;\r
import java.sql.Connection;\r
- import java.sql.ResultSet;\r
+import java.sql.ResultSetMetaData;\r
import java.sql.SQLException;\r
import java.sql.Statement;\r
import java.util.HashMap;\r
* $LastChangedDate: $\r
*/\r
public class JDBCTools {\r
- public static HashMap<String, DataSource> cachedDataSources = new HashMap<String, DataSource>();\r
+ public static HashMap<String, DataSource> cachedDataSources = new HashMap<String, DataSource>();\r
- public static String CSPACE_REPOSITORY_NAME = "CspaceDS";\r
- public static String NUXEO_REPOSITORY_NAME = "NuxeoDS";\r
+ public static String CSPACE_DATASOURCE_NAME = "CspaceDS";\r
+ public static String NUXEO_DATASOURCE_NAME = "NuxeoDS";\r
+ // Default database names\r
+ public static String DEFAULT_CSPACE_DATABASE_NAME = ConfigUtils.DEFAULT_CSPACE_DATABASE_NAME;\r
+ public static String DEFAULT_NUXEO_REPOSITORY_NAME = ConfigUtils.DEFAULT_NUXEO_REPOSITORY_NAME;\r
+ public static String DEFAULT_NUXEO_DATABASE_NAME = ConfigUtils.DEFAULT_NUXEO_DATABASE_NAME;\r
//\r
// Private constants\r
//\r
return result;\r
}\r
\r
- /*\r
- * This is a wrapper around DataSource's getConnectionMethod -mainly exists modularize all connection related code to JDBCTool class.\r
- */\r
- public static Connection getConnection(DataSource dataSource) throws SQLException {\r
- Connection result = null;\r
- result = dataSource.getConnection();\r
- return result;\r
- }\r
- \r
- public static Connection getConnection(String repositoryName) throws NamingException, SQLException {\r
+ public static Connection getConnection(String dataSourceName, String repositoryName) throws NamingException, SQLException {\r
Connection result = null;\r
\r
- if (Tools.isEmpty(repositoryName)) {\r
- repositoryName = getDefaultRepositoryName();\r
- if (logger.isWarnEnabled() == true) {\r
- logger.warn("getConnection() method was called with an empty or null repository name. Using " + repositoryName + " instead.");\r
- }\r
+ if (Tools.isEmpty(dataSourceName) || Tools.isEmpty(repositoryName)) {\r
+ String errMsg = String.format(\r
+ "The getConnection() method was called with an empty or null repository name = '%s' and/or data source name = '%s'.", \r
+ dataSourceName, repositoryName);\r
+ logger.error(errMsg);\r
+ throw new NamingException(errMsg);\r
}\r
- \r
- DataSource ds = getDataSource(repositoryName);\r
- Connection conn = getConnection(ds);\r
- result = conn;\r
- \r
+ \r
+ /*\r
+ * We synch this block as a workaround to not have separate DataSource instances for\r
+ * each Nuxeo repo/DB. Ideally, we should replace the need for this synch block by\r
+ * registering a separate DataSource for each repo/db at init/start-up time.\r
+ * \r
+ * We need to sync because we're changing the URL of the datasource inorder to get the correct\r
+ * connection. The synch prevents different threads from getting the incorrect connection -i.e., one pointing\r
+ * to the wrong URL.\r
+ */\r
+ Connection conn = null;\r
+ synchronized (JDBCTools.class) {\r
+ org.apache.tomcat.dbcp.dbcp.BasicDataSource dataSource = \r
+ (org.apache.tomcat.dbcp.dbcp.BasicDataSource)getDataSource(dataSourceName);\r
+ // Get the template URL value from the JNDI datasource and substitute the databaseName\r
+ String urlTemplate = dataSource.getUrl();\r
+ String databaseName = getDatabaseName(repositoryName);\r
+ String connectionUrl = urlTemplate.replace(URL_DATABASE_NAME, databaseName);\r
+ dataSource.setUrl(connectionUrl);\r
+ \r
+ try {\r
+ conn = dataSource.getConnection();\r
+ result = conn;\r
+ if (logger.isTraceEnabled() == true && conn != null) {\r
+ logger.trace(String.format("Connection made to repository = '%s' using datasource = '%s'", repositoryName, dataSourceName));\r
+ }\r
+ } finally {\r
+ dataSource.setUrl(urlTemplate); // Reset the data source URL value back to the template value\r
+ }\r
+ }\r
+ \r
return result;\r
}\r
+ \r
+ // Regarding the method below, we might instead identify whether we can\r
+ // return a CachedRowSet or equivalent.\r
+ // http://docs.oracle.com/javase/1.5.0/docs/api/javax/sql/rowset/CachedRowSet.html\r
+ // -- ADR 2012-12-06\r
\r
/* THIS IS BROKEN - If you close the statement, it closes the ResultSet!!!\r
- public static CachedRowSet executeQuery(String repoName, String sql) throws Exception {\r
+ public static ResultSet executeQuery(String repoName, String sql) throws Exception {\r
Connection conn = null;\r
Statement stmt = null;\r
try {\r