com.mysql.jdbc.JDBC4Connection Java Examples

The following examples show how to use com.mysql.jdbc.JDBC4Connection. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConsumerSchemaMeta.java    From syncer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private HashMap<Consumer, List<SchemaMeta>> build(Set<Consumer> consumers)
    throws SQLException {
  logger.info("Getting connection[{}], timeout in {}s", jdbcUrl,TIMEOUT);
  Connection connection = dataSource.getConnection();
  if (calculatedSchemaName.equals(MysqlConnection.DEFAULT_DB)) {
    // make it to get all databases
    ((JDBC4Connection) connection).setNullCatalogMeansCurrent(false);
  }
  HashMap<Consumer, List<SchemaMeta>> res;
  try {
    DatabaseMetaData metaData = connection.getMetaData();
    try (ResultSet tableResultSet = metaData
        .getTables(null, null, "%", new String[]{"TABLE"})) {
      res = getSchemaMeta(metaData, tableResultSet, consumers);
    }
    fetchLatestIfHas(connection, consumers);
  } finally {
    connection.close();
  }
  logger.info("Fetched {} for {}", res, consumers);
  return res;
}
 
Example #2
Source File: Metrics.java    From SuitAgent with Apache License 2.0 4 votes vote down vote up
/**
     * 获取宿主机对应连接的数据库文件大小信息的采集
     * @return
     * @throws SQLException
     * @throws SocketException
     * @throws UnknownHostException
     */
    private Collection<FalconReportObject> getDBFilesSize() throws Exception {
        Set<FalconReportObject> reportObjectSet = new HashSet<>();
        //jdbc:mysql://10.250.140.104:3306
        String url = ((JDBC4Connection) connection).getURL();
        List<String> ips = HostUtil.getHostIps();
        ips.add("127.0.0.1");
        ips.add("localhost");
        ips.add("0.0.0.0");
        for (String ip : ips) {
            url = url.replace(String.format("jdbc:mysql://%s:",ip),"");
        }
        if (NumberUtils.isNumber(url)){ //若有得到该连接下本机有效的端口地址
            String dataDir = getDataDirByPort(url);
            if (dataDir != null){
                List<String> filter = Arrays.asList("mysql","performance_schema","temp","information_schema");
                File dataDirFile = new File(dataDir);
                String[] dirList = dataDirFile.list();
                if (dirList != null){
                    for (String dbName : dirList) {
                        if (!filter.contains(dbName)){
                            try {
                                Path path = Paths.get(dataDir + File.separator + dbName);
                                if (path.toFile().isDirectory()){
                                    Files.walkFileTree(path,new SimpleFileVisitor<Path>(){
                                        @Override
                                        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                                            String fileName = file.getFileName().toString();
                                            String fileNameLower = fileName.toLowerCase();
                                            if(fileNameLower.endsWith(".myi") || fileNameLower.endsWith(".ibd")){
                                                FalconReportObject falconReportObject = new FalconReportObject();
                                                MetricsCommon.setReportCommonValue(falconReportObject,plugin.step());
                                                falconReportObject.setCounterType(CounterType.GAUGE);
                                                //时间戳会统一上报
//                                                falconReportObject.setTimestamp(System.currentTimeMillis() / 1000);
                                                falconReportObject.setMetric("mysql-file-size");
                                                falconReportObject.setValue(String.valueOf(file.toFile().length()));
                                                falconReportObject.appendTags("database=" + dbName);
                                                falconReportObject.appendTags("table=" + fileName.split("\\.")[0]);
                                                falconReportObject.appendTags("type=" + fileName.split("\\.")[1].toUpperCase());
                                                falconReportObject.appendTags(MetricsCommon.getTags(plugin.agentSignName(),plugin,plugin.serverName()));
                                                reportObjectSet.add(falconReportObject);
                                            }

                                            return super.visitFile(file, attrs);
                                        }
                                    });
                                }
                            } catch (IOException e) {
                                log.error("遍历目录 {} 发生异常",dbName,e);
                            }
                        }
                    }
                }
            }
        }
        return reportObjectSet;
    }