Example #1
0
  public boolean isValidConnection(DruidDataSource dataSource, Connection conn) {
    Statement stmt = null;
    ResultSet rs = null;
    try {
      stmt = conn.createStatement();
      stmt.setQueryTimeout(queryTimeoutSeconds);
      rs = stmt.executeQuery(getValidateSql());
      if (!rs.next()) {
        return false;
      }

      String status = rs.getString(1);

      if ("on".equalsIgnoreCase(status)) {
        return true;
      } else {
        return false;
      }
    } catch (Exception ex) {
      LOG.error("check datasource valid errror", ex);
      return false;
    } finally {
      JdbcUtils.close(rs);
      JdbcUtils.close(stmt);
    }
  }
Example #2
0
  public static List<Map<String, Object>> executeQuery(
      Connection conn, String sql, List<Object> parameters) throws SQLException {
    List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();

    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
      stmt = conn.prepareStatement(sql);

      setParameters(stmt, parameters);

      rs = stmt.executeQuery();

      ResultSetMetaData rsMeta = rs.getMetaData();

      while (rs.next()) {
        Map<String, Object> row = new LinkedHashMap<String, Object>();

        for (int i = 0, size = rsMeta.getColumnCount(); i < size; ++i) {
          String columName = rsMeta.getColumnLabel(i + 1);
          Object value = rs.getObject(i + 1);
          row.put(columName, value);
        }

        rows.add(row);
      }
    } finally {
      JdbcUtils.close(rs);
      JdbcUtils.close(stmt);
    }

    return rows;
  }
  public boolean isValidConnection(
      Connection conn, String valiateQuery, int validationQueryTimeout) {
    try {
      if (conn.isClosed()) {
        return false;
      }
    } catch (SQLException ex) {
      // skip
      return false;
    }

    if (valiateQuery == null) {
      return true;
    }

    try {
      if (conn instanceof DruidPooledConnection) {
        conn = ((DruidPooledConnection) conn).getConnection();
      }

      if (conn instanceof ConnectionProxy) {
        conn = ((ConnectionProxy) conn).getRawObject();
      }

      // unwrap
      if (clazz != null && clazz.isAssignableFrom(conn.getClass())) {
        Integer status = (Integer) ping.invoke(conn, params);

        // Error
        if (status.intValue() < 0) {
          return false;
        }

        return true;
      }

      Statement stmt = null;
      ResultSet rs = null;
      try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery(valiateQuery);
        return true;
      } catch (SQLException e) {
        return false;
      } catch (Exception e) {
        LOG.warn("Unexpected error in ping", e);
        return false;
      } finally {
        JdbcUtils.close(rs);
        JdbcUtils.close(stmt);
      }
    } catch (Exception e) {
      LOG.warn("Unexpected error in pingDatabase", e);
    }

    // OK
    return true;
  }
Example #4
0
  static {
    try {
      ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
      if (ctxClassLoader != null) {
        for (Enumeration<URL> e = ctxClassLoader.getResources("META-INF/druid-driver.properties");
            e.hasMoreElements(); ) {
          URL url = e.nextElement();

          Properties property = new Properties();

          InputStream is = null;
          try {
            is = url.openStream();
            property.load(is);
          } finally {
            JdbcUtils.close(is);
          }

          DRIVER_URL_MAPPING.putAll(property);
        }
      }
    } catch (Exception e) {
      LOG.error("load druid-driver.properties error", e);
    }
  }
  public void reset() throws SQLException {
    // reset default settings
    if (underlyingReadOnly != defaultReadOnly) {
      conn.setReadOnly(defaultReadOnly);
      underlyingReadOnly = defaultReadOnly;
    }

    if (underlyingHoldability != defaultHoldability) {
      conn.setHoldability(defaultHoldability);
      underlyingHoldability = defaultHoldability;
    }

    if (underlyingTransactionIsolation != defaultTransactionIsolation) {
      conn.setTransactionIsolation(defaultTransactionIsolation);
      underlyingTransactionIsolation = defaultTransactionIsolation;
    }

    if (underlyingAutoCommit != defaultAutoCommit) {
      conn.setAutoCommit(defaultAutoCommit);
      underlyingAutoCommit = defaultAutoCommit;
    }

    connectionEventListeners.clear();
    statementEventListeners.clear();

    for (Object item : statementTrace.toArray()) {
      Statement stmt = (Statement) item;
      JdbcUtils.close(stmt);
    }
    statementTrace.clear();

    conn.clearWarnings();
  }
Example #6
0
  public void exec_test(String resource) throws Exception {
    //        System.out.println(resource);
    InputStream is = null;

    is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
    Reader reader = new InputStreamReader(is, "UTF-8");
    String input = Utils.read(reader);
    JdbcUtils.close(reader);
    String[] items = input.split("---------------------------");
    String sql = items[0].trim();
    String expect = items[1].trim();

    OdpsStatementParser parser = new OdpsStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);

    Assert.assertEquals(1, statementList.size());

    SchemaStatVisitor visitor = new OdpsSchemaStatVisitor();
    stmt.accept(visitor);

    //        System.out.println(sql);
    //        System.out.println("Tables : " + visitor.getTables());
    //        System.out.println("fields : " + visitor.getColumns());
    //
    //        System.out.println();
    //        System.out.println("---------------------------");
    //        System.out.println(SQLUtils.toOdpsString(stmt));
  }
Example #7
0
 public static void insertToTable(
     DataSource dataSource, String tableName, Map<String, Object> data) throws SQLException {
   Connection conn = null;
   try {
     conn = dataSource.getConnection();
     insertToTable(conn, tableName, data);
   } finally {
     close(conn);
   }
 }
Example #8
0
 public static List<Map<String, Object>> executeQuery(
     DataSource dataSource, String sql, List<Object> parameters) throws SQLException {
   Connection conn = null;
   try {
     conn = dataSource.getConnection();
     return executeQuery(conn, sql, parameters);
   } finally {
     close(conn);
   }
 }
Example #9
0
 public static void execute(DataSource dataSource, String sql, List<Object> parameters)
     throws SQLException {
   Connection conn = null;
   try {
     conn = dataSource.getConnection();
     execute(conn, sql, parameters);
   } finally {
     close(conn);
   }
 }
Example #10
0
  public static void execute(Connection conn, String sql, List<Object> parameters)
      throws SQLException {
    PreparedStatement stmt = null;

    try {
      stmt = conn.prepareStatement(sql);

      setParameters(stmt, parameters);

      stmt.executeUpdate();
    } finally {
      JdbcUtils.close(stmt);
    }
  }
 protected void tearDown() throws Exception {
   JdbcUtils.close(dataSource);
 }
  public boolean isValidConnection(
      Connection conn, String validateQuery, int validationQueryTimeout) {
    try {
      if (conn.isClosed()) {
        return false;
      }
    } catch (SQLException ex) {
      // skip
      return false;
    }

    if (usePingMethod) {
      if (conn instanceof DruidPooledConnection) {
        conn = ((DruidPooledConnection) conn).getConnection();
      }

      if (conn instanceof ConnectionProxy) {
        conn = ((ConnectionProxy) conn).getRawObject();
      }

      if (clazz.isAssignableFrom(conn.getClass())) {
        if (validationQueryTimeout < 0) {
          validationQueryTimeout = DEFAULT_VALIDATION_QUERY_TIMEOUT;
        }

        try {
          ping.invoke(conn, true, validationQueryTimeout);
          return true;
        } catch (InvocationTargetException e) {
          Throwable cause = e.getCause();
          if (cause instanceof SQLException) {
            return false;
          }

          LOG.warn("Unexpected error in ping", e);
          return false;
        } catch (Exception e) {
          LOG.warn("Unexpected error in ping", e);
          return false;
        }
      }
    }

    Statement stmt = null;
    ResultSet rs = null;
    try {
      stmt = conn.createStatement();
      if (validationQueryTimeout > 0) {
        stmt.setQueryTimeout(validationQueryTimeout);
      }
      rs = stmt.executeQuery(validateQuery);
      return true;
    } catch (SQLException e) {
      return false;
    } catch (Exception e) {
      LOG.warn("Unexpected error in ping", e);
      return false;
    } finally {
      JdbcUtils.close(rs);
      JdbcUtils.close(stmt);
    }
  }
 @Override
 protected void tearDown() throws Exception {
   JdbcUtils.close(dataSource);
   Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
 }
  protected void tearDown() throws Exception {
    JdbcUtils.close(dataSource);

    Assert.assertEquals(0, JdbcStatManager.getInstance().getSqlList().size());
  }