Beispiel #1
0
  /**
   * 刷新数据源
   *
   * @param newConfig
   */
  private void reflush(DataSourceConfig newConfig) {
    // 本地配置优先
    overByLocal(newConfig);

    try {
      _ds_lock_.lock();
      if (null == config
          || (config.getDbStatus() == DBStatus.NA_STATUS
              && newConfig.getDbStatus() != DBStatus.NA_STATUS)) {
        // 创建数据源
        localTxDataSource =
            IDataSourceFactory.createLocalTxDataSource(
                dataSourceConfig2LocalTxDataSourceConfig(newConfig));
        logger.warn("Init datasource");
      } else if (config.getDbStatus() != DBStatus.NA_STATUS
          && newConfig.getDbStatus() == DBStatus.NA_STATUS) {
        // 销毁数据源
        destroy();
        logger.warn("Destroy datasource");
      } else {
        // 刷新
        if (isNeedFlush(config, newConfig)) {
          LocalTxDataSourceConfig c = dataSourceConfig2LocalTxDataSourceConfig(newConfig);
          localTxDataSource.setConnectionURL(c.getConnectionURL());
          localTxDataSource.setDriverClass(c.getDriverClassName());
          localTxDataSource.setExceptionSorterClassName(c.getExceptionSorterClassName());
          flush();
        }
      }

      config = newConfig;
      dbType = config.getDbType();
    } catch (Exception e) {
      logger.error(e);
    } finally {
      _ds_lock_.unlock();
    }
  }
Beispiel #2
0
  @Override
  protected DataSource getDataSource() throws SQLException {
    if (null == wrapDataSource) {
      try {
        String errorMsg = null;
        _ds_lock_.lock();
        if (null != wrapDataSource) {
          return wrapDataSource;
        }

        if (null == localTxDataSource) {
          errorMsg = "DynamicAtomDataSource maybe not inited";
          logger.error(errorMsg);
          throw new SQLException(errorMsg);
        }

        DataSource ds = localTxDataSource.getDatasource();
        if (null == ds) {
          errorMsg = "DynamicAtomDataSource maybe not inited";
          logger.error(errorMsg);
          throw new SQLException(errorMsg);
        }

        DBStatus status = config.getDbStatus();
        if (null == status || status == DBStatus.NA_STATUS) {
          errorMsg = "DynamicAtomDataSource database status unknown";
          logger.error(errorMsg);
          throw new SQLException(errorMsg);
        }

        AtomDataSourceWrapper newds =
            new AtomDataSourceWrapper(localTxDataSource.getDatasource(), config);
        wrapDataSource = newds;
        return wrapDataSource;
      } finally {
        _ds_lock_.unlock();
      }
    } else {
      return wrapDataSource;
    }
  }
Beispiel #3
0
  /**
   * @param dsConfig
   * @return
   */
  private LocalTxDataSourceConfig dataSourceConfig2LocalTxDataSourceConfig(
      DataSourceConfig dsConfig) {
    LocalTxDataSourceConfig config = new LocalTxDataSourceConfig();
    config.setJndiName(dsConfig.getDbName());
    config.setUserName(dsConfig.getUserName());
    config.setPassword(dsConfig.getPassword());
    config.setDriverClassName(dsConfig.getDriverClassName());
    config.setExceptionSorterClassName(dsConfig.getSorterClassName());
    if (dsConfig.getDbType() == DBType.MYSQL) {
      String connectionURL =
          DBCononectionURLTool.getMySqlConnectionURL(
              dsConfig.getIp(),
              dsConfig.getPort(),
              dsConfig.getDbName(),
              dsConfig.getConnectionProperties());
      config.setConnectionURL(connectionURL);
      // 如果可以找到mysql driver中的Valid就使用,否则不设置valid
      try {
        Class validClass = Class.forName(DBConstants.DEFAULT_MYSQL_VALID_CONNECTION_CHECKERCLASS);
        if (null != validClass) {
          config.setValidConnectionCheckerClassName(
              DBConstants.DEFAULT_MYSQL_VALID_CONNECTION_CHECKERCLASS);
        } else {
          logger.warn(
              "MYSQL Driver is Not Suport "
                  + DBConstants.DEFAULT_MYSQL_VALID_CONNECTION_CHECKERCLASS);
        }
      } catch (ClassNotFoundException e) {
        logger.warn(
            "MYSQL Driver is Not Suport "
                + DBConstants.DEFAULT_MYSQL_VALID_CONNECTION_CHECKERCLASS);
      } catch (NoClassDefFoundError e) {
        logger.warn(
            "MYSQL Driver is Not Suport "
                + DBConstants.DEFAULT_MYSQL_VALID_CONNECTION_CHECKERCLASS);
      }

      // 如果可以找到mysqlDriver中的integrationSorter就使用否则使用默认的
      try {
        Class integrationSorterCalss = Class.forName(DBConstants.MYSQL_INTEGRATION_SORTER_CLASS);
        if (null != integrationSorterCalss) {
          config.setExceptionSorterClassName(DBConstants.MYSQL_INTEGRATION_SORTER_CLASS);
        } else {
          config.setExceptionSorterClassName(DBConstants.DEFAULT_MYSQL_SORTER_CLASS);
          logger.warn(
              "MYSQL Driver is Not Suport "
                  + DBConstants.MYSQL_INTEGRATION_SORTER_CLASS
                  + " use default sorter "
                  + DBConstants.DEFAULT_MYSQL_SORTER_CLASS);
        }
      } catch (ClassNotFoundException e) {
        logger.warn(
            "MYSQL Driver is Not Suport "
                + DBConstants.MYSQL_INTEGRATION_SORTER_CLASS
                + " use default sorter "
                + DBConstants.DEFAULT_MYSQL_SORTER_CLASS);
      } catch (NoClassDefFoundError e) {
        logger.warn(
            "MYSQL Driver is Not Suport "
                + DBConstants.MYSQL_INTEGRATION_SORTER_CLASS
                + " use default sorter "
                + DBConstants.DEFAULT_MYSQL_SORTER_CLASS);
      }
    } else if (dsConfig.getDbType() == DBType.ORACLE) {
      String connectionURL =
          DBCononectionURLTool.getOracleConnectionURL(
              dsConfig.getIp(),
              dsConfig.getPort(),
              dsConfig.getDbName(),
              dsConfig.getOracleConnectionType());
      config.setConnectionURL(connectionURL);
      // 如果是oracle没有设置ConnectionProperties则给以个默认的
      if (!dsConfig.getConnectionProperties().isEmpty()) {
        config.setConnectionProperties(dsConfig.getConnectionProperties());
      } else {
        config.setConnectionProperties(DBConstants.DEFAULT_ORACLE_CONNECTION_PROPERTIES);
      }
    }

    config.setMinPoolSize(dsConfig.getMinPoolSize());
    config.setMaxPoolSize(dsConfig.getMaxPoolSize());
    config.setPreparedStatementCacheSize(dsConfig.getPreparedStatementCacheSize());
    if (dsConfig.getIdleTimeout() > 0) {
      config.setIdleTimeoutMinutes(dsConfig.getIdleTimeout());
    }
    if (dsConfig.getBlockingTimeout() > 0) {
      config.setBlockingTimeoutMillis(dsConfig.getBlockingTimeout());
    }
    return config;
  }
Beispiel #4
0
 /**
  * 优先使用本地配置
  *
  * @param config
  */
 private void overByLocal(DataSourceConfig config) {
   if (null == config || null == localConfig) {
     return;
   }
   if (StringUtil.isNotBlank(localConfig.getDriverClassName())) {
     config.setDriverClassName(localConfig.getDriverClassName());
   }
   if (StringUtil.isNotBlank(localConfig.getSorterClassName())) {
     config.setSorterClassName(localConfig.getSorterClassName());
   }
   if (StringUtil.isNotBlank(localConfig.getPassword())) {
     config.setPassword(localConfig.getPassword());
   }
   if (null != localConfig.getConnectionProperties()
       && !localConfig.getConnectionProperties().isEmpty()) {
     config.setConnectionProperties(localConfig.getConnectionProperties());
   }
 }