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);
    }
  }
Exemple #2
0
  public static boolean registerDriver(Driver driver) {
    try {
      DriverManager.registerDriver(driver);

      try {
        MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();

        ObjectName objectName = new ObjectName(MBEAN_NAME);
        if (!mbeanServer.isRegistered(objectName)) {
          mbeanServer.registerMBean(instance, objectName);
        }
      } catch (Exception ex) {
        LOG.error("register druid-driver mbean error", ex);
      }

      return true;
    } catch (Exception e) {
      LOG.error("registerDriver error", e);
    }

    return false;
  }