@Bean(destroyMethod = "close") public DataSource dataSource() { String dbUrl, username, password; List<String> activeProfiles = Arrays.asList(environment.getActiveProfiles()); if (activeProfiles.contains("production")) { URI dbUri; try { dbUri = new URI(datasourcePropertyResolver.getProperty("heroku-uri")); } catch (URISyntaxException e) { throw new ApplicationContextException( "Heroku database connection pool is not configured correctly"); } dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ":" + dbUri.getPort() + dbUri.getPath(); username = dbUri.getUserInfo().split(":")[0]; password = dbUri.getUserInfo().split(":")[1]; } else { dbUrl = datasourcePropertyResolver.getProperty("url"); username = datasourcePropertyResolver.getProperty("username"); password = datasourcePropertyResolver.getProperty("password"); } DataSource dataSource = new DataSource(); dataSource.setDriverClassName(datasourcePropertyResolver.getProperty("driver-class-name")); dataSource.setUrl(dbUrl); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; }
@Test public void test2PoolCleaners() throws Exception { datasource.getPoolProperties().setTimeBetweenEvictionRunsMillis(2000); datasource.getPoolProperties().setTestWhileIdle(true); DataSource ds2 = new DataSource(datasource.getPoolProperties()); Assert.assertEquals( "Pool cleaner should not be started yet.", 0, ConnectionPool.getPoolCleaners().size()); Assert.assertNull("Pool timer should be null", ConnectionPool.getPoolTimer()); Assert.assertEquals( "Pool cleaner threads should not be present.", 0, countPoolCleanerThreads()); datasource.getConnection().close(); ds2.getConnection().close(); Assert.assertEquals( "Pool cleaner should have 2 cleaner.", 2, ConnectionPool.getPoolCleaners().size()); Assert.assertNotNull("Pool timer should not be null", ConnectionPool.getPoolTimer()); Assert.assertEquals("Pool cleaner threads should be 1.", 1, countPoolCleanerThreads()); datasource.close(); Assert.assertEquals( "Pool cleaner should have 1 cleaner.", 1, ConnectionPool.getPoolCleaners().size()); Assert.assertNotNull("Pool timer should not be null", ConnectionPool.getPoolTimer()); ds2.close(); Assert.assertEquals( "Pool shutdown, no cleaners should be present.", 0, ConnectionPool.getPoolCleaners().size()); Assert.assertNull("Pool timer should be null after shutdown", ConnectionPool.getPoolTimer()); Assert.assertEquals( "Pool cleaner threads should not be present after close.", 0, countPoolCleanerThreads()); }
@Bean public DataSource datasource() { org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource(); ds.setDriverClassName(databaseDriverClassName); ds.setUrl(datasourceUrl); ds.setUsername(databaseUsername); ds.setPassword(databasePassword); return ds; }
@Test public void testDataSourceUrlHasEmbeddedDefault() throws Exception { this.context.register( DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); org.apache.tomcat.jdbc.pool.DataSource dataSource = this.context.getBean(org.apache.tomcat.jdbc.pool.DataSource.class); assertNotNull(dataSource.getUrl()); assertNotNull(dataSource.getDriverClassName()); }
@SuppressWarnings("unused") private static Connection createTomcatJdbcPoolWrappedConnection() throws SQLException { // set up database DataSource ds = new DataSource(); ds.setDriverClassName("org.hsqldb.jdbc.JDBCDriver"); ds.setUrl("jdbc:hsqldb:mem:test"); ds.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.StatementDecoratorInterceptor"); Connection connection = ds.getConnection(); insertRecords(connection); return connection; }
public DataSource createDataSource(Properties properties, Context context, boolean XA) throws Exception { PoolConfiguration poolProperties = DataSourceFactory.parsePoolProperties(properties); if (poolProperties.getDataSourceJNDI() != null && poolProperties.getDataSource() == null) { performJNDILookup(context, poolProperties); } org.apache.tomcat.jdbc.pool.DataSource dataSource = XA ? new org.apache.tomcat.jdbc.pool.XADataSource(poolProperties) : new org.apache.tomcat.jdbc.pool.DataSource(poolProperties); // initialise the pool itself dataSource.createPool(); // Return the configured DataSource instance return dataSource; }
@Test public void testEmbeddedTypeDefaultsUsername() throws Exception { EnvironmentTestUtils.addEnvironment( this.context, "spring.datasource.driverClassName:org.hsqldb.jdbcDriver", "spring.datasource.url:jdbc:hsqldb:mem:testdb"); this.context.register( DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); DataSource bean = this.context.getBean(DataSource.class); assertNotNull(bean); org.apache.tomcat.jdbc.pool.DataSource pool = (org.apache.tomcat.jdbc.pool.DataSource) bean; assertEquals("org.hsqldb.jdbcDriver", pool.getDriverClassName()); assertEquals("sa", pool.getUsername()); }
private static void setDatasource() { PoolProperties p = new PoolProperties(); p.setUrl("jdbc:mysql://10.15.62.59:3306/userinfo"); // p.setUrl("jdbc:mysql://localhost:3306/mysql"); p.setDriverClassName("com.mysql.jdbc.Driver"); p.setUsername("root"); p.setPassword("Cadal205"); p.setJmxEnabled(true); p.setTestWhileIdle(false); p.setTestOnBorrow(true); p.setValidationQuery("SELECT 1"); p.setTestOnReturn(false); p.setValidationInterval(30000); p.setTimeBetweenEvictionRunsMillis(30000); p.setMaxActive(100); p.setInitialSize(10); p.setMaxWait(10000); p.setRemoveAbandonedTimeout(60); p.setMinEvictableIdleTimeMillis(30000); p.setMinIdle(10); p.setLogAbandoned(false); p.setRemoveAbandoned(true); p.setJdbcInterceptors( "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;" + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"); datasource = new DataSource(); datasource.setPoolProperties(p); // Connection conn = null; // conn = datasource.getConnection(); }
@Test public void testExplicitDriverClassClearsUserName() throws Exception { EnvironmentTestUtils.addEnvironment( this.context, "spring.datasource.driverClassName:org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationTests$DatabaseDriver", "spring.datasource.url:jdbc:foo://localhost"); this.context.register( DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); DataSource bean = this.context.getBean(DataSource.class); assertNotNull(bean); org.apache.tomcat.jdbc.pool.DataSource pool = (org.apache.tomcat.jdbc.pool.DataSource) bean; assertEquals( "org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationTests$DatabaseDriver", pool.getDriverClassName()); assertNull(pool.getUsername()); }
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof DataSource) { try { ((DataSource) bean).preRegister(null, new ObjectName("*:*")); } catch (Exception e) { log.warn("error registering mbean", e); } } return bean; }
public static Connection connectMySql() { Connection conn = null; try { conn = datasource.getConnection(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; }
@Bean public DataSource dataSource() { String[] userInfo = databaseUri.getUserInfo().split(":"); DataSource dataSource = new DataSource(); dataSource.setDriverClassName("org.postgresql.Driver"); dataSource.setUrl( String.format("jdbc:postgresql://%s%s", databaseUri.getHost(), databaseUri.getPath())); dataSource.setUsername(userInfo[0]); dataSource.setPassword(userInfo[1]); dataSource.setMinIdle(10); dataSource.setMaxActive(200); dataSource.setTestOnBorrow(true); dataSource.setTestOnReturn(true); dataSource.setTestWhileIdle(true); dataSource.setRemoveAbandoned(true); dataSource.setRemoveAbandonedTimeout(300); dataSource.setMaxWait(5000); dataSource.setValidationQuery("SELECT 1 FROM DUAL"); dataSource.setTimeBetweenEvictionRunsMillis(1800000); dataSource.setNumTestsPerEvictionRun(3); dataSource.setMinEvictableIdleTimeMillis(1800000); return dataSource; }
@Bean public DataSource getDefaultDataSource() { DataSource dataSource = new DataSource(); dataSource.setUrl(env.getProperty("jdbc.url")); dataSource.setUsername(env.getProperty("jdbc.user")); dataSource.setPassword(env.getProperty("jdbc.password")); dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); dataSource.setInitialSize(Integer.parseInt(env.getProperty("jdbc.initialSize"))); dataSource.setMaxActive(Integer.parseInt(env.getProperty("jdbc.maxActive"))); dataSource.setValidationQuery(env.getProperty("jdbc.validationQuery")); dataSource.setMaxIdle(Integer.parseInt(env.getProperty("jdbc.maxIdle"))); dataSource.setMinIdle(Integer.parseInt(env.getProperty("jdbc.minIdle"))); dataSource.setMaxWait(Integer.parseInt(env.getProperty("jdbc.maxWait"))); dataSource.setTestOnBorrow(Boolean.parseBoolean(env.getProperty("jdbc.testOnBorrow"))); dataSource.setTestOnReturn(Boolean.parseBoolean(env.getProperty("jdbc.testOnReturn"))); dataSource.setTestWhileIdle(Boolean.parseBoolean(env.getProperty("jdbc.testWhileIdle"))); dataSource.setTimeBetweenEvictionRunsMillis( Integer.parseInt(env.getProperty("jdbc.timeBetweenEvictionRunsMillis"))); dataSource.setNumTestsPerEvictionRun( Integer.parseInt(env.getProperty("jdbc.numTestsPerEvictionRun"))); dataSource.setMinEvictableIdleTimeMillis( Integer.parseInt(env.getProperty("jdbc.minEvictableIdleTimeMillis"))); dataSource.setRemoveAbandonedTimeout( Integer.parseInt(env.getProperty("jdbc.removeAbandonedTimeout"))); dataSource.setRemoveAbandoned(Boolean.parseBoolean(env.getProperty("jdbc.removeAbandoned"))); dataSource.setLogAbandoned(Boolean.parseBoolean(env.getProperty("jdbc.logAbandoned"))); return dataSource; }
private static void pullTomcatJdbcDataSourceProperties(String name, DataSource dataSource) { // si tomcat-jdbc, alors on récupère des infos final org.apache.tomcat.jdbc.pool.DataSource jdbcDataSource = (org.apache.tomcat.jdbc.pool.DataSource) dataSource; final BasicDataSourcesProperties properties = TOMCAT_JDBC_DATASOURCES_PROPERTIES; // basicDataSource.getNumActive() est en théorie égale à USED_CONNECTION_COUNT à un instant t, // numIdle + numActive est le nombre de connexions ouvertes dans la bdd pour ce serveur à un // instant t // les propriétés généralement importantes en premier (se méfier aussi de testOnBorrow) properties.put(name, MAX_ACTIVE_PROPERTY_NAME, jdbcDataSource.getMaxActive()); properties.put(name, "defaultCatalog", jdbcDataSource.getDefaultCatalog()); properties.put(name, "defaultAutoCommit", jdbcDataSource.getDefaultAutoCommit()); properties.put(name, "defaultReadOnly", jdbcDataSource.getDefaultReadOnly()); properties.put( name, "defaultTransactionIsolation", jdbcDataSource.getDefaultTransactionIsolation()); properties.put(name, "driverClassName", jdbcDataSource.getDriverClassName()); properties.put(name, "connectionProperties", jdbcDataSource.getConnectionProperties()); properties.put(name, "initSQL", jdbcDataSource.getInitSQL()); properties.put(name, "initialSize", jdbcDataSource.getInitialSize()); properties.put(name, "maxIdle", jdbcDataSource.getMaxIdle()); properties.put(name, "maxWait", jdbcDataSource.getMaxWait()); properties.put(name, "maxAge", jdbcDataSource.getMaxAge()); properties.put(name, "faireQueue", jdbcDataSource.isFairQueue()); properties.put(name, "jmxEnabled", jdbcDataSource.isJmxEnabled()); properties.put( name, "minEvictableIdleTimeMillis", jdbcDataSource.getMinEvictableIdleTimeMillis()); properties.put(name, "minIdle", jdbcDataSource.getMinIdle()); properties.put(name, "numTestsPerEvictionRun", jdbcDataSource.getNumTestsPerEvictionRun()); properties.put(name, "testOnBorrow", jdbcDataSource.isTestOnBorrow()); properties.put(name, "testOnConnect", jdbcDataSource.isTestOnConnect()); properties.put(name, "testOnReturn", jdbcDataSource.isTestOnReturn()); properties.put(name, "testWhileIdle", jdbcDataSource.isTestWhileIdle()); properties.put( name, "timeBetweenEvictionRunsMillis", jdbcDataSource.getTimeBetweenEvictionRunsMillis()); properties.put(name, "validationInterval", jdbcDataSource.getValidationInterval()); properties.put(name, "validationQuery", jdbcDataSource.getValidationQuery()); properties.put(name, "validatorClassName", jdbcDataSource.getValidatorClassName()); }
@Bean(destroyMethod = "close") public DataSource dataSource() { DataSource dataSource = new DataSource(); dataSource.setUrl(url); dataSource.setDriverClassName(driverClassName); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setMaxActive(maxActive); dataSource.setMaxIdle(maxIdle); dataSource.setMinIdle(minIdle); dataSource.setMaxWait(maxWait); dataSource.setDefaultAutoCommit(defaultAutoCommit); dataSource.setValidationQuery(validationQuery); dataSource.setValidationInterval(validationInterval); dataSource.setTestWhileIdle(testWhileIdle); dataSource.setLogAbandoned(logAbandoned); dataSource.setRemoveAbandoned(removeAbandoned); dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout); return dataSource; }
public DataSource dataSource() { org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(); DatabaseConfiguration conf = DatabaseConfiguration.getInstance(); dataSource.setDriverClassName(conf.getDriverClassName()); dataSource.setUsername(conf.getUsername()); dataSource.setPassword(conf.getPassword()); dataSource.setUrl(conf.getUrl()); dataSource.setMaxActive(conf.getMaxActive()); dataSource.setMaxIdle(conf.getMaxIdle()); dataSource.setInitialSize(conf.getInitialSize()); dataSource.setTestWhileIdle(conf.isTestWhileIdle()); dataSource.setValidationQuery(conf.getValidationQuery()); dataSource.setValidationInterval(conf.getValidationInterval()); dataSource.setMaxAge(conf.getMaxAge()); return dataSource; }