Exemplo n.º 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) {
         }
     }
   }
 }
Exemplo n.º 2
0
 public void run() {
   Connection con = null;
   Statement stmt = null;
   try {
     DataSource ds = getDataSource();
     con = ds.getConnection();
     if (executeOnlyIf(con, onlyIfQuery)) {
       stmt = con.createStatement();
       if (query != null) {
         executeQuery(stmt, query);
       } else if (update != null) {
         executeUpdate(stmt, update);
       } else {
         throw new IllegalStateException("Both query and update properties are unset");
       }
     } else {
       LOG.debug("Skipped because of " + onlyIfQuery);
     }
   } catch (RuntimeException e) {
     throw e;
   } catch (Throwable t) {
     if (ignore.matcher(t.getMessage()).matches()) {
       LOG.info("Ignoring " + t.getMessage());
     } else {
       throw new RuntimeException(t.getMessage(), t);
     }
   } finally {
     try {
       if (stmt != null) {
         stmt.close();
       }
     } catch (Exception g) {
     }
     try {
       if (con != null) {
         con.close();
       }
     } catch (Exception g) {
     }
   }
 }
Exemplo n.º 3
0
 protected boolean executeOnlyIf(Connection con, String q) throws SQLException {
   if (q == null) return true;
   Statement stmt = null;
   try {
     stmt = con.createStatement();
     q = q.replace("$PREFIX", getPrefix());
     LOG.debug(" Executing query " + q);
     ResultSet rs = stmt.executeQuery(q);
     rs.next();
     boolean res = rs.getBoolean(1);
     LOG.debug("Result: " + res);
     return res;
   } catch (SQLException sqe) {
     LOG.error(sqe.getMessage() + " from " + q);
     throw sqe;
   } finally {
     try {
       if (stmt != null) {
         stmt.close();
       }
     } catch (Exception g) {
     }
   }
 }
Exemplo n.º 4
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);
    }
  }