You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by sh...@apache.org on 2009/09/07 15:19:15 UTC

svn commit: r812123 - in /lucene/solr/trunk/contrib/dataimporthandler: CHANGES.txt src/main/java/org/apache/solr/handler/dataimport/DataImporter.java src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java

Author: shalin
Date: Mon Sep  7 13:19:14 2009
New Revision: 812123

URL: http://svn.apache.org/viewvc?rev=812123&view=rev
Log:
SOLR-1269 -- Better error messages from JdbcDataSource when JDBC Driver name or SQL is incorrect

Modified:
    lucene/solr/trunk/contrib/dataimporthandler/CHANGES.txt
    lucene/solr/trunk/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java
    lucene/solr/trunk/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java

Modified: lucene/solr/trunk/contrib/dataimporthandler/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/contrib/dataimporthandler/CHANGES.txt?rev=812123&r1=812122&r2=812123&view=diff
==============================================================================
--- lucene/solr/trunk/contrib/dataimporthandler/CHANGES.txt (original)
+++ lucene/solr/trunk/contrib/dataimporthandler/CHANGES.txt Mon Sep  7 13:19:14 2009
@@ -306,6 +306,9 @@
 
 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

Modified: lucene/solr/trunk/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java?rev=812123&r1=812122&r2=812123&view=diff
==============================================================================
--- lucene/solr/trunk/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java (original)
+++ lucene/solr/trunk/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java Mon Sep  7 13:19:14 2009
@@ -22,6 +22,8 @@
 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 @@
 
   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 @@
       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 @@
       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 @@
       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 @@
       }
       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;
   }

Modified: lucene/solr/trunk/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java?rev=812123&r1=812122&r2=812123&view=diff
==============================================================================
--- lucene/solr/trunk/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java (original)
+++ lucene/solr/trunk/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java Mon Sep  7 13:19:14 2009
@@ -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 @@
 
     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 @@
       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 @@
                 + (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 @@
           }
         } 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 @@
           return false;
         }
       } catch (SQLException e) {
-        logError("Error reading data ", e);
         close();
         wrapAndThrow(SEVERE,e);
         return false;
@@ -350,7 +345,6 @@
           resultSet.close();
         if (stmt != null)
           stmt.close();
-
       } catch (Exception e) {
         logError("Exception while closing result set", e);
       } finally {
@@ -365,7 +359,7 @@
     if (currTime - connLastUsed > CONN_TIME_OUT) {
       synchronized (this) {
         Connection tmpConn = factory.call();
-        close();
+        closeConnection();
         connLastUsed = System.currentTimeMillis();
         return conn = tmpConn;
       }
@@ -388,15 +382,23 @@
   }
 
   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