Skip to content

Apache Lucene open-source search software

SOLR-1269 -- Better error messages from JdbcDataSource when JDBC Driver name or SQL is incorrect

Shalin Shekhar Mangar500bf0cdb0ef
diff --git a/contrib/dataimporthandler/CHANGES.txt b/contrib/dataimporthandler/CHANGES.txt
index f83c4eba041..3c6bc653775 100644
--- a/contrib/dataimporthandler/CHANGES.txt
+++ b/contrib/dataimporthandler/CHANGES.txt
@@ -306,6 +306,9 @@ Other
10.SOLR-1265: Add variable resolving for URLDataSource properties like baseUrl. (Chris Eldredge via ehatcher)
+11.SOLR-1269: Better error messages from JdbcDataSource when JDBC Driver name or SQL is incorrect.
+ (ehatcher, shalin)
+
================== Release 1.3.0 20080915 ==================
Status
diff --git a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java
index 552e1dbcede..63743d2a2fe 100644
--- a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java
+++ b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java
@@ -22,6 +22,8 @@ import org.apache.solr.core.SolrCore;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.common.util.ContentStream;
+import static org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow;
+import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
@@ -88,7 +90,7 @@ public class DataImporter {
DataImporter(String dataConfig, SolrCore core, Map<String, Properties> ds, Map<String, Object> session) {
if (dataConfig == null)
- throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
+ throw new DataImportHandlerException(SEVERE,
"Configuration not found");
this.core = core;
this.schema = core.getSchema();
@@ -173,13 +175,13 @@ public class DataImporter {
config = new DataConfig();
NodeList elems = document.getElementsByTagName("dataConfig");
if(elems == null || elems.getLength() == 0) {
- throw new DataImportHandlerException(DataImportHandlerException.SEVERE, "the root node '<dataConfig>' is missing");
+ throw new DataImportHandlerException(SEVERE, "the root node '<dataConfig>' is missing");
}
config.readFromXml((Element) elems.item(0));
LOG.info("Data Configuration loaded successfully");
} catch (Exception e) {
SolrConfig.severeErrors.add(e);
- throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
+ throw new DataImportHandlerException(SEVERE,
"Exception occurred while initializing context", e);
}
}
@@ -275,8 +277,8 @@ public class DataImporter {
p = dataSourceProps.get(null);// for default data source
if (p == null)
p = config.dataSources.get(null);
- if (p == null)
- throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
+ if (p == null)
+ throw new DataImportHandlerException(SEVERE,
"No dataSource :" + name + " available for entity :"
+ key.name);
String impl = p.getProperty(TYPE);
@@ -287,8 +289,7 @@ public class DataImporter {
try {
dataSrc = (DataSource) DocBuilder.loadClass(impl, getCore()).newInstance();
} catch (Exception e) {
- throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
- "Invalid type for data source: " + impl, e);
+ wrapAndThrow(SEVERE, e, "Invalid type for data source: " + impl);
}
}
try {
@@ -304,8 +305,7 @@ public class DataImporter {
}
dataSrc.init(ctx, copyProps);
} catch (Exception e) {
- throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
- "Failed to initialize DataSource: " + key.dataSource, e);
+ wrapAndThrow(SEVERE, e, "Failed to initialize DataSource: " + key.dataSource);
}
return dataSrc;
}
diff --git a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java
index fafdbdf21c9..0b749acd185 100644
--- a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java
+++ b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java
@@ -16,7 +16,6 @@
*/
package org.apache.solr.handler.dataimport;
-import org.apache.solr.common.SolrException;
import static org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow;
import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE;
import org.slf4j.Logger;
@@ -64,7 +63,7 @@ public class JdbcDataSource extends
String bsz = initProps.getProperty("batchSize");
if (bsz != null) {
- bsz = (String) context.getVariableResolver().replaceTokens(bsz);
+ bsz = context.getVariableResolver().replaceTokens(bsz);
try {
batchSize = Integer.parseInt(bsz);
if (batchSize == -1)
@@ -112,11 +111,11 @@ public class JdbcDataSource extends
try {
DocBuilder.loadClass(driver, context.getSolrCore());
} catch (ClassNotFoundException e) {
- throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Could not load driver: " + driver, e);
+ wrapAndThrow(SEVERE, e, "Could not load driver: " + driver);
}
} else {
if(jndiName == null){
- throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Driver must be specified");
+ throw new DataImportHandlerException(SEVERE, "One of driver or jndiName must be specified in the data source");
}
}
@@ -249,8 +248,7 @@ public class JdbcDataSource extends
+ (System.currentTimeMillis() - start));
colNames = readFieldNames(resultSet.getMetaData());
} catch (Exception e) {
- throw new DataImportHandlerException(SEVERE,
- "Unable to execute query: " + query, e);
+ wrapAndThrow(SEVERE, e, "Unable to execute query: " + query);
}
if (resultSet == null) {
rSetIterator = new ArrayList<Map<String, Object>>().iterator();
@@ -318,9 +316,7 @@ public class JdbcDataSource extends
}
} catch (SQLException e) {
logError("Error reading data ", e);
- throw new DataImportHandlerException(
- DataImportHandlerException.SEVERE,
- "Error reading data from database", e);
+ wrapAndThrow(SEVERE, e, "Error reading data from database");
}
}
return result;
@@ -337,7 +333,6 @@ public class JdbcDataSource extends
return false;
}
} catch (SQLException e) {
- logError("Error reading data ", e);
close();
wrapAndThrow(SEVERE,e);
return false;
@@ -350,7 +345,6 @@ public class JdbcDataSource extends
resultSet.close();
if (stmt != null)
stmt.close();
-
} catch (Exception e) {
logError("Exception while closing result set", e);
} finally {
@@ -365,7 +359,7 @@ public class JdbcDataSource extends
if (currTime - connLastUsed > CONN_TIME_OUT) {
synchronized (this) {
Connection tmpConn = factory.call();
- close();
+ closeConnection();
connLastUsed = System.currentTimeMillis();
return conn = tmpConn;
}
@@ -388,15 +382,23 @@ public class JdbcDataSource extends
}
private boolean isClosed = false;
+
public void close() {
try {
- conn.close();
- } catch (Exception e) {
- LOG.error("Ignoring Error when closing connection", e);
- } finally{
+ closeConnection();
+ } finally {
isClosed = true;
}
+ }
+ private void closeConnection() {
+ try {
+ if (conn != null) {
+ conn.close();
+ }
+ } catch (Exception e) {
+ LOG.error("Ignoring Error when closing connection", e);
+ }
}
private static final long CONN_TIME_OUT = 10 * 1000; // 10 seconds