Apache Lucene open-source search software
SOLR-4920: JdbcDataSource incorrectly suppresses exceptions when retrieving a connection from a JNDI context and falls back to trying to use DriverManager to obtain a connection. Additionally, if a SQLException is thrown while initializing a connection, such as in setAutoCommit(), the connection will not be closed.
Shalin Shekhar Mangar14d7d11d59c3
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex fc5d408aa20..feb23fbbfd2 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -131,6 +131,11 @@ Bug Fixes* SOLR-4899: When reconnecting after ZooKeeper expiration, we need to be willing to waitforever, not just for 30 seconds. (Mark Miller)+* SOLR-4920: JdbcDataSource incorrectly suppresses exceptions when retrieving a connection from+ a JNDI context and falls back to trying to use DriverManager to obtain a connection. Additionally,+ if a SQLException is thrown while initializing a connection, such as in setAutoCommit(), the+ connection will not be closed. (Chris Eldredge via shalin)+Other Changes----------------------diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/JdbcDataSource.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/JdbcDataSource.javaindex ccaf3a67b6b..b3a0e9c7cd7 100644--- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/JdbcDataSource.java+++ b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/JdbcDataSource.java@@ -22,6 +22,8 @@ import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.naming.InitialContext;+import javax.naming.NamingException;+import java.sql.*;import java.util.*;import java.util.concurrent.Callable;@@ -132,66 +134,90 @@ public class JdbcDataSource extends+ url);long start = System.currentTimeMillis();Connection c = null;- try {- if(url != null){++ if (jndiName != null) {+ c = getFromJndi(initProps, jndiName);+ } else if (url != null) {+ try {c = DriverManager.getConnection(url, initProps);- } else if(jndiName != null){- InitialContext ctx = new InitialContext();- Object jndival = ctx.lookup(jndiName);- if (jndival instanceof javax.sql.DataSource) {- javax.sql.DataSource dataSource = (javax.sql.DataSource) jndival;- String user = (String) initProps.get("user");- String pass = (String) initProps.get("password");- if(user == null || user.trim().equals("")){- c = dataSource.getConnection();- } else {- c = dataSource.getConnection(user, pass);- }- } else {- throw new DataImportHandlerException(SEVERE,- "the jndi name : '"+jndiName +"' is not a valid javax.sql.DataSource");- }+ } catch (SQLException e) {+ // DriverManager does not allow you to use a driver which is not loaded through+ // the class loader of the class which is trying to make the connection.+ // This is a workaround for cases where the user puts the driver jar in the+ // solr.home/lib or solr.home/core/lib directories.+ Driver d = (Driver) DocBuilder.loadClass(driver, context.getSolrCore()).newInstance();+ c = d.connect(url, initProps);}- } catch (SQLException e) {- // DriverManager does not allow you to use a driver which is not loaded through- // the class loader of the class which is trying to make the connection.- // This is a workaround for cases where the user puts the driver jar in the- // solr.home/lib or solr.home/core/lib directories.- Driver d = (Driver) DocBuilder.loadClass(driver, context.getSolrCore()).newInstance();- c = d.connect(url, initProps);}if (c != null) {- if (Boolean.parseBoolean(initProps.getProperty("readOnly"))) {- c.setReadOnly(true);- // Add other sane defaults- c.setAutoCommit(true);- c.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);- c.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);- }- if (!Boolean.parseBoolean(initProps.getProperty("autoCommit"))) {- c.setAutoCommit(false);- }- String transactionIsolation = initProps.getProperty("transactionIsolation");- if ("TRANSACTION_READ_UNCOMMITTED".equals(transactionIsolation)) {- c.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);- } else if ("TRANSACTION_READ_COMMITTED".equals(transactionIsolation)) {- c.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);- } else if ("TRANSACTION_REPEATABLE_READ".equals(transactionIsolation)) {- c.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);- } else if ("TRANSACTION_SERIALIZABLE".equals(transactionIsolation)) {- c.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);- } else if ("TRANSACTION_NONE".equals(transactionIsolation)) {- c.setTransactionIsolation(Connection.TRANSACTION_NONE);- }- String holdability = initProps.getProperty("holdability");- if ("CLOSE_CURSORS_AT_COMMIT".equals(holdability)) {- c.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);- } else if ("HOLD_CURSORS_OVER_COMMIT".equals(holdability)) {- c.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);+ try {+ initializeConnection(c, initProps);+ } catch (SQLException e) {+ try {+ c.close();+ } catch (SQLException e2) {+ LOG.warn("Exception closing connection during cleanup", e2);+ }++ throw new DataImportHandlerException(SEVERE, "Exception initializing SQL connection", e);}}LOG.info("Time taken for getConnection(): "- + (System.currentTimeMillis() - start));+ + (System.currentTimeMillis() - start));+ return c;+ }++ private void initializeConnection(Connection c, final Properties initProps)+ throws SQLException {+ if (Boolean.parseBoolean(initProps.getProperty("readOnly"))) {+ c.setReadOnly(true);+ // Add other sane defaults+ c.setAutoCommit(true);+ c.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);+ c.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);+ }+ if (!Boolean.parseBoolean(initProps.getProperty("autoCommit"))) {+ c.setAutoCommit(false);+ }+ String transactionIsolation = initProps.getProperty("transactionIsolation");+ if ("TRANSACTION_READ_UNCOMMITTED".equals(transactionIsolation)) {+ c.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);+ } else if ("TRANSACTION_READ_COMMITTED".equals(transactionIsolation)) {+ c.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);+ } else if ("TRANSACTION_REPEATABLE_READ".equals(transactionIsolation)) {+ c.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);+ } else if ("TRANSACTION_SERIALIZABLE".equals(transactionIsolation)) {+ c.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);+ } else if ("TRANSACTION_NONE".equals(transactionIsolation)) {+ c.setTransactionIsolation(Connection.TRANSACTION_NONE);+ }+ String holdability = initProps.getProperty("holdability");+ if ("CLOSE_CURSORS_AT_COMMIT".equals(holdability)) {+ c.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);+ } else if ("HOLD_CURSORS_OVER_COMMIT".equals(holdability)) {+ c.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);+ }+ }++ private Connection getFromJndi(final Properties initProps, final String jndiName) throws NamingException,+ SQLException {++ Connection c = null;+ InitialContext ctx = new InitialContext();+ Object jndival = ctx.lookup(jndiName);+ if (jndival instanceof javax.sql.DataSource) {+ javax.sql.DataSource dataSource = (javax.sql.DataSource) jndival;+ String user = (String) initProps.get("user");+ String pass = (String) initProps.get("password");+ if(user == null || user.trim().equals("")){+ c = dataSource.getConnection();+ } else {+ c = dataSource.getConnection(user, pass);+ }+ } else {+ throw new DataImportHandlerException(SEVERE,+ "the jndi name : '"+jndiName +"' is not a valid javax.sql.DataSource");+ }return c;}};diff --git a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestJdbcDataSource.java b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestJdbcDataSource.javaindex eed883c9071..da3c0a5c8df 100644--- a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestJdbcDataSource.java+++ b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestJdbcDataSource.java@@ -19,6 +19,7 @@ package org.apache.solr.handler.dataimport;import java.sql.Connection;import java.sql.Driver;import java.sql.DriverManager;+import java.sql.SQLException;import java.util.*;import javax.sql.DataSource;@@ -126,6 +127,48 @@ public class TestJdbcDataSource extends AbstractDataImportHandlerTestCase {assertSame("connection", conn, connection);}+ @Test+ public void testRetrieveFromJndiFailureNotHidden() throws Exception {+ MockInitialContextFactory.bind("java:comp/env/jdbc/JndiDB", dataSource);++ props.put(JdbcDataSource.JNDI_NAME, "java:comp/env/jdbc/JndiDB");++ SQLException sqlException = new SQLException("fake");+ EasyMock.expect(dataSource.getConnection()).andThrow(sqlException);++ mockControl.replay();++ try {+ jdbcDataSource.createConnectionFactory(context, props).call();+ } catch (SQLException ex) {+ assertSame(sqlException, ex);+ }++ mockControl.verify();+ }++ @Test+ public void testClosesConnectionWhenExceptionThrownOnSetAutocommit() throws Exception {+ MockInitialContextFactory.bind("java:comp/env/jdbc/JndiDB", dataSource);++ props.put(JdbcDataSource.JNDI_NAME, "java:comp/env/jdbc/JndiDB");++ SQLException sqlException = new SQLException("fake");+ EasyMock.expect(dataSource.getConnection()).andReturn(connection);+ connection.setAutoCommit(false);+ EasyMock.expectLastCall().andThrow(sqlException);+ connection.close();+ mockControl.replay();++ try {+ jdbcDataSource.createConnectionFactory(context, props).call();+ } catch (DataImportHandlerException ex) {+ assertSame(sqlException, ex.getCause());+ }++ mockControl.verify();+ }+@Testpublic void testRetrieveFromDriverManager() throws Exception {DriverManager.registerDriver(driver);