use of org.apache.tomcat.jdbc.pool.DataSource in project tomcat by apache.
the class PoolCleanerTest method test2PoolCleaners.
@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());
}
use of org.apache.tomcat.jdbc.pool.DataSource in project tomcat by apache.
the class SimplePOJOExample method main.
public static void main(String[] args) throws Exception {
PoolConfiguration p = new PoolProperties();
p.setUrl("jdbc:mysql://localhost:3306/mysql?autoReconnect=true");
p.setDriverClassName("com.mysql.jdbc.Driver");
p.setUsername("root");
p.setPassword("password");
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.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
DataSource datasource = new DataSource();
datasource.setPoolProperties(p);
Connection con = null;
try {
con = datasource.getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from user");
int cnt = 1;
while (rs.next()) {
System.out.println((cnt++) + ". Host:" + rs.getString("Host") + " User:" + rs.getString("User") + " Password:" + rs.getString("Password"));
}
rs.close();
st.close();
} finally {
if (con != null) {
try {
con.close();
} catch (Exception ignore) {
// Ignore
}
}
}
}
use of org.apache.tomcat.jdbc.pool.DataSource in project pinot by linkedin.
the class TestDBResources method initializeDs.
private void initializeDs(PersistenceConfig configuration) {
String dbId = System.currentTimeMillis() + "" + Math.random();
ds = new DataSource();
ds.setUrl(configuration.getDatabaseConfiguration().getUrl() + dbId);
System.out.println("Creating db with connection url : " + ds.getUrl());
ds.setPassword(configuration.getDatabaseConfiguration().getPassword());
ds.setUsername(configuration.getDatabaseConfiguration().getUser());
ds.setDriverClassName(configuration.getDatabaseConfiguration().getProperties().get("hibernate.connection.driver_class"));
// pool size configurations
ds.setMaxActive(200);
ds.setMinIdle(10);
ds.setInitialSize(10);
// when returning connection to pool
ds.setTestOnReturn(true);
ds.setRollbackOnReturn(true);
// Timeout before an abandoned(in use) connection can be removed.
ds.setRemoveAbandonedTimeout(600_000);
ds.setRemoveAbandoned(true);
}
use of org.apache.tomcat.jdbc.pool.DataSource in project pinot by linkedin.
the class AbstractManagerTestBase method initializeDs.
private void initializeDs(PersistenceConfig configuration) throws Exception {
ds = new DataSource();
dbUrlId = configuration.getDatabaseConfiguration().getUrl() + System.currentTimeMillis() + "" + Math.random();
ds.setUrl(dbUrlId);
System.out.println("Creating db with connection url : " + ds.getUrl());
ds.setPassword(configuration.getDatabaseConfiguration().getPassword());
ds.setUsername(configuration.getDatabaseConfiguration().getUser());
ds.setDriverClassName(configuration.getDatabaseConfiguration().getProperties().get("hibernate.connection.driver_class"));
// pool size configurations
ds.setMaxActive(200);
ds.setMinIdle(10);
ds.setInitialSize(10);
// when returning connection to pool
ds.setTestOnReturn(true);
ds.setRollbackOnReturn(true);
// Timeout before an abandoned(in use) connection can be removed.
ds.setRemoveAbandonedTimeout(600_000);
ds.setRemoveAbandoned(true);
Connection conn = ds.getConnection();
// create schema
URL createSchemaUrl = getClass().getResource("/schema/create-schema.sql");
ScriptRunner scriptRunner = new ScriptRunner(conn, false, false);
scriptRunner.setDelimiter(";", true);
scriptRunner.runScript(new FileReader(createSchemaUrl.getFile()));
}
use of org.apache.tomcat.jdbc.pool.DataSource in project spring-boot by spring-projects.
the class DataSourceJsonSerializationTests method serializerWithMixin.
@Test
public void serializerWithMixin() throws Exception {
DataSource dataSource = new DataSource();
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(DataSource.class, DataSourceJson.class);
String value = mapper.writeValueAsString(dataSource);
assertThat(value.contains("\"url\":")).isTrue();
assertThat(StringUtils.countOccurrencesOf(value, "\"url\"")).isEqualTo(1);
}
Aggregations