Search in sources :

Example 1 with DataSource

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());
}
Also used : DataSource(org.apache.tomcat.jdbc.pool.DataSource) Test(org.junit.Test)

Example 2 with DataSource

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
            }
        }
    }
}
Also used : PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(org.apache.tomcat.jdbc.pool.DataSource)

Example 3 with DataSource

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);
}
Also used : DataSource(org.apache.tomcat.jdbc.pool.DataSource)

Example 4 with DataSource

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()));
}
Also used : Connection(java.sql.Connection) FileReader(java.io.FileReader) ScriptRunner(com.linkedin.thirdeye.datalayer.ScriptRunner) URL(java.net.URL) DataSource(org.apache.tomcat.jdbc.pool.DataSource)

Example 5 with DataSource

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);
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataSource(org.apache.tomcat.jdbc.pool.DataSource) Test(org.junit.Test)

Aggregations

DataSource (org.apache.tomcat.jdbc.pool.DataSource)21 PoolProperties (org.apache.tomcat.jdbc.pool.PoolProperties)8 Test (org.junit.Test)8 Connection (java.sql.Connection)5 SQLException (java.sql.SQLException)5 DefaultProperties (org.apache.tomcat.jdbc.test.DefaultProperties)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 GargoyleException (com.kyj.fx.voeditor.visual.exceptions.GargoyleException)2 NotSupportException (com.kyj.fx.voeditor.visual.exceptions.NotSupportException)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 PoolConfiguration (org.apache.tomcat.jdbc.pool.PoolConfiguration)2 BeanSerializerFactory (com.fasterxml.jackson.databind.ser.BeanSerializerFactory)1 SerializerFactory (com.fasterxml.jackson.databind.ser.SerializerFactory)1 GargoyleConnectionFailException (com.kyj.fx.voeditor.visual.exceptions.GargoyleConnectionFailException)1 ScriptRunner (com.linkedin.thirdeye.datalayer.ScriptRunner)1 FileReader (java.io.FileReader)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1