Search in sources :

Example 16 with DataSource

use of org.apache.tomcat.jdbc.pool.DataSource in project tomcat by apache.

the class SimplePOJOAsyncExample method main.

public static void main(String[] args) throws Exception {
    PoolConfiguration p = new PoolProperties();
    p.setFairQueue(true);
    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.setLogAbandoned(true);
    p.setRemoveAbandoned(true);
    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
    DataSource datasource = new DataSource();
    datasource.setPoolProperties(p);
    Connection con = null;
    try {
        Future<Connection> future = datasource.getConnectionAsync();
        while (!future.isDone()) {
            System.out.println("Connection is not yet available. Do some background work");
            try {
                //simulate work
                Thread.sleep(100);
            } catch (InterruptedException x) {
                Thread.interrupted();
            }
        }
        //should return instantly
        con = future.get();
        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 17 with DataSource

use of org.apache.tomcat.jdbc.pool.DataSource in project pinot by linkedin.

the class DaoProviderUtil method init.

public static void init(File localConfigFile) {
    PersistenceConfig configuration = createConfiguration(localConfigFile);
    dataSource = new DataSource();
    dataSource.setInitialSize(10);
    dataSource.setDefaultAutoCommit(false);
    dataSource.setMaxActive(100);
    dataSource.setUsername(configuration.getDatabaseConfiguration().getUser());
    dataSource.setPassword(configuration.getDatabaseConfiguration().getPassword());
    dataSource.setUrl(configuration.getDatabaseConfiguration().getUrl());
    dataSource.setDriverClassName(configuration.getDatabaseConfiguration().getDriver());
    dataSource.setValidationQuery("select 1");
    dataSource.setTestWhileIdle(true);
    dataSource.setTestOnBorrow(true);
    // when returning connection to pool
    dataSource.setTestOnReturn(true);
    dataSource.setRollbackOnReturn(true);
    // Timeout before an abandoned(in use) connection can be removed.
    dataSource.setRemoveAbandonedTimeout(600_000);
    dataSource.setRemoveAbandoned(true);
    init(dataSource);
}
Also used : DataSource(org.apache.tomcat.jdbc.pool.DataSource)

Example 18 with DataSource

use of org.apache.tomcat.jdbc.pool.DataSource in project Prism-Bukkit by prism.

the class Prism method initDbPool.

/**
     * 
     * @return
     */
public DataSource initDbPool() {
    DataSource pool = null;
    final String dns = "jdbc:mysql://" + config.getString("prism.mysql.hostname") + ":" + config.getString("prism.mysql.port") + "/" + config.getString("prism.mysql.database") + "?useUnicode=true&characterEncoding=UTF-8";
    pool = new DataSource();
    pool.setDriverClassName("com.mysql.jdbc.Driver");
    pool.setUrl(dns);
    pool.setUsername(config.getString("prism.mysql.username"));
    pool.setPassword(config.getString("prism.mysql.password"));
    pool.setInitialSize(config.getInt("prism.database.pool-initial-size"));
    pool.setMaxActive(config.getInt("prism.database.max-pool-connections"));
    pool.setMaxIdle(config.getInt("prism.database.max-idle-connections"));
    pool.setMaxWait(config.getInt("prism.database.max-wait"));
    pool.setRemoveAbandoned(true);
    pool.setRemoveAbandonedTimeout(60);
    pool.setTestOnBorrow(true);
    pool.setValidationQuery("/* ping */SELECT 1");
    pool.setValidationInterval(30000);
    return pool;
}
Also used : DataSource(org.apache.tomcat.jdbc.pool.DataSource)

Example 19 with DataSource

use of org.apache.tomcat.jdbc.pool.DataSource in project spring-boot by spring-projects.

the class DataSourceJsonSerializationTests method serializerFactory.

@Test
public void serializerFactory() throws Exception {
    DataSource dataSource = new DataSource();
    SerializerFactory factory = BeanSerializerFactory.instance.withSerializerModifier(new GenericSerializerModifier());
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializerFactory(factory);
    String value = mapper.writeValueAsString(dataSource);
    assertThat(value.contains("\"url\":")).isTrue();
}
Also used : SerializerFactory(com.fasterxml.jackson.databind.ser.SerializerFactory) BeanSerializerFactory(com.fasterxml.jackson.databind.ser.BeanSerializerFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataSource(org.apache.tomcat.jdbc.pool.DataSource) Test(org.junit.Test)

Example 20 with DataSource

use of org.apache.tomcat.jdbc.pool.DataSource in project Gargoyle by callakrsos.

the class ConnectionManager method getDataSource.

public static DataSource getDataSource(String driver, String url, String id, String pass) throws Exception {
    if (dataSource == null) {
        /*2016-08-10
			 * id도 입력안하는 경우가 있음 sqlite. by kyj
			 * */
        if (ValueUtil.isEmpty(driver, url)) {
            throw new GargoyleConnectionFailException("Check 'Datatabse Settings' on the 'Configuration tab' ");
        }
        dataSource = new DataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(id);
        dataSource.setPassword(pass);
        dataSource.setDefaultAutoCommit(false);
        dataSource.setInitialSize(2);
        dataSource.setLoginTimeout(3);
    }
    return dataSource;
}
Also used : GargoyleConnectionFailException(com.kyj.fx.voeditor.visual.exceptions.GargoyleConnectionFailException) DataSource(org.apache.tomcat.jdbc.pool.DataSource)

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