Java Code Examples for com.zaxxer.hikari.HikariConfig#setConnectionTimeout()
The following examples show how to use
com.zaxxer.hikari.HikariConfig#setConnectionTimeout() .
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: SagaPersistenceLoader.java From opensharding-spi-impl with Apache License 2.0 | 6 votes |
private static DataSource initDataSource(final SagaPersistenceConfiguration persistenceConfiguration) { HikariConfig config = new HikariConfig(); config.setJdbcUrl(persistenceConfiguration.getUrl()); config.setUsername(persistenceConfiguration.getUsername()); config.setPassword(persistenceConfiguration.getPassword()); config.setConnectionTimeout(persistenceConfiguration.getConnectionTimeoutMilliseconds()); config.setIdleTimeout(persistenceConfiguration.getIdleTimeoutMilliseconds()); config.setMaxLifetime(persistenceConfiguration.getMaxLifetimeMilliseconds()); config.setMaximumPoolSize(persistenceConfiguration.getMaxPoolSize()); config.setMinimumIdle(persistenceConfiguration.getMinPoolSize()); config.addDataSourceProperty("useServerPrepStmts", Boolean.TRUE.toString()); config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", 250); config.addDataSourceProperty("prepStmtCacheSqlLimit", 2048); config.addDataSourceProperty("useLocalSessionState", Boolean.TRUE.toString()); config.addDataSourceProperty("rewriteBatchedStatements", Boolean.TRUE.toString()); config.addDataSourceProperty("cacheResultSetMetadata", Boolean.TRUE.toString()); config.addDataSourceProperty("cacheServerConfiguration", Boolean.TRUE.toString()); config.addDataSourceProperty("elideSetAutoCommits", Boolean.TRUE.toString()); config.addDataSourceProperty("maintainTimeStats", Boolean.FALSE.toString()); config.addDataSourceProperty("netTimeoutForStreamingResults", 0); return new HikariDataSource(config); }
Example 2
Source File: PostgresqlServerClientConnectionManager.java From digdag with Apache License 2.0 | 6 votes |
private HikariDataSource createDataSourceWithConnectionPool() { DatabaseConfig databaseConfig = DatabaseConfig.convertFrom(systemConfig, "param_server"); String url = DatabaseConfig.buildJdbcUrl(databaseConfig); HikariConfig hikari = new HikariConfig(); hikari.setJdbcUrl(url); hikari.setDriverClassName(DatabaseMigrator.getDriverClassName(databaseConfig.getType())); hikari.setDataSourceProperties(DatabaseConfig.buildJdbcProperties(databaseConfig)); hikari.setConnectionTimeout(databaseConfig.getConnectionTimeout() * 1000); hikari.setIdleTimeout(databaseConfig.getIdleTimeout() * 1000); hikari.setValidationTimeout(databaseConfig.getValidationTimeout() * 1000); hikari.setMaximumPoolSize(databaseConfig.getMaximumPoolSize()); hikari.setMinimumIdle(databaseConfig.getMinimumPoolSize()); // Here should not set connectionTestQuery (that overrides isValid) because // ThreadLocalTransactionManager.commit assumes that Connection.isValid returns // false when an error happened during a transaction. logger.debug("Using postgresql URL {}", hikari.getJdbcUrl()); return new HikariDataSource(hikari); }
Example 3
Source File: DatabaseConfig.java From cloudbreak with Apache License 2.0 | 6 votes |
@Bean public DataSource dataSource() throws SQLException { DatabaseUtil.createSchemaIfNeeded("postgresql", databaseAddress, dbName, dbUser, dbPassword, dbSchemaName); HikariConfig config = new HikariConfig(); if (ssl && Files.exists(Paths.get(certFile))) { config.addDataSourceProperty("ssl", "true"); config.addDataSourceProperty("sslfactory", "org.postgresql.ssl.SingleCertValidatingFactory"); config.addDataSourceProperty("sslfactoryarg", "file://" + certFile); } if (nodeConfig.isNodeIdSpecified()) { config.addDataSourceProperty("ApplicationName", nodeConfig.getId()); } config.setDriverClassName("io.opentracing.contrib.jdbc.TracingDriver"); config.setJdbcUrl(String.format("jdbc:tracing:postgresql://%s/%s?currentSchema=%s", databaseAddress, dbName, dbSchemaName)); config.setUsername(dbUser); config.setPassword(dbPassword); config.setMaximumPoolSize(poolSize); config.setMinimumIdle(minimumIdle); config.setConnectionTimeout(SECONDS.toMillis(connectionTimeout)); config.setIdleTimeout(MINUTES.toMillis(idleTimeout)); return new HikariDataSource(config); }
Example 4
Source File: HikariCpImpl.java From JOOQ with Apache License 2.0 | 6 votes |
public HikariCpImpl() { Properties properties = new Properties(); properties.setProperty("dataSourceClassName", "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"); properties.setProperty("dataSource.serverName", "127.0.0.1"); properties.setProperty("dataSource.portNumber", "3306"); properties.setProperty("dataSource.databaseName", "study"); properties.setProperty("dataSource.user", "root"); properties.setProperty("dataSource.password", "root"); properties.setProperty("dataSource.encoding", "UTF-8"); properties.setProperty("maximumPoolSize", "100"); HikariConfig config = new HikariConfig(properties); config.setConnectionTimeout(30 * 1000); config.setIdleTimeout(60 * 1000); config.setMaxLifetime(60 * 1000); config.setMinimumIdle(50); config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", "1000"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); ds = new HikariDataSource(config); }
Example 5
Source File: MySQLDataSourceProvider.java From conductor with Apache License 2.0 | 6 votes |
private HikariConfig createConfiguration(){ HikariConfig cfg = new HikariConfig(); cfg.setJdbcUrl(configuration.getJdbcUrl()); cfg.setUsername(configuration.getJdbcUserName()); cfg.setPassword(configuration.getJdbcPassword()); cfg.setAutoCommit(false); cfg.setMaximumPoolSize(configuration.getConnectionPoolMaxSize()); cfg.setMinimumIdle(configuration.getConnectionPoolMinIdle()); cfg.setMaxLifetime(configuration.getConnectionMaxLifetime()); cfg.setIdleTimeout(configuration.getConnectionIdleTimeout()); cfg.setConnectionTimeout(configuration.getConnectionTimeout()); cfg.setTransactionIsolation(configuration.getTransactionIsolationLevel()); cfg.setAutoCommit(configuration.isAutoCommit()); ThreadFactory tf = new ThreadFactoryBuilder() .setDaemon(true) .setNameFormat("hikari-mysql-%d") .build(); cfg.setThreadFactory(tf); return cfg; }
Example 6
Source File: PostgresDataSourceProvider.java From conductor with Apache License 2.0 | 6 votes |
private HikariConfig createConfiguration(){ HikariConfig cfg = new HikariConfig(); cfg.setJdbcUrl(configuration.getJdbcUrl()); cfg.setUsername(configuration.getJdbcUserName()); cfg.setPassword(configuration.getJdbcPassword()); cfg.setAutoCommit(false); cfg.setMaximumPoolSize(configuration.getConnectionPoolMaxSize()); cfg.setMinimumIdle(configuration.getConnectionPoolMinIdle()); cfg.setMaxLifetime(configuration.getConnectionMaxLifetime()); cfg.setIdleTimeout(configuration.getConnectionIdleTimeout()); cfg.setConnectionTimeout(configuration.getConnectionTimeout()); cfg.setTransactionIsolation(configuration.getTransactionIsolationLevel()); cfg.setAutoCommit(configuration.isAutoCommit()); ThreadFactory tf = new ThreadFactoryBuilder() .setDaemon(true) .setNameFormat("hikari-postgres-%d") .build(); cfg.setThreadFactory(tf); return cfg; }
Example 7
Source File: JooqJobActivityConnectorComponent.java From titus-control-plane with Apache License 2.0 | 6 votes |
@Bean public JooqContext getJooqProducerContext(JooqConfiguration jooqConfiguration, EmbeddedPostgresService embeddedPostgresService) { HikariConfig hikariConfig = new HikariConfig(); hikariConfig.setAutoCommit(true); // Connection management hikariConfig.setConnectionTimeout(10000); hikariConfig.setMaximumPoolSize(10); hikariConfig.setLeakDetectionThreshold(3000); if (jooqConfiguration.isInMemoryDb()) { hikariConfig.setDataSource(embeddedPostgresService.getDataSource()); } else { hikariConfig.addDataSourceProperty(PGProperty.SSL.getName(), "true"); hikariConfig.addDataSourceProperty(PGProperty.SSL_MODE.getName(), "verify-ca"); hikariConfig.addDataSourceProperty(PGProperty.SSL_FACTORY.getName(), RDSSSLSocketFactory.class.getName()); hikariConfig.setJdbcUrl(jooqConfiguration.getProducerDatatabaseUrl()); } return new JooqContext(jooqConfiguration, new HikariDataSource(hikariConfig), embeddedPostgresService); }
Example 8
Source File: DatabaseConfig.java From cloudbreak with Apache License 2.0 | 6 votes |
@Bean public DataSource dataSource() throws SQLException { createSchemaIfNeeded("postgresql", databaseAddress, dbName, dbUser, dbPassword, dbSchemaName); HikariConfig config = new HikariConfig(); if (ssl && Files.exists(Paths.get(certFile))) { config.addDataSourceProperty("ssl", "true"); config.addDataSourceProperty("sslfactory", "org.postgresql.ssl.SingleCertValidatingFactory"); config.addDataSourceProperty("sslfactoryarg", "file://" + certFile); } if (nodeConfig.isNodeIdSpecified()) { config.addDataSourceProperty("ApplicationName", nodeConfig.getId()); } config.setDriverClassName("io.opentracing.contrib.jdbc.TracingDriver"); config.setJdbcUrl(String.format("jdbc:tracing:postgresql://%s/%s?currentSchema=%s", databaseAddress, dbName, dbSchemaName)); config.setUsername(dbUser); config.setPassword(dbPassword); config.setMaximumPoolSize(poolSize); config.setMinimumIdle(minimumIdle); config.setConnectionTimeout(SECONDS.toMillis(connectionTimeout)); config.setIdleTimeout(MINUTES.toMillis(idleTimeout)); return new HikariDataSource(config); }
Example 9
Source File: DatabaseConfig.java From cloudbreak with Apache License 2.0 | 6 votes |
@Bean public DataSource dataSource() throws SQLException { DatabaseUtil.createSchemaIfNeeded("postgresql", databaseAddress, dbName, dbUser, dbPassword, dbSchemaName); HikariConfig config = new HikariConfig(); if (ssl && Files.exists(Paths.get(certFile))) { config.addDataSourceProperty("ssl", "true"); config.addDataSourceProperty("sslfactory", "org.postgresql.ssl.SingleCertValidatingFactory"); config.addDataSourceProperty("sslfactoryarg", "file://" + certFile); } if (nodeConfig.isNodeIdSpecified()) { config.addDataSourceProperty("ApplicationName", nodeConfig.getId()); } config.setDriverClassName("io.opentracing.contrib.jdbc.TracingDriver"); config.setJdbcUrl(String.format("jdbc:tracing:postgresql://%s/%s?currentSchema=%s", databaseAddress, dbName, dbSchemaName)); config.setUsername(dbUser); config.setPassword(dbPassword); config.setMaximumPoolSize(poolSize); config.setMinimumIdle(minimumIdle); config.setConnectionTimeout(SECONDS.toMillis(connectionTimeout)); config.setIdleTimeout(MINUTES.toMillis(idleTimeout)); return new HikariDataSource(config); }
Example 10
Source File: DataSourceConfig.java From momo-cloud-permission with Apache License 2.0 | 6 votes |
@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 11
Source File: DBManager.java From blynk-server with GNU General Public License v3.0 | 5 votes |
private HikariConfig initConfig(BaseProperties serverProperties) { HikariConfig config = new HikariConfig(); config.setJdbcUrl(serverProperties.getProperty("jdbc.url")); config.setUsername(serverProperties.getProperty("user")); config.setPassword(serverProperties.getProperty("password")); config.setAutoCommit(false); config.setConnectionTimeout(serverProperties.getLongProperty("connection.timeout.millis")); config.setMaximumPoolSize(3); config.setMaxLifetime(0); config.setConnectionTestQuery("SELECT 1"); return config; }
Example 12
Source File: DBSource.java From TabooLib with MIT License | 5 votes |
public static HikariConfig createConfig(IHost host) { HikariConfig config = new HikariConfig(); config.setJdbcUrl(host.getConnectionUrl()); if (host instanceof SQLHost) { config.setDriverClassName(settings.getString("DefaultSettings.DriverClassName", "com.mysql.jdbc.Driver")); config.setUsername(((SQLHost) host).getUser()); config.setPassword(((SQLHost) host).getPassword()); } else if (host instanceof SQLiteHost) { config.setDriverClassName("org.sqlite.JDBC"); } else { throw new IllegalArgumentException("Invalid host: " + host.getClass().getName()); } config.setAutoCommit(settings.getBoolean("DefaultSettings.AutoCommit", true)); config.setMinimumIdle(settings.getInt("DefaultSettings.MinimumIdle", 1)); config.setMaximumPoolSize(settings.getInt("DefaultSettings.MaximumPoolSize", 10)); config.setValidationTimeout(settings.getInt("DefaultSettings.ValidationTimeout", 5000)); config.setConnectionTimeout(settings.getInt("DefaultSettings.ConnectionTimeout", 30000)); config.setIdleTimeout(settings.getInt("DefaultSettings.IdleTimeout", 600000)); config.setMaxLifetime(settings.getInt("DefaultSettings.MaxLifetime", 1800000)); if (settings.contains("DefaultSettings.ConnectionTestQuery")) { config.setConnectionTestQuery(settings.getString("DefaultSettings.ConnectionTestQuery")); } if (settings.contains("DefaultSettings.DataSourceProperty")) { settings.getConfigurationSection("DefaultSettings.DataSourceProperty").getKeys(false).forEach(key -> config.addDataSourceProperty(key, settings.getString("DefaultSettings.DataSourceProperty." + key))); } return config; }
Example 13
Source File: ReportingDBManager.java From blynk-server with GNU General Public License v3.0 | 5 votes |
private HikariConfig initConfig(BaseProperties serverProperties) { HikariConfig config = new HikariConfig(); config.setJdbcUrl(serverProperties.getProperty("reporting.jdbc.url")); config.setUsername(serverProperties.getProperty("reporting.user")); config.setPassword(serverProperties.getProperty("reporting.password")); config.setAutoCommit(false); config.setConnectionTimeout(serverProperties.getLongProperty("reporting.connection.timeout.millis")); config.setMaximumPoolSize(5); config.setMaxLifetime(0); config.setConnectionTestQuery("SELECT 1"); return config; }
Example 14
Source File: JDBCRawBackendDataSourceFactory.java From shardingsphere with Apache License 2.0 | 5 votes |
@Override public DataSource build(final String dataSourceName, final DataSourceParameter dataSourceParameter) { HikariConfig config = new HikariConfig(); String driverClassName = JDBCDriverURLRecognizerEngine.getJDBCDriverURLRecognizer(dataSourceParameter.getUrl()).getDriverClassName(); validateDriverClassName(driverClassName); config.setDriverClassName(driverClassName); config.setJdbcUrl(dataSourceParameter.getUrl()); config.setUsername(dataSourceParameter.getUsername()); config.setPassword(dataSourceParameter.getPassword()); config.setConnectionTimeout(dataSourceParameter.getConnectionTimeoutMilliseconds()); config.setIdleTimeout(dataSourceParameter.getIdleTimeoutMilliseconds()); config.setMaxLifetime(dataSourceParameter.getMaxLifetimeMilliseconds()); config.setMaximumPoolSize(dataSourceParameter.getMaxPoolSize()); config.setMinimumIdle(dataSourceParameter.getMinPoolSize()); config.setReadOnly(dataSourceParameter.isReadOnly()); config.addDataSourceProperty("useServerPrepStmts", Boolean.TRUE.toString()); config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", 250); config.addDataSourceProperty("prepStmtCacheSqlLimit", 2048); config.addDataSourceProperty("useLocalSessionState", Boolean.TRUE.toString()); config.addDataSourceProperty("rewriteBatchedStatements", Boolean.TRUE.toString()); config.addDataSourceProperty("cacheResultSetMetadata", Boolean.FALSE.toString()); config.addDataSourceProperty("cacheServerConfiguration", Boolean.TRUE.toString()); config.addDataSourceProperty("elideSetAutoCommits", Boolean.TRUE.toString()); config.addDataSourceProperty("maintainTimeStats", Boolean.FALSE.toString()); config.addDataSourceProperty("netTimeoutForStreamingResults", 0); config.addDataSourceProperty("tinyInt1isBit", Boolean.FALSE.toString()); return new HikariDataSource(config); }
Example 15
Source File: DatabaseConfiguration.java From osiam with MIT License | 5 votes |
@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 16
Source File: DataSourceProvider.java From digdag with Apache License 2.0 | 5 votes |
private void createPooledDataSource() { String url = DatabaseConfig.buildJdbcUrl(config); HikariConfig hikari = new HikariConfig(); hikari.setJdbcUrl(url); hikari.setDriverClassName(DatabaseMigrator.getDriverClassName(config.getType())); hikari.setDataSourceProperties(DatabaseConfig.buildJdbcProperties(config)); hikari.setConnectionTimeout(config.getConnectionTimeout() * 1000); hikari.setIdleTimeout(config.getIdleTimeout() * 1000); hikari.setValidationTimeout(config.getValidationTimeout() * 1000); hikari.setMaximumPoolSize(config.getMaximumPoolSize()); hikari.setMinimumIdle(config.getMinimumPoolSize()); hikari.setRegisterMbeans(config.getEnableJMX()); hikari.setLeakDetectionThreshold(config.getLeakDetectionThreshold()); // Here should not set connectionTestQuery (that overrides isValid) because // ThreadLocalTransactionManager.commit assumes that Connection.isValid returns // false when an error happened during a transaction. logger.debug("Using database URL {}", hikari.getJdbcUrl()); HikariDataSource ds = new HikariDataSource(hikari); this.ds = ds; this.closer = ds; }
Example 17
Source File: Utils.java From Rulette with Apache License 2.0 | 5 votes |
/** * This method build a {@link HikariConfig} object from the given properties file. * @param props An {@link Properties} object encapsulating Hikari properties */ public static HikariConfig getHikariConfig(Properties props) { HikariConfig hikariConfig = new HikariConfig(); hikariConfig.setDriverClassName(props.getProperty(PROPERTY_MYSQL_DRIVER_CLASS)); hikariConfig.setJdbcUrl(props.getProperty(PROPERTY_JDBC_URL)); hikariConfig.setUsername(props.getProperty(PROPERTY_USER_NAME)); hikariConfig.setPassword(props.getProperty(PROPERTY_PASSWORD)); hikariConfig.setMaximumPoolSize(Integer.parseInt(props.getProperty(PROPERTY_MAX_POOL_SIZE))); hikariConfig.setConnectionTimeout(Long.parseLong(props.getProperty(PROPERTY_CONN_TIMEOUT))); return hikariConfig; }
Example 18
Source File: JdbcUtil.java From datacollector with Apache License 2.0 | 5 votes |
private HikariConfig createDataSourceConfig( HikariPoolConfigBean hikariConfigBean, boolean autoCommit, boolean readOnly ) throws StageException { HikariConfig config = new HikariConfig(); config.setJdbcUrl(hikariConfigBean.getConnectionString()); if (hikariConfigBean.useCredentials){ config.setUsername(hikariConfigBean.getUsername().get()); config.setPassword(hikariConfigBean.getPassword().get()); } config.setAutoCommit(autoCommit); config.setReadOnly(readOnly); config.setMaximumPoolSize(hikariConfigBean.maximumPoolSize); config.setMinimumIdle(hikariConfigBean.minIdle); config.setConnectionTimeout(hikariConfigBean.connectionTimeout * MILLISECONDS); config.setIdleTimeout(hikariConfigBean.idleTimeout * MILLISECONDS); config.setMaxLifetime(hikariConfigBean.maxLifetime * MILLISECONDS); if (!StringUtils.isEmpty(hikariConfigBean.driverClassName)) { config.setDriverClassName(hikariConfigBean.driverClassName); } if (!StringUtils.isEmpty(hikariConfigBean.connectionTestQuery)) { config.setConnectionTestQuery(hikariConfigBean.connectionTestQuery); } if(hikariConfigBean.transactionIsolation != TransactionIsolationLevel.DEFAULT) { config.setTransactionIsolation(hikariConfigBean.transactionIsolation.name()); } if(StringUtils.isNotEmpty(hikariConfigBean.initialQuery)) { config.setConnectionInitSql(hikariConfigBean.initialQuery); } config.setDataSourceProperties(hikariConfigBean.getDriverProperties()); return config; }
Example 19
Source File: ConnectionPoolContextListener.java From java-docs-samples with Apache License 2.0 | 4 votes |
@SuppressFBWarnings( value = "USBR_UNNECESSARY_STORE_BEFORE_RETURN", justification = "Necessary for sample region tag.") private DataSource createConnectionPool() { // [START cloud_sql_postgres_servlet_create] // The configuration object specifies behaviors for the connection pool. HikariConfig config = new HikariConfig(); // Configure which instance and what database user to connect with. config.setJdbcUrl(String.format("jdbc:postgresql:///%s", DB_NAME)); config.setUsername(DB_USER); // e.g. "root", "postgres" config.setPassword(DB_PASS); // e.g. "my-password" // For Java users, the Cloud SQL JDBC Socket Factory can provide authenticated connections. // See https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory for details. config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.postgres.SocketFactory"); config.addDataSourceProperty("cloudSqlInstance", CLOUD_SQL_CONNECTION_NAME); // ... Specify additional connection properties here. // [START_EXCLUDE] // [START cloud_sql_postgres_servlet_limit] // maximumPoolSize limits the total number of concurrent connections this pool will keep. Ideal // values for this setting are highly variable on app design, infrastructure, and database. config.setMaximumPoolSize(5); // minimumIdle is the minimum number of idle connections Hikari maintains in the pool. // Additional connections will be established to meet this value unless the pool is full. config.setMinimumIdle(5); // [END cloud_sql_postgres_servlet_limit] // [START cloud_sql_postgres_servlet_timeout] // setConnectionTimeout is the maximum number of milliseconds to wait for a connection checkout. // Any attempt to retrieve a connection from this pool that exceeds the set limit will throw an // SQLException. config.setConnectionTimeout(10000); // 10 seconds // idleTimeout is the maximum amount of time a connection can sit in the pool. Connections that // sit idle for this many milliseconds are retried if minimumIdle is exceeded. config.setIdleTimeout(600000); // 10 minutes // [END cloud_sql_postgres_servlet_timeout] // [START cloud_sql_postgres_servlet_backoff] // Hikari automatically delays between failed connection attempts, eventually reaching a // maximum delay of `connectionTimeout / 2` between attempts. // [END cloud_sql_postgres_servlet_backoff] // [START cloud_sql_postgres_servlet_lifetime] // maxLifetime is the maximum possible lifetime of a connection in the pool. Connections that // live longer than this many milliseconds will be closed and reestablished between uses. This // value should be several minutes shorter than the database's timeout value to avoid unexpected // terminations. config.setMaxLifetime(1800000); // 30 minutes // [END cloud_sql_postgres_servlet_lifetime] // [END_EXCLUDE] // Initialize the connection pool using the configuration object. DataSource pool = new HikariDataSource(config); // [END cloud_sql_postgres_servlet_create] return pool; }
Example 20
Source File: ConnectionPoolContextListener.java From java-docs-samples with Apache License 2.0 | 4 votes |
@SuppressFBWarnings( value = "USBR_UNNECESSARY_STORE_BEFORE_RETURN", justification = "Necessary for sample region tag.") private DataSource createConnectionPool() { // [START cloud_sql_mysql_servlet_create] // The configuration object specifies behaviors for the connection pool. HikariConfig config = new HikariConfig(); // Configure which instance and what database user to connect with. config.setJdbcUrl(String.format("jdbc:mysql:///%s", DB_NAME)); config.setUsername(DB_USER); // e.g. "root", "postgres" config.setPassword(DB_PASS); // e.g. "my-password" // For Java users, the Cloud SQL JDBC Socket Factory can provide authenticated connections. // See https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory for details. config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory"); config.addDataSourceProperty("cloudSqlInstance", CLOUD_SQL_CONNECTION_NAME); // ... Specify additional connection properties here. // [START_EXCLUDE] // [START cloud_sql_mysql_servlet_limit] // maximumPoolSize limits the total number of concurrent connections this pool will keep. Ideal // values for this setting are highly variable on app design, infrastructure, and database. config.setMaximumPoolSize(5); // minimumIdle is the minimum number of idle connections Hikari maintains in the pool. // Additional connections will be established to meet this value unless the pool is full. config.setMinimumIdle(5); // [END cloud_sql_mysql_servlet_limit] // [START cloud_sql_mysql_servlet_timeout] // setConnectionTimeout is the maximum number of milliseconds to wait for a connection checkout. // Any attempt to retrieve a connection from this pool that exceeds the set limit will throw an // SQLException. config.setConnectionTimeout(10000); // 10 seconds // idleTimeout is the maximum amount of time a connection can sit in the pool. Connections that // sit idle for this many milliseconds are retried if minimumIdle is exceeded. config.setIdleTimeout(600000); // 10 minutes // [END cloud_sql_mysql_servlet_timeout] // [START cloud_sql_mysql_servlet_backoff] // Hikari automatically delays between failed connection attempts, eventually reaching a // maximum delay of `connectionTimeout / 2` between attempts. // [END cloud_sql_mysql_servlet_backoff] // [START cloud_sql_mysql_servlet_lifetime] // maxLifetime is the maximum possible lifetime of a connection in the pool. Connections that // live longer than this many milliseconds will be closed and reestablished between uses. This // value should be several minutes shorter than the database's timeout value to avoid unexpected // terminations. config.setMaxLifetime(1800000); // 30 minutes // [END cloud_sql_mysql_servlet_lifetime] // [END_EXCLUDE] // Initialize the connection pool using the configuration object. DataSource pool = new HikariDataSource(config); // [END cloud_sql_mysql_servlet_create] return pool; }