コード例 #1
0
 protected void check() {
   if (map == null) {
     Connection connection = null;
     Statement statement = null;
     ResultSet results = null;
     try {
       connection = dataSource.getConnection();
       statement = connection.createStatement();
       long start = System.currentTimeMillis();
       String s = getFindSql(identifier);
       if (log.isTraceEnabled()) {
         log.trace("About to execute " + s + " because ", new Exception());
       }
       results = statement.executeQuery(s);
       ResultSetMetaData meta = results.getMetaData();
       map = new HashMap<String, String>();
       if (results.next()) {
         for (int i = 1; i <= meta.getColumnCount(); i++) {
           String value = org.mmbase.util.Casting.toString(results.getString(i));
           map.put(meta.getColumnName(i).toLowerCase(), value);
         }
       }
       long duration = (System.currentTimeMillis() - start);
       if (duration > 500) {
         log.warn("Executed " + s + " in " + duration + " ms");
       } else if (duration > 100) {
         log.debug("Executed " + s + " in " + duration + " ms");
       } else {
         log.trace("Executed " + s + " in " + duration + " ms");
       }
     } catch (Exception e) {
       throw new RuntimeException(e.getMessage(), e);
     } finally {
       if (results != null)
         try {
           results.close();
         } catch (Exception e) {
         }
       if (statement != null)
         try {
           statement.close();
         } catch (Exception e) {
         }
       if (connection != null)
         try {
           connection.close();
         } catch (Exception e) {
         }
     }
   }
 }
コード例 #2
0
 @Override
 public void index(Document document) {
   if (log.isTraceEnabled()) {
     log.trace(
         "Indexing "
             + sql
             + " id="
             + JdbcIndexDefinition.this.identifier
             + ", key = "
             + JdbcIndexDefinition.this.key);
   }
   String id = getIdentifier();
   if (id != null) {
     document.add(
         new Field(
             "builder",
             "VIRTUAL BUILDER",
             Field.Store.YES,
             Field.Index.NOT_ANALYZED)); // keyword
     document.add(
         new Field(
             "number", getIdentifier(), Field.Store.YES, Field.Index.NOT_ANALYZED)); // keyword
   }
   try {
     for (int i = 1; i <= meta.getColumnCount(); i++) {
       String value = org.mmbase.util.Casting.toString(results.getString(i));
       if (log.isTraceEnabled()) {
         log.trace(
             "Indexing " + value + " for " + meta.getColumnName(i) + " on " + getIdentifier());
       }
       String fieldName = meta.getColumnName(i);
       if (keyWords.contains(fieldName)) {
         Indexer.addField(
             document,
             new Field(fieldName, value, Field.Store.YES, Field.Index.NOT_ANALYZED),
             nonDefaultMultiples.get(fieldName)); // keyword
       } else {
         Field field = new Field(fieldName, value, Field.Store.YES, Field.Index.ANALYZED);
         Float boost = boosts.get(fieldName);
         if (boost != null) {
           field.setBoost(boost);
         }
         Indexer.addField(document, field, nonDefaultMultiples.get(fieldName));
         Field fullText = new Field("fulltext", value, Field.Store.YES, Field.Index.ANALYZED);
         if (boost != null) {
           fullText.setBoost(boost);
         }
         document.add(fullText);
       }
     }
   } catch (SQLException sqe) {
     log.error(sqe.getMessage(), sqe);
   }
 }
コード例 #3
0
 @Override
 public String getKey() {
   if (JdbcIndexDefinition.this.key != null && !JdbcIndexDefinition.this.key.equals("")) {
     try {
       return results.getString(JdbcIndexDefinition.this.key);
     } catch (SQLException sqe) {
       log.error(sqe.getMessage(), sqe);
       return "";
     }
   } else {
     return null;
   }
 }
コード例 #4
0
 @Override
 public String getIdentifier() {
   if (JdbcIndexDefinition.this.identifier != null
       && !JdbcIndexDefinition.this.identifier.equals("")) {
     try {
       return results.getString(JdbcIndexDefinition.this.identifier);
     } catch (SQLException sqe) {
       log.error(meta + " " + sqe.getMessage(), sqe);
       return "";
     }
   } else {
     return null;
   }
 }
コード例 #5
0
ファイル: DBRecordKey.java プロジェクト: neeph/OpenGTSFull
  /**
   * ** Returns true if the specified key attribute exists in the table ** @param altIndexName The
   * alternate index name, or null to use the primary index ** @param whereKeyType The partial key
   * match type ** @return True if the specified key attribute exists in the table, false otherwise
   */
  protected boolean _exists(String altIndexName, int whereKeyType)
      throws SQLException, DBException {

    /* key fields */
    boolean usePrimaryKey = StringTools.isBlank(altIndexName);
    DBField kfld[] = usePrimaryKey ? this.getKeyFields() : this.getAltKeyFields(altIndexName);
    if (ListTools.isEmpty(kfld)) {
      throw new DBException("No keys found!");
    }

    /* check last key for "auto_increment" */
    if (whereKeyType == DBWhere.KEY_FULL) {
      DBField lastField = kfld[kfld.length - 1];
      if (lastField.isAutoIncrement()
          && !this.getFieldValues().hasFieldValue(lastField.getName())) {
        // full key requested and last key is auto_increment, which is missing
        return false;
      }
    }

    // DBSelect: SELECT <Keys> FROM <TableName> <KeyWhere>
    String firstKey = kfld[0].getName();
    DBSelect<gDBR> dsel = new DBSelect<gDBR>(this.getFactory());
    dsel.setSelectedFields(firstKey);
    dsel.setWhere(this._getWhereClause(altIndexName, whereKeyType));

    /* get keyed record */
    DBConnection dbc = null;
    Statement stmt = null;
    ResultSet rs = null;
    boolean exists = false;
    try {
      dbc = DBConnection.getDefaultConnection();
      stmt = dbc.execute(dsel.toString()); // may throw DBException
      rs = stmt.getResultSet();
      exists = rs.next();
    } catch (SQLException sqe) {
      if (sqe.getErrorCode() == DBFactory.SQLERR_TABLE_NOTLOCKED) {
        // MySQL: This case has been seen on rare occasions.  Not sure what causes it.
        Print.logError("SQL Lock Error: " + sqe);
        Print.logError("Hackery! Forcing lock on table: " + this.getTableName());
        if (DBProvider.lockTableForRead(this.getTableName(), true)) { // may throw DBException
          stmt = dbc.execute(dsel.toString()); // may throw SQLException, DBException
          rs = stmt.getResultSet(); // SQLException
          exists = rs.next(); // SQLException
          DBProvider.unlockTables(); // DBException
        }
      } else {
        throw sqe;
      }
    } finally {
      if (rs != null) {
        try {
          rs.close();
        } catch (Throwable t) {
        }
      }
      if (stmt != null) {
        try {
          stmt.close();
        } catch (Throwable t) {
        }
      }
      DBConnection.release(dbc);
    }

    return exists;
  }
コード例 #6
0
  CloseableIterator<JdbcEntry> getSqlCursor(final String sql) {
    try {
      long start = System.currentTimeMillis();
      final Connection con = getDirectConnection();
      log.debug("About to execute " + sql + " (" + directConnections + ")");
      final Statement statement = con.createStatement();
      final ResultSet results = statement.executeQuery(sql);
      if (log.isDebugEnabled()) {
        log.debug("Executed " + sql + " in " + (System.currentTimeMillis() - start) + " ms");
      }
      final ResultSetMetaData meta = results.getMetaData();

      return new CloseableIterator<JdbcEntry>() {
        boolean hasNext = results.isBeforeFirst();
        int i = 0;

        @Override
        public boolean hasNext() {
          return hasNext;
        }

        @Override
        public JdbcEntry next() {
          if (!hasNext) {
            throw new NoSuchElementException();
          }
          try {
            results.next();
            hasNext = !results.isLast();
          } catch (java.sql.SQLException sqe) {
            log.error(sqe);
            hasNext = false;
          }
          JdbcEntry entry = new JdbcEntry(meta, results, sql);
          i++;
          if (log.isServiceEnabled()) {
            if (i % 100 == 0) {
              log.service("jdbc cursor " + i + " (now at id=" + entry.getIdentifier() + ")");
            } else if (log.isDebugEnabled()) {
              log.trace("jdbc cursor " + i + " (now at id=" + entry.getIdentifier() + ")");
            }
          }
          return entry;
        }

        @Override
        public void remove() {
          throw new UnsupportedOperationException();
        }

        @Override
        public void close() {
          log.debug("Closing " + con);
          try {
            if (results != null) results.close();
            if (statement != null) statement.close();
            if (con != null) con.close();
          } catch (Exception e) {
            log.error(e);
          }
        }
      };
    } catch (Exception e) {
      throw new RuntimeException(e.getMessage(), e);
    }
  }