Java Code Examples for com.zaxxer.hikari.HikariConfig#setPoolName()

The following examples show how to use com.zaxxer.hikari.HikariConfig#setPoolName() . 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: HikariDataSourceHelper.java    From freeacs with MIT License 8 votes vote down vote up
public static DataSource dataSource(Config config) {
  HikariConfig hikariConfig = new HikariConfig();
  hikariConfig.setDriverClassName(config.getString("datasource.driverClassName"));
  hikariConfig.setJdbcUrl(config.getString("datasource.jdbcUrl"));
  hikariConfig.setUsername(config.getString("datasource.username"));
  hikariConfig.setPassword(config.getString("datasource.password"));

  hikariConfig.setMinimumIdle(config.getInt("datasource.minimum-idle"));
  hikariConfig.setMaximumPoolSize(config.getInt("datasource.maximum-pool-size"));
  hikariConfig.setConnectionTestQuery("SELECT 1");
  hikariConfig.setPoolName(config.getString("datasource.poolName"));

  hikariConfig.addDataSourceProperty("dataSource.cachePrepStmts", "true");
  hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSize", "250");
  hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSqlLimit", "2048");
  hikariConfig.addDataSourceProperty("dataSource.useServerPrepStmts", "true");

  hikariConfig.setAutoCommit(true);

  return new HikariDataSource(hikariConfig);
}
 
Example 2
Source File: TestDataConfig.java    From Spring with Apache License 2.0 7 votes vote down vote up
@Bean
public DataSource dataSource() {
    try {
        final HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName(driverClassName);
        hikariConfig.setJdbcUrl(url);
        hikariConfig.setUsername(username);
        hikariConfig.setPassword(password);

        hikariConfig.setMaximumPoolSize(5);
        hikariConfig.setConnectionTestQuery("SELECT 1");
        hikariConfig.setPoolName("springHikariCP");

        hikariConfig.addDataSourceProperty("dataSource.cachePrepStmts", "true");
        hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSize", "250");
        hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSqlLimit", "2048");
        hikariConfig.addDataSourceProperty("dataSource.useServerPrepStmts", "true");

        return new HikariDataSource(hikariConfig);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 3
Source File: DataSourceConfig.java    From momo-cloud-permission with Apache License 2.0 6 votes vote down vote up
@Bean(name = "primaryDataSource")
    @Primary
//    @ConfigurationProperties(prefix = "spring.datasource")
    public HikariDataSource dataSource() {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName(hikariDattaSourceConfig.getDriverClassName());
        hikariConfig.setJdbcUrl(hikariDattaSourceConfig.getJdbcUrl());
        hikariConfig.setUsername(hikariDattaSourceConfig.getUsername());
        hikariConfig.setPassword(hikariDattaSourceConfig.getPassword());
        hikariConfig.setMaxLifetime(hikariDattaSourceConfig.getMaxlifetime());
        hikariConfig.setConnectionTestQuery(hikariDattaSourceConfig.getConnectionTestQuery());
        hikariConfig.setPoolName(hikariDattaSourceConfig.getPoolName());
        hikariConfig.setIdleTimeout(hikariDattaSourceConfig.getIdleTimeout());
        hikariConfig.setAutoCommit(true);
        hikariConfig.setConnectionTimeout(hikariDattaSourceConfig.getConnectionTimeout());
        hikariConfig.setMinimumIdle(hikariDattaSourceConfig.getMinimumTdle());
        hikariConfig.setMaximumPoolSize(hikariDattaSourceConfig.getMaximumPoolSize());
        hikariConfig.addDataSourceProperty("dataSource.cachePrepStmts", "true");
        hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSize", "250");
        hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSqlLimit", "2048");
        hikariConfig.addDataSourceProperty("dataSource.useServerPrepStmts", "true");
        return new HikariDataSource(hikariConfig);
    }
 
Example 4
Source File: DataSourceConfig.java    From Spring with Apache License 2.0 6 votes vote down vote up
@Bean
public DataSource dataSource() {
    try {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName(driverClassName);
        hikariConfig.setJdbcUrl(url);
        hikariConfig.setUsername(username);
        hikariConfig.setPassword(password);

        hikariConfig.setMaximumPoolSize(5);
        hikariConfig.setConnectionTestQuery("SELECT 1");
        hikariConfig.setPoolName("springHikariCP");

        hikariConfig.addDataSourceProperty("dataSource.cachePrepStmts", "true");
        hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSize", "250");
        hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSqlLimit", "2048");
        hikariConfig.addDataSourceProperty("dataSource.useServerPrepStmts", "true");

        return new HikariDataSource(hikariConfig);
    } catch (Exception e) {
        return null;
    }
}
 
Example 5
Source File: DataSourceConfig.java    From freeacs with MIT License 6 votes vote down vote up
@Bean
public DataSource getDataSource(Environment config) {
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setDriverClassName(config.getProperty("main.datasource.driverClassName"));
    hikariConfig.setJdbcUrl(config.getProperty("main.datasource.jdbcUrl"));
    hikariConfig.setUsername(config.getProperty("main.datasource.username"));
    hikariConfig.setPassword(config.getProperty("main.datasource.password"));

    hikariConfig.setMinimumIdle(config.getProperty("main.datasource.minimum-idle", Integer.class, 1));
    hikariConfig.setMaximumPoolSize(config.getProperty("main.datasource.maximum-pool-size", Integer.class, 10));
    hikariConfig.setConnectionTestQuery("SELECT 1");
    hikariConfig.setPoolName(config.getProperty("main.datasource.poolName"));

    hikariConfig.addDataSourceProperty("dataSource.cachePrepStmts", "true");
    hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSize", "250");
    hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSqlLimit", "2048");
    hikariConfig.addDataSourceProperty("dataSource.useServerPrepStmts", "true");

    hikariConfig.setAutoCommit(true);

    return new HikariDataSource(hikariConfig);
}
 
Example 6
Source File: DefaultDataSourceConfig.java    From blog with MIT License 6 votes vote down vote up
@Primary
@Bean(destroyMethod = "close", name = "dataSource")
public DataSource dataSource() throws Exception {
    String jdbcUrl = new StringBuilder("jdbc:mysql://localhost:3306/")
            .append(dataSourceDo.getDatabaseName()).toString();

    HikariConfig config = new HikariConfig();
    config.setDriverClassName("com.mysql.cj.jdbc.Driver");
    config.setJdbcUrl(jdbcUrl);
    config.setUsername(dataSourceDo.getMysqlUserName());
    config.setPassword(dataSourceDo.getMysqlPassage());
    config.addDataSourceProperty("cachePrepStmts", "true");
    config.addDataSourceProperty("prepStmtCacheSize", "250");
    config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
    config.setIdleTimeout(25000);
    config.setMinimumIdle(5);
    config.setMaximumPoolSize(30);
    config.setPoolName("db_pool_blog");

    HikariDataSource ds = new HikariDataSource(config);

    return ds;
}
 
Example 7
Source File: HikariPool.java    From Java-11-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static DataSource createDataSource2() {
    HikariConfig config = new HikariConfig();
    config.setPoolName("cookpool");
    config.setDriverClassName("org.postgresql.Driver");
    config.setJdbcUrl("jdbc:postgresql://localhost/cookbook");
    config.setUsername("cook");
    //conf.setPassword("123Secret");
    config.setMaximumPoolSize(10);
    config.setMinimumIdle(2);
    config.addDataSourceProperty("cachePrepStmts", true);
    config.addDataSourceProperty("prepStmtCacheSize", 256);
    config.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
    config.addDataSourceProperty("useServerPrepStmts", true);

    HikariDataSource ds = new HikariDataSource(config);
    return ds;
}
 
Example 8
Source File: HelperSql.java    From helper with MIT License 6 votes vote down vote up
public HelperSql(@Nonnull DatabaseCredentials credentials) {
    final HikariConfig hikari = new HikariConfig();

    hikari.setPoolName("helper-sql-" + POOL_COUNTER.getAndIncrement());

    hikari.setDataSourceClassName(DATA_SOURCE_CLASS);
    hikari.addDataSourceProperty("serverName", credentials.getAddress());
    hikari.addDataSourceProperty("port", credentials.getPort());
    hikari.addDataSourceProperty("databaseName", credentials.getDatabase());

    hikari.setUsername(credentials.getUsername());
    hikari.setPassword(credentials.getPassword());

    hikari.setMaximumPoolSize(MAXIMUM_POOL_SIZE);
    hikari.setMinimumIdle(MINIMUM_IDLE);

    hikari.setMaxLifetime(MAX_LIFETIME);
    hikari.setConnectionTimeout(CONNECTION_TIMEOUT);
    hikari.setLeakDetectionThreshold(LEAK_DETECTION_THRESHOLD);

    // ensure we use unicode (this calls #setProperties, a hack for the mariadb driver)
    hikari.addDataSourceProperty("properties", "useUnicode=true;characterEncoding=utf8");

    this.source = new HikariDataSource(hikari);
    this.stream = SqlStream.connect(this.source);
}
 
Example 9
Source File: ConnectionPool.java    From StubbornJava with MIT License 6 votes vote down vote up
public static HikariDataSource getDataSourceFromConfig(
    Config conf
    , MetricRegistry metricRegistry
    , HealthCheckRegistry healthCheckRegistry) {

    HikariConfig jdbcConfig = new HikariConfig();
    jdbcConfig.setPoolName(conf.getString("poolName"));
    jdbcConfig.setMaximumPoolSize(conf.getInt("maximumPoolSize"));
    jdbcConfig.setMinimumIdle(conf.getInt("minimumIdle"));
    jdbcConfig.setJdbcUrl(conf.getString("jdbcUrl"));
    jdbcConfig.setUsername(conf.getString("username"));
    jdbcConfig.setPassword(conf.getString("password"));

    jdbcConfig.addDataSourceProperty("cachePrepStmts", conf.getBoolean("cachePrepStmts"));
    jdbcConfig.addDataSourceProperty("prepStmtCacheSize", conf.getInt("prepStmtCacheSize"));
    jdbcConfig.addDataSourceProperty("prepStmtCacheSqlLimit", conf.getInt("prepStmtCacheSqlLimit"));
    jdbcConfig.addDataSourceProperty("useServerPrepStmts", conf.getBoolean("useServerPrepStmts"));

    // Add HealthCheck
    jdbcConfig.setHealthCheckRegistry(healthCheckRegistry);

    // Add Metrics
    jdbcConfig.setMetricRegistry(metricRegistry);
    return new HikariDataSource(jdbcConfig);
}
 
Example 10
Source File: DataSourceFactory.java    From extract with MIT License 6 votes vote down vote up
private DataSource createPooled(final String poolName) {
	final HikariConfig config = new HikariConfig();

	config.setJdbcUrl(createURL());
	config.setUsername(null == user ? "extract" : user);
	config.setPassword(password);

	config.setMaximumPoolSize(maximumPoolSize);

	if (null != poolName) {
		config.setPoolName(poolName);
	}

	config.setDataSourceProperties(createProperties());

	return new HikariDataSource(config);
}
 
Example 11
Source File: ConnectionPool.java    From StubbornJava with MIT License 6 votes vote down vote up
public static HikariDataSource getDataSourceFromConfig(
    Config conf
    , MetricRegistry metricRegistry
    , HealthCheckRegistry healthCheckRegistry) {

    HikariConfig jdbcConfig = new HikariConfig();
    jdbcConfig.setPoolName(conf.getString("poolName"));
    jdbcConfig.setMaximumPoolSize(conf.getInt("maximumPoolSize"));
    jdbcConfig.setMinimumIdle(conf.getInt("minimumIdle"));
    jdbcConfig.setJdbcUrl(conf.getString("jdbcUrl"));
    jdbcConfig.setUsername(conf.getString("username"));
    jdbcConfig.setPassword(conf.getString("password"));

    jdbcConfig.addDataSourceProperty("cachePrepStmts", conf.getBoolean("cachePrepStmts"));
    jdbcConfig.addDataSourceProperty("prepStmtCacheSize", conf.getInt("prepStmtCacheSize"));
    jdbcConfig.addDataSourceProperty("prepStmtCacheSqlLimit", conf.getInt("prepStmtCacheSqlLimit"));
    jdbcConfig.addDataSourceProperty("useServerPrepStmts", conf.getBoolean("useServerPrepStmts"));

    // Add HealthCheck
    jdbcConfig.setHealthCheckRegistry(healthCheckRegistry);

    // Add Metrics
    jdbcConfig.setMetricRegistry(metricRegistry);
    return new HikariDataSource(jdbcConfig);
}
 
Example 12
Source File: DataSourceConfig.java    From fable-spring-read-replica with MIT License 5 votes vote down vote up
private DataSource buildDataSource(String poolName, String dataSourcePrefix) {
	final HikariConfig hikariConfig = new HikariConfig();

	hikariConfig.setPoolName(poolName);
	hikariConfig.setJdbcUrl(environment.getProperty(String.format("%s.url", dataSourcePrefix)));
	hikariConfig.setUsername(environment.getProperty(String.format("%s.username", dataSourcePrefix)));
	hikariConfig.setPassword(environment.getProperty(String.format("%s.password", dataSourcePrefix)));
	hikariConfig.setDriverClassName(environment.getProperty(String.format("%s.driver", dataSourcePrefix)));

	return new HikariDataSource(hikariConfig);
}
 
Example 13
Source File: SqlConnectionPool.java    From luna with MIT License 5 votes vote down vote up
/**
 * Creates a new connection pool.
 *
 * @return The new pool.
 * @throws SQLException If the pool cannot be created.
 */
public SqlConnectionPool build() throws SQLException {
    HikariConfig config = new HikariConfig();
    config.setJdbcUrl("jdbc:mysql://" + HOST + ":" + PORT + "/" + database + "");
    config.setUsername(USERNAME);
    config.setPassword(PASSWORD);
    config.setPoolName(poolName);
    return new SqlConnectionPool(new HikariDataSource(config));
}
 
Example 14
Source File: DatabaseConfiguration.java    From osiam with MIT License 5 votes vote down vote up
@Primary
@Bean
public DataSource dataSource() {
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setPoolName("osiam-cp");
    hikariConfig.setDriverClassName(driverClassName);
    hikariConfig.setJdbcUrl(databaseUrl);
    hikariConfig.setUsername(databaseUserName);
    hikariConfig.setPassword(databasePassword);
    hikariConfig.setMaximumPoolSize(maximumPoolSize);
    hikariConfig.setConnectionTimeout(connectionTimeoutMs);
    return new HikariDataSource(hikariConfig);
}
 
Example 15
Source File: DataSourceProvider.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected HikariConfig getConnectionPoolConfig(String storeName, Map<String, String> dsParameters) {
    Map<String, String> configParameters = dsParameters.entrySet().stream()
            .filter(e -> !DS_CONNECTION_PARAMS.contains(e.getKey()))
            .collect(Collectors.toMap(e -> getConfigParameterName(e.getKey()), Map.Entry::getValue));

    HikariConfig config = new HikariConfig(MapUtils.toProperties(configParameters));
    config.setRegisterMbeans(true);
    config.setPoolName(String.format("Connection Pool-%s", Stores.storeNameToString(storeName)));

    return config;
}
 
Example 16
Source File: MySQLDB.java    From Plan with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Setups the {@link HikariDataSource}
 */
@Override
public void setupDataSource() {
    try {
        loadMySQLDriver();

        HikariConfig hikariConfig = new HikariConfig();

        String host = config.get(DatabaseSettings.MYSQL_HOST);
        String port = config.get(DatabaseSettings.MYSQL_PORT);
        String database = config.get(DatabaseSettings.MYSQL_DATABASE);
        String launchOptions = config.get(DatabaseSettings.MYSQL_LAUNCH_OPTIONS);
        // REGEX: match "?", match "word=word&" *-times, match "word=word"
        if (launchOptions.isEmpty() || !launchOptions.matches("\\?((\\w*=\\w*)&)*(\\w*=\\w*)")) {
            launchOptions = "?rewriteBatchedStatements=true&useSSL=false";
            logger.error(locale.getString(PluginLang.DB_MYSQL_LAUNCH_OPTIONS_FAIL, launchOptions));
        }
        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
        hikariConfig.setJdbcUrl("jdbc:mysql://" + host + ":" + port + "/" + database + launchOptions);

        String username = config.get(DatabaseSettings.MYSQL_USER);
        String password = config.get(DatabaseSettings.MYSQL_PASS);

        hikariConfig.setUsername(username);
        hikariConfig.setPassword(password);
        hikariConfig.addDataSourceProperty("connectionInitSql", "set time_zone = '+00:00'");

        hikariConfig.setPoolName("Plan Connection Pool-" + increment);
        increment();

        hikariConfig.setAutoCommit(true);
        hikariConfig.setMaximumPoolSize(8);
        hikariConfig.setMaxLifetime(TimeUnit.MINUTES.toMillis(25L));
        hikariConfig.setLeakDetectionThreshold(TimeUnit.MINUTES.toMillis(10L));

        this.dataSource = new HikariDataSource(hikariConfig);
    } catch (HikariPool.PoolInitializationException e) {
        throw new DBInitException("Failed to set-up HikariCP Datasource: " + e.getMessage(), e);
    }
}
 
Example 17
Source File: HikariDataSourceFactory.java    From jboot with Apache License 2.0 5 votes vote down vote up
@Override
public DataSource createDataSource(DataSourceConfig config) {

    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setJdbcUrl(config.getUrl());
    hikariConfig.setUsername(config.getUser());
    hikariConfig.setPassword(config.getPassword());
    hikariConfig.addDataSourceProperty("cachePrepStmts", config.isCachePrepStmts());
    hikariConfig.addDataSourceProperty("prepStmtCacheSize", config.getPrepStmtCacheSize());
    hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", config.getPrepStmtCacheSqlLimit());
    hikariConfig.setDriverClassName(config.getDriverClassName());
    hikariConfig.setPoolName(config.getPoolName());
    hikariConfig.setMaximumPoolSize(config.getMaximumPoolSize());

    if (config.getMaxLifetime() != null) {
        hikariConfig.setMaxLifetime(config.getMaxLifetime());
    }
    if (config.getIdleTimeout() != null) {
        hikariConfig.setIdleTimeout(config.getIdleTimeout());
    }

    if (config.getMinimumIdle() != null) {
        hikariConfig.setMinimumIdle(config.getMinimumIdle());
    }

    if (config.getConnectionInitSql() != null) {
        hikariConfig.setConnectionInitSql(config.getConnectionInitSql());
    }


    HikariDataSource dataSource = new HikariDataSource(hikariConfig);

    if (Jboot.getMetric() != null) {
        dataSource.setMetricRegistry(Jboot.getMetric());
    }

    return dataSource;
}
 
Example 18
Source File: Storage.java    From Kepler with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Storage(String host, int port, String username, String password, String db) {
    try {
        HikariConfig config = new HikariConfig();
        config.setDriverClassName("org.mariadb.jdbc.Driver");
        config.setJdbcUrl("jdbc:mariadb://" + host + ":" + port + "/" + db);
        config.setUsername(username);
        config.setPassword(password);

        config.setPoolName("processing");

        // No martinmine/Leon/other Habbotards, you don't know better.
        // Read up on this first, before commenting dumb shit
        // https://github.com/brettwooldridge/HikariCP/wiki/About-Pool-Sizing
        // availableProcessors() already returns thread count on hyper-threaded processors
        // Thus we don't need the * 2 described there
        config.setMaximumPoolSize(Runtime.getRuntime().availableProcessors() + 1);
        config.setMinimumIdle(1);

        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        config.addDataSourceProperty("characterEncoding", "utf8");
        config.addDataSourceProperty("useUnicode", "true");
        config.addDataSourceProperty("useSSL", "false");
        config.addDataSourceProperty("serverTimezone", "UTC");
        config.addDataSourceProperty("sessionVariables", "sql_mode='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'");

        this.ds = new HikariDataSource(config);
        this.isConnected = true;

    } catch (Exception ex) {
        Storage.logError(ex);
    }
}
 
Example 19
Source File: HikariCPDataSourceProvider.java    From vertx-jdbc-client with Apache License 2.0 4 votes vote down vote up
@Override
public DataSource getDataSource(JsonObject json) throws SQLException {

  final HikariConfig config = new HikariConfig();

  for (Map.Entry<String, Object> entry : json) {
    switch (entry.getKey()) {
      case "dataSourceClassName":
        config.setDataSourceClassName((String) entry.getValue());
        break;
      case "jdbcUrl":
        config.setJdbcUrl((String) entry.getValue());
        break;
      case "username":
        config.setUsername((String) entry.getValue());
        break;
      case "password":
        config.setPassword((String) entry.getValue());
        break;
      case "autoCommit":
        config.setAutoCommit((Boolean) entry.getValue());
        break;
      case "connectionTimeout":
        config.setConnectionTimeout(getLong(entry.getValue()));
        break;
      case "idleTimeout":
        config.setIdleTimeout(getLong(entry.getValue()));
        break;
      case "maxLifetime":
        config.setMaxLifetime(getLong(entry.getValue()));
        break;
      case "connectionTestQuery":
        config.setConnectionTestQuery((String) entry.getValue());
        break;
      case "minimumIdle":
        config.setMinimumIdle((Integer) entry.getValue());
        break;
      case "maximumPoolSize":
        config.setMaximumPoolSize((Integer) entry.getValue());
        break;
      case "metricRegistry":
        throw new UnsupportedOperationException(entry.getKey());
      case "healthCheckRegistry":
        throw new UnsupportedOperationException(entry.getKey());
      case "poolName":
        config.setPoolName((String) entry.getValue());
        break;
      case "isolationInternalQueries":
        config.setIsolateInternalQueries((Boolean) entry.getValue());
        break;
      case "allowPoolSuspension":
        config.setAllowPoolSuspension((Boolean) entry.getValue());
        break;
      case "readOnly":
        config.setReadOnly((Boolean) entry.getValue());
        break;
      case "registerMBeans":
        config.setRegisterMbeans((Boolean) entry.getValue());
        break;
      case "catalog":
        config.setCatalog((String) entry.getValue());
        break;
      case "connectionInitSql":
        config.setConnectionInitSql((String) entry.getValue());
        break;
      case "driverClassName":
        config.setDriverClassName((String) entry.getValue());
        break;
      case "transactionIsolation":
        config.setTransactionIsolation((String) entry.getValue());
        break;
      case "validationTimeout":
        config.setValidationTimeout(getLong(entry.getValue()));
        break;
      case "leakDetectionThreshold":
        config.setLeakDetectionThreshold(getLong(entry.getValue()));
        break;
      case "dataSource":
        throw new UnsupportedOperationException(entry.getKey());
      case "threadFactory":
        throw new UnsupportedOperationException(entry.getKey());
      case "datasource":
        // extension to support configuring datasource.* properties
        for (Map.Entry<String, Object> key : ((JsonObject) entry.getValue())) {
          config.addDataSourceProperty(key.getKey(), key.getValue());
        }
        break;
    }
  }

  return new HikariDataSource(config);
}
 
Example 20
Source File: SkinStorage.java    From ChangeSkin with MIT License 4 votes vote down vote up
public SkinStorage(ChangeSkinCore core, String driver, String host, int port, String database
        , String user, String pass, boolean useSSL) {
    this.logger = core.getLogger();

    HikariConfig config = new HikariConfig();
    config.setPoolName(core.getPlugin().getName());

    config.setUsername(user);
    config.setPassword(pass);
    config.setDriverClassName(driver);

    ThreadFactory threadFactory = core.getPlugin().getThreadFactory();
    if (threadFactory != null) {
        config.setThreadFactory(threadFactory);
    }

    Properties properties = new Properties();

    String jdbcUrl = "jdbc:";
    if (driver.contains("sqlite")) {
        String folderPath = core.getPlugin().getPluginFolder().toAbsolutePath().toString();
        database = database.replace("{pluginDir}", folderPath);

        jdbcUrl += "sqlite://" + database;
        config.setConnectionTestQuery("SELECT 1");
        config.setMaximumPoolSize(1);

        //a try to fix https://www.spigotmc.org/threads/fastlogin.101192/page-26#post-1874647
        properties.setProperty("date_string_format", "yyyy-MM-dd HH:mm:ss");
    } else {
        jdbcUrl += "mysql://" + host + ':' + port + '/' + database;
        properties.setProperty("useSSL", String.valueOf(useSSL));
        // enable MySQL specific optimizations
        // default prepStmtCacheSize 25 - amount of cached statements - enough for us
        // default prepStmtCacheSqlLimit 256 - length of SQL - our queries are not longer
        // disabled by default - will return the same prepared statement instance
        config.addDataSourceProperty("cachePrepStmts", true);
        // default false - available in newer versions caches the statements server-side
        config.addDataSourceProperty("useServerPrepStmts", true);
    }

    config.setJdbcUrl(jdbcUrl);
    config.setDataSourceProperties(properties);
    this.dataSource = new HikariDataSource(config);
}