Example #1
0
  protected void afterConnectionClose(MockConnection conn) {
    connectionCloseCount.incrementAndGet();

    connections.remove(conn);

    if (LOG.isDebugEnabled()) {
      LOG.debug("conn-" + conn.getId() + " close");
    }
  }
Example #2
0
  @Override
  public Connection connect(String url, Properties info) throws SQLException {
    if (!acceptsURL(url)) {
      return null;
    }

    if (info != null) {
      Object val = info.get("connectSleep");
      if (val != null) {
        long millis = Long.parseLong(val.toString());
        try {
          Thread.sleep(millis);
        } catch (InterruptedException e) {
          // skip
        }
      }
    }

    MockConnection conn = new MockConnection(this, url, info);

    if (LOG.isDebugEnabled()) {
      LOG.debug("connect, url " + url + ", id " + conn.getId());
    }

    if (url == null) {
      connectCount.incrementAndGet();
      connections.add(conn);
      return conn;
    }

    if (url.startsWith(prefix)) {
      String catalog = url.substring(prefix.length());
      conn.setCatalog(catalog);

      connectCount.incrementAndGet();
      connections.add(conn);
      return conn;
    }

    if (url.startsWith(mockPrefix)) {
      String catalog = url.substring(mockPrefix.length());
      conn.setCatalog(catalog);

      connectCount.incrementAndGet();
      connections.add(conn);
      return conn;
    }

    return null;
  }
Example #3
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 #4
0
  protected ResultSet executeQuery(MockStatement stmt, String sql) throws SQLException {
    if (logExecuteQueryEnable && LOG.isDebugEnabled()) {
      LOG.debug("executeQuery " + sql);
    }

    MockConnection conn = stmt.getMockConnection();

    long idleTimeMillis = System.currentTimeMillis() - conn.getLastActiveTimeMillis();
    if (idleTimeMillis >= this.idleTimeCount) {
      throw new SQLException("connection is idle time count");
    }

    conn.setLastActiveTimeMillis(System.currentTimeMillis());

    if (conn != null) {
      if (conn.getConnectProperties() != null) {
        Object propertyValue = conn.getConnectProperties().get("executeSleep");

        if (propertyValue != null) {
          long millis = Long.parseLong(propertyValue.toString());
          try {
            Thread.sleep(millis);
          } catch (InterruptedException e) {
            // skip
          }
        }
      }
    }

    if ("SELECT value FROM _int_1000_".equalsIgnoreCase(sql)) {
      MockResultSet rs = new MockResultSet(stmt);

      for (int i = 0; i < 1000; ++i) {
        rs.getRows().add(new Object[] {i});
      }

      return rs;
    }

    return this.executeHandler.executeQuery(stmt, sql);
  }
Example #5
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;
  }