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

The following examples show how to use com.zaxxer.hikari.HikariConfig#setAutoCommit() . 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: 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 3
Source File: MySQLDataSourceProvider.java    From conductor with Apache License 2.0 6 votes vote down vote up
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 4
Source File: PostgresDataSourceProvider.java    From conductor with Apache License 2.0 6 votes vote down vote up
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 5
Source File: JooqJobActivityConnectorComponent.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@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 6
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 7
Source File: DbUtil.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
private static DataSource createDataSource() {
    HikariConfig hikariDataSourceConfig = new HikariConfig();
    hikariDataSourceConfig.setJdbcUrl(DATABASE_URL);
    hikariDataSourceConfig.setDriverClassName(DRIVER_CLASS_NAME);
    hikariDataSourceConfig.setAutoCommit(false);
    return new HikariDataSource(hikariDataSourceConfig);
}
 
Example 8
Source File: EmbeddedBroker.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
/**
 * Use Hikari as default datasource implementation and configure with parameters given
 * in {@link BrokerCommonConfiguration.DataSourceConfiguration}
 * If user has provide an external datasource, then ignore the default datasource.
 *
 * @param dataSourceConfig configuration data set in this object
 * @return {@link DataSource} object configure with parameters
 */
private DataSource getDataSource(BrokerCommonConfiguration.DataSourceConfiguration dataSourceConfig) {
    if (Objects.isNull(configuration.getDataSource())) {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(dataSourceConfig.getUrl());
        config.setUsername(dataSourceConfig.getUser());
        config.setPassword(dataSourceConfig.getPassword());
        config.setAutoCommit(false);
        return new HikariDataSource(config);
    } else {
        return configuration.getDataSource();
    }
}
 
Example 9
Source File: Main.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
private static DataSource getDataSource(BrokerCommonConfiguration.DataSourceConfiguration dataSourceConfiguration) {
    HikariConfig config = new HikariConfig();
    config.setJdbcUrl(dataSourceConfiguration.getUrl());
    config.setUsername(dataSourceConfiguration.getUser());
    config.setPassword(dataSourceConfiguration.getPassword());
    config.setAutoCommit(false);

    return new HikariDataSource(config);
}
 
Example 10
Source File: DBManager.java    From blynk-server with GNU General Public License v3.0 5 votes vote down vote up
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 11
Source File: TestMysqlSchemaRepositoryIT.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void connect() throws Exception {
  HikariConfig hikariConfig = new HikariConfig();
  hikariConfig.setJdbcUrl(mysql.getJdbcUrl());
  hikariConfig.setUsername(mysql.getUsername());
  hikariConfig.setPassword(mysql.getPassword());
  hikariConfig.setAutoCommit(false);
  ds = new HikariDataSource(hikariConfig);
  Utils.runInitScript("schema.sql", ds);
}
 
Example 12
Source File: ReportingDBManager.java    From blynk-server with GNU General Public License v3.0 5 votes vote down vote up
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 13
Source File: DBSource.java    From TabooLib with MIT License 5 votes vote down vote up
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 14
Source File: HikariBasicConfig.java    From registry with Apache License 2.0 5 votes vote down vote up
private static HikariConfig getHikariConfig(final Map<String, Object> config, boolean autoCommit) {
    HikariConfig hikariConfig = new HikariConfig(new Properties(){{putAll(config);}});
    // need to do this because of a bug in Hikari that does not allow the override of the
    // property dataSource.autoCommit
    hikariConfig.setAutoCommit(autoCommit);
    return hikariConfig;
}
 
Example 15
Source File: LocalPostgresConfiguration.java    From fahrschein with Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
    HikariConfig hikariConfig = new HikariConfig();

    hikariConfig.setJdbcUrl("jdbc:postgresql://localhost:5432/local_nakadi_cursor_db");
    hikariConfig.setUsername("postgres");
    hikariConfig.setPassword("postgres");
    hikariConfig.setAutoCommit(false);

    return new HikariDataSource(hikariConfig);
}
 
Example 16
Source File: TestAbstractMysqlSource.java    From datacollector with Apache License 2.0 5 votes vote down vote up
protected static HikariDataSource connect() {
  HikariConfig hikariConfig = new HikariConfig();
  hikariConfig.setJdbcUrl(getJdbcUrl());
  hikariConfig.setUsername("root");
  hikariConfig.setPassword(MYSQL_PASSWORD);
  hikariConfig.setAutoCommit(false);
  return new HikariDataSource(hikariConfig);
}
 
Example 17
Source File: JdbcUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
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 18
Source File: AbstractMysqlSource.java    From datacollector with Apache License 2.0 5 votes vote down vote up
protected static HikariDataSource connect() {
  HikariConfig hikariConfig = new HikariConfig();
  hikariConfig.setJdbcUrl(getJdbcUrl());
  hikariConfig.setUsername("root");
  hikariConfig.setPassword(MYSQL_PASSWORD);
  hikariConfig.addDataSourceProperty("useSSL", false);
  hikariConfig.setAutoCommit(false);
  return new HikariDataSource(hikariConfig);
}
 
Example 19
Source File: MysqlSchemaRepositoryIT.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void connect() throws Exception {
  HikariConfig hikariConfig = new HikariConfig();
  hikariConfig.setJdbcUrl(mysql.getJdbcUrl());
  hikariConfig.setUsername(mysql.getUsername());
  hikariConfig.setPassword(mysql.getPassword());
  hikariConfig.addDataSourceProperty("useSSL", false);
  hikariConfig.setAutoCommit(false);
  ds = new HikariDataSource(hikariConfig);
  Utils.runInitScript("schema.sql", ds);
}
 
Example 20
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);
}