// @Test public void testConnectionCloseBlocking() throws SQLException { HikariConfig config = new HikariConfig(); config.setMinimumIdle(0); config.setMaximumPoolSize(1); config.setConnectionTimeout(1500); config.setDataSource(new CustomMockDataSource()); long start = ClockSource.INSTANCE.currentTime(); try (HikariDataSource ds = new HikariDataSource(config)) { Connection connection = ds.getConnection(); connection.close(); // Hikari only checks for validity for connections with lastAccess > 1000 ms so we sleep for // 1001 ms to force // Hikari to do a connection validation which will fail and will trigger the connection to be // closed UtilityElf.quietlySleep(1100L); shouldFail = true; // on physical connection close we sleep 2 seconds connection = ds.getConnection(); Assert.assertTrue( "Waited longer than timeout", (ClockSource.INSTANCE.elapsedMillis(start) < config.getConnectionTimeout())); } catch (SQLException e) { Assert.assertTrue( "getConnection failed because close connection took longer than timeout", (ClockSource.INSTANCE.elapsedMillis(start) < config.getConnectionTimeout())); } }
@Provides @Singleton DataSource provideDataSource() { HikariConfig config = new HikariConfig(); // The URL and driver should be changed to match your database // for MSSQL, this is a valid URI, cwad is the address of the database server and LM is the // database name // Similar to saying "USE LM" in your sql code config.setJdbcUrl("jdbc:sqlserver://cwad;database=LM"); config.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); config.setConnectionTimeout(20000); config.setMaximumPoolSize(20); // Change this to an easy to identify thread pool name. This helps with debugging config.setPoolName("LM-DB-POOL"); // This should probably be gotten in a different way, ideally it would be something like // an environment variable or a config file value rather than a hard coded value. config.setUsername("perseus"); config.setPassword("perseus"); return new HikariDataSource(config); }
public MySQLDatabase( Logger log, String ip, String port, String database, String username, String password) { super(log, "MySQL"); HikariConfig hikariConfig = new HikariConfig(); hikariConfig.setJdbcUrl("jdbc:mysql://" + ip + ":" + port + "/" + database); hikariConfig.setDriverClassName("com.mysql.jdbc.Driver"); hikariConfig.setUsername(username); hikariConfig.setPassword(password); hikariConfig.setMinimumIdle(1); hikariConfig.setMaximumPoolSize(10); hikariConfig.setConnectionTimeout(10000); _source = new HikariDataSource(hikariConfig); }
/** * get HikariCP Common Configuration * * @return */ private HikariConfig getHikariConfig() { HikariConfig config = new HikariConfig(); config.setMinimumIdle(minimumIdle); config.setMaximumPoolSize(maximumPoolSize); config.setConnectionTestQuery(validationQuery); config.setConnectionTimeout(connectionTimeout); config.setAutoCommit(isAutoCommit); config.addDataSourceProperty("cachePrepStmts", cachePrepStmts); config.addDataSourceProperty("prepStmtCacheSize", prepStmtCacheSize); config.addDataSourceProperty("useServerPrepStmts", useServerPrepStmts); return config; }
public static HikariDataSource getHikari() { config.setJdbcUrl(urlHC); config.setUsername(userHC); config.setPassword(passwordHC); config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", "250"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); config.setMaximumPoolSize(3); config.setIdleTimeout(28740000); config.setMaxLifetime(28740000); config.setConnectionTimeout(34000); HikariDataSource ds = new HikariDataSource(config); System.out.println("Returning HikairDataSource \n"); return ds; }
/** * Configure the database pool. * * @param logger A JUL logger to use for error reporting, etc. * @param user The user to connect as * @param pass The password to use * @param host The host to connect to * @param port The port to connect to * @param database The database to use * @param poolSize How many connections at most to keep in the connection pool awaiting activity. * Consider this against your Database's max connections across all askers. * @param connectionTimeout How long will a single connection wait for activity before timing out. * @param idleTimeout How long will a connection wait in the pool, typically, inactive. * @param maxLifetime How long will a connection be kept alive at maximum */ public Database( Logger logger, String user, String pass, String host, int port, String database, int poolSize, long connectionTimeout, long idleTimeout, long maxLifetime) { this.logger = logger; if (user != null && host != null && port > 0 && database != null) { HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:mysql://" + host + ":" + port + "/" + database); config.setConnectionTimeout(connectionTimeout); // 10000l); config.setIdleTimeout(idleTimeout); // 600000l); config.setMaxLifetime(maxLifetime); // 7200000l); config.setMaximumPoolSize(poolSize); // 10); config.setUsername(user); if (pass != null) { config.setPassword(pass); } this.datasource = new HikariDataSource(config); try { // No-op test. Connection connection = getConnection(); Statement statement = connection.createStatement(); statement.execute("SELECT 1"); statement.close(); connection.close(); } catch (SQLException se) { this.logger.log(Level.SEVERE, "Unable to initialize Database", se); this.datasource = null; } } else { this.datasource = null; this.logger.log(Level.SEVERE, "Database not configured and is unavaiable"); } }