Java Code Examples for org.apache.tomcat.jdbc.pool.ConnectionPool

The following examples show how to use org.apache.tomcat.jdbc.pool.ConnectionPool. These examples are extracted from open source projects. 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 Project: Tomcat8-Source-Read   Source File: StatementCache.java    License: MIT License 6 votes vote down vote up
@Override
public void reset(ConnectionPool parent, PooledConnection con) {
    super.reset(parent, con);
    if (parent==null) {
        cacheSize = null;
        this.pcon = null;
        if (oname != null) {
            JmxUtil.unregisterJmx(oname);
            oname = null;
        }
    } else {
        cacheSize = cacheSizeMap.get(parent);
        this.pcon = con;
        if (!pcon.getAttributes().containsKey(STATEMENT_CACHE_ATTR)) {
            ConcurrentHashMap<CacheKey,CachedStatement> cache =
                    new ConcurrentHashMap<>();
            pcon.getAttributes().put(STATEMENT_CACHE_ATTR,cache);
        }
        if (oname == null) {
            String keyprop = ",JdbcInterceptor=" + getClass().getSimpleName();
            oname = JmxUtil.registerJmx(pcon.getObjectName(), keyprop, this);
        }
    }
}
 
Example 2
Source Project: Tomcat8-Source-Read   Source File: SlowQueryReport.java    License: MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void poolStarted(ConnectionPool pool) {
    super.poolStarted(pool);
    //see if we already created a map for this pool
    queries = SlowQueryReport.perPoolStats.get(pool.getName());
    if (queries==null) {
        //create the map to hold our stats
        //however TODO we need to improve the eviction
        //selection
        queries = new ConcurrentHashMap<>();
        if (perPoolStats.putIfAbsent(pool.getName(), queries)!=null) {
            //there already was one
            queries = SlowQueryReport.perPoolStats.get(pool.getName());
        }
    }
}
 
Example 3
Source Project: Tomcat8-Source-Read   Source File: ResetAbandonedTimer.java    License: MIT License 6 votes vote down vote up
@Override
public void reset(ConnectionPool parent, PooledConnection con) {
    super.reset(parent, con);
    if (con == null) {
        this.pcon = null;
        if (oname != null) {
            JmxUtil.unregisterJmx(oname);
            oname = null;
        }
    } else {
        this.pcon = con;
        if (oname == null) {
            String keyprop = ",JdbcInterceptor=" + getClass().getSimpleName();
            oname = JmxUtil.registerJmx(pcon.getObjectName(), keyprop, this);
        }
    }
}
 
Example 4
Source Project: Tomcat8-Source-Read   Source File: TestSlowQueryReport.java    License: MIT License 6 votes vote down vote up
@Test
public void testFastSql() throws Exception {
    int count = 3;
    Connection con = this.datasource.getConnection();
    String fastSql = this.datasource.getValidationQuery();
    for (int i=0; i<count; i++) {
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(fastSql);
        rs.close();
        st.close();
    }
    Map<String,SlowQueryReport.QueryStats> map = SlowQueryReport.getPoolStats(datasource.getPool().getName());
    Assert.assertNotNull(map);
    Assert.assertEquals(1,map.size());
    ConnectionPool pool = datasource.getPool();
    con.close();
    tearDown();
    Assert.assertNull(SlowQueryReport.getPoolStats(pool.getName()));
}
 
Example 5
Source Project: Tomcat8-Source-Read   Source File: TestSlowQueryReport.java    License: MIT License 6 votes vote down vote up
@Test
public void testFailedSql() throws Exception {
    int count = 3;
    Connection con = this.datasource.getConnection();
    for (int i=0; i<count; i++) {
        Statement st = con.createStatement();
        try {
            ResultSet rs = st.executeQuery(failedSql);
            rs.close();
        }catch (Exception x) {
            // NO-OP
        }
        st.close();
    }
    Map<String,SlowQueryReport.QueryStats> map = SlowQueryReport.getPoolStats(datasource.getPool().getName());
    Assert.assertNotNull(map);
    Assert.assertEquals(1,map.size());
    ConnectionPool pool = datasource.getPool();
    String key = map.keySet().iterator().next();
    SlowQueryReport.QueryStats stats = map.get(key);
    System.out.println("Stats:"+stats);
    con.close();
    tearDown();
    Assert.assertNull(SlowQueryReport.getPoolStats(pool.getName()));
}
 
Example 6
Source Project: Tomcat8-Source-Read   Source File: PoolCleanerTest.java    License: MIT License 6 votes vote down vote up
@Test
public void testPoolCleaner() throws Exception {
    datasource.getPoolProperties().setTimeBetweenEvictionRunsMillis(2000);
    datasource.getPoolProperties().setTestWhileIdle(true);
    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();
    Assert.assertEquals("Pool cleaner should have 1 cleaner.",1,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 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());


}
 
Example 7
Source Project: jqm   Source File: PayloadInterceptor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void reset(ConnectionPool parent, PooledConnection con)
{
    trackedConnPool = parent;
    trackedPooledCon = con;

    if (parent != null && con != null)
    {
        ConnPair cp = new ConnPair();
        cp.conn = con;
        cp.thread = Thread.currentThread();
        Set<ConnPair> pairs = conns.get(parent);
        if (pairs != null)
        {
            conns.get(parent).add(cp);
        }
    }
}
 
Example 8
Source Project: Tomcat8-Source-Read   Source File: JmxPasswordTest.java    License: MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.datasource.setDriverClassName(Driver.class.getName());
    this.datasource.setUrl("jdbc:tomcat:test");
    this.datasource.setPassword(password);
    this.datasource.setUsername(username);
    this.datasource.getConnection().close();
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    String domain = "tomcat.jdbc";
    Hashtable<String,String> properties = new Hashtable<>();
    properties.put("type", "ConnectionPool");
    properties.put("class", this.getClass().getName());
    oname = new ObjectName(domain,properties);
    ConnectionPool pool = datasource.createPool();
    org.apache.tomcat.jdbc.pool.jmx.ConnectionPool jmxPool = new org.apache.tomcat.jdbc.pool.jmx.ConnectionPool(pool);
    mbs.registerMBean(jmxPool, oname);

}
 
Example 9
Source Project: tomcatsrc   Source File: TestSlowQueryReport.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testFastSql() throws Exception {
    int count = 3;
    this.init();
    this.datasource.setMaxActive(1);
    this.datasource.setJdbcInterceptors(SlowQueryReport.class.getName());
    Connection con = this.datasource.getConnection();
    String fastSql = this.datasource.getValidationQuery();
    for (int i=0; i<count; i++) {
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(fastSql);
        rs.close();
        st.close();
    }
    Map<String,SlowQueryReport.QueryStats> map = SlowQueryReport.getPoolStats(datasource.getPool().getName());
    Assert.assertNotNull(map);
    Assert.assertEquals(0,map.size());
    ConnectionPool pool = datasource.getPool();
    con.close();
    tearDown();
    Assert.assertNull(SlowQueryReport.getPoolStats(pool.getName()));
}
 
Example 10
Source Project: jqm   Source File: PayloadInterceptor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Called by the engine to trigger the cleanup at the end of a payload thread.
 */
public static int forceCleanup(Thread t)
{
    int i = 0;
    for (Map.Entry<ConnectionPool, Set<ConnPair>> e : conns.entrySet())
    {
        for (ConnPair c : e.getValue())
        {
            if (c.thread.equals(t))
            {
                try
                {
                    // This will in turn remove it from the static Map.
                    c.conn.getHandler().invoke(c.conn, Connection.class.getMethod("close"), null);
                }
                catch (Throwable e1)
                {
                    e1.printStackTrace();
                }
                i++;
            }
        }
    }
    return i;
}
 
Example 11
@Test
void testCloseDataSource() throws Exception {
	TomcatJdbcDataSourceFactory tomcatJdbcDataSourceFactory = new TomcatJdbcDataSourceFactory();
	tomcatJdbcDataSourceFactory.setInitialSize(0);

	DataSourceInformation dataSourceInformation = new DataSourceInformation(
			DatabaseType.MYSQL, "localhost", 3306, "test", "user", "password");
	DataSource dataSource = tomcatJdbcDataSourceFactory
			.createDataSource(dataSourceInformation);
	assertThat(dataSource).isNotNull();

	ConnectionPool pool = dataSource.createPool();
	assertThat(pool.isClosed()).isFalse();
	tomcatJdbcDataSourceFactory.closeDataSource(dataSource);
	assertThat(pool.isClosed()).isTrue();
}
 
Example 12
Source Project: tomcatsrc   Source File: JmxPasswordTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.datasource.setDriverClassName(Driver.class.getName());
    this.datasource.setUrl("jdbc:tomcat:test");
    this.datasource.setPassword(password);
    this.datasource.setUsername(username);
    this.datasource.getConnection().close();
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    String domain = "tomcat.jdbc";
    Hashtable<String,String> properties = new Hashtable<String,String>();
    properties.put("type", "ConnectionPool");
    properties.put("class", this.getClass().getName());
    oname = new ObjectName(domain,properties);
    ConnectionPool pool = datasource.createPool();
    org.apache.tomcat.jdbc.pool.jmx.ConnectionPool jmxPool = new org.apache.tomcat.jdbc.pool.jmx.ConnectionPool(pool);
    mbs.registerMBean(jmxPool, oname);

}
 
Example 13
Source Project: Tomcat7.0.67   Source File: SlowQueryReport.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void poolStarted(ConnectionPool pool) {
    super.poolStarted(pool);
    //see if we already created a map for this pool
    queries = SlowQueryReport.perPoolStats.get(pool.getName());
    if (queries==null) {
        //create the map to hold our stats
        //however TODO we need to improve the eviction
        //selection
        queries = new ConcurrentHashMap<String,QueryStats>();
        if (perPoolStats.putIfAbsent(pool.getName(), queries)!=null) {
            //there already was one
            queries = SlowQueryReport.perPoolStats.get(pool.getName());
        }
    }
}
 
Example 14
Source Project: Tomcat7.0.67   Source File: TestSlowQueryReport.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testFastSql() throws Exception {
    int count = 3;
    this.init();
    this.datasource.setMaxActive(1);
    this.datasource.setJdbcInterceptors(SlowQueryReport.class.getName());
    Connection con = this.datasource.getConnection();
    String fastSql = this.datasource.getValidationQuery();
    for (int i=0; i<count; i++) {
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(fastSql);
        rs.close();
        st.close();
    }
    Map<String,SlowQueryReport.QueryStats> map = SlowQueryReport.getPoolStats(datasource.getPool().getName());
    Assert.assertNotNull(map);
    Assert.assertEquals(0,map.size());
    ConnectionPool pool = datasource.getPool();
    con.close();
    tearDown();
    Assert.assertNull(SlowQueryReport.getPoolStats(pool.getName()));
}
 
Example 15
Source Project: Tomcat7.0.67   Source File: PoolCleanerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPoolCleaner() throws Exception {
    datasource.getPoolProperties().setTimeBetweenEvictionRunsMillis(2000);
    datasource.getPoolProperties().setTestWhileIdle(true);
    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();
    Assert.assertEquals("Pool cleaner should have 1 cleaner.",1,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 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());


}
 
Example 16
Source Project: Tomcat7.0.67   Source File: PoolCleanerTest.java    License: Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 17
Source Project: Tomcat7.0.67   Source File: JmxPasswordTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.datasource.setDriverClassName(Driver.class.getName());
    this.datasource.setUrl("jdbc:tomcat:test");
    this.datasource.setPassword(password);
    this.datasource.setUsername(username);
    this.datasource.getConnection().close();
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    String domain = "tomcat.jdbc";
    Hashtable<String,String> properties = new Hashtable<String,String>();
    properties.put("type", "ConnectionPool");
    properties.put("class", this.getClass().getName());
    oname = new ObjectName(domain,properties);
    ConnectionPool pool = datasource.createPool();
    org.apache.tomcat.jdbc.pool.jmx.ConnectionPool jmxPool = new org.apache.tomcat.jdbc.pool.jmx.ConnectionPool(pool);
    mbs.registerMBean(jmxPool, oname);

}
 
Example 18
Source Project: dal   Source File: DefaultConnectionState.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void reset(ConnectionPool parent, PooledConnection con) {
    if (parent == null || con == null)
        return;

    try {
        boolean firstTime = isFirstTime.get();
        if (!firstTime)
            return;

        isFirstTime.set(false);
        String connectionName = con.toString();
        String url = con.getPoolProperties().getUrl();
        String poolName = parent.getName();
        String info = String.format("%s of url %s has been created for the first time,connection pool name:%s.",
                connectionName, url, poolName);
        logger.info(info);
    } catch (Throwable e) {
    }
}
 
Example 19
Source Project: tomcatsrc   Source File: PoolCleanerTest.java    License: Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 20
Source Project: Tomcat8-Source-Read   Source File: SlowQueryReportJmx.java    License: MIT License 5 votes vote down vote up
@Override
public void reset(ConnectionPool parent, PooledConnection con) {
    super.reset(parent, con);
    if (parent!=null) {
        poolName = parent.getName();
        pool = parent;
        registerJmx();
    }
}
 
Example 21
Source Project: Tomcat8-Source-Read   Source File: StatementCache.java    License: MIT License 5 votes vote down vote up
@Override
public void disconnected(ConnectionPool parent, PooledConnection con, boolean finalizing) {
    @SuppressWarnings("unchecked")
    ConcurrentHashMap<CacheKey,CachedStatement> statements =
        (ConcurrentHashMap<CacheKey,CachedStatement>)con.getAttributes().get(STATEMENT_CACHE_ATTR);

    if (statements!=null) {
        for (Map.Entry<CacheKey, CachedStatement> p : statements.entrySet()) {
            closeStatement(p.getValue());
        }
        statements.clear();
    }

    super.disconnected(parent, con, finalizing);
}
 
Example 22
Source Project: Tomcat8-Source-Read   Source File: SlowQueryReport.java    License: MIT License 5 votes vote down vote up
@Override
public void reset(ConnectionPool parent, PooledConnection con) {
    super.reset(parent, con);
    if (parent!=null)
        queries = SlowQueryReport.perPoolStats.get(parent.getName());
    else
        queries = null;
}
 
Example 23
Source Project: Tomcat8-Source-Read   Source File: ConnectionState.java    License: MIT License 5 votes vote down vote up
@Override
public void disconnected(ConnectionPool parent, PooledConnection con, boolean finalizing) {
    //we are resetting, reset our defaults
    autoCommit = null;
    transactionIsolation = null;
    readOnly = null;
    catalog = null;
    super.disconnected(parent, con, finalizing);
}
 
Example 24
Source Project: tomee   Source File: TomEEDataSourceCreator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ConnectionPool createPool() throws SQLException {
    if (pool != null) {
        return pool;
    } else {
        pool = new TomEEConnectionPool(poolProperties, Thread.currentThread().getContextClassLoader()); // to force to init the driver with TCCL
        return pool;
    }
}
 
Example 25
Source Project: das   Source File: DalTomcatDataSource.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ConnectionPool createPool() throws SQLException {
    if (pool != null) {
        return pool;
    } else {
        return pCreatePool();
    }
}
 
Example 26
Source Project: das   Source File: DalTomcatDataSource.java    License: Apache License 2.0 5 votes vote down vote up
private synchronized ConnectionPool pCreatePool() throws SQLException {
    if (pool != null) {
        return pool;
    } else {
        pool = new DalConnectionPool(poolProperties);
        return pool;
    }
}
 
Example 27
Source Project: Tomcat7.0.67   Source File: SlowQueryReportJmx.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void reset(ConnectionPool parent, PooledConnection con) {
    super.reset(parent, con);
    if (parent!=null) {
        poolName = parent.getName();
        pool = parent;
        registerJmx();
    }
}
 
Example 28
/**
 * Creates a mocked SQL connection, that looks like a tomcat-jdbc pooled connection.
 * @param username the username to use
 * @param password the password to use
 * @return the connection
 * @throws SQLException
 */
public static Connection create(String username, String password) throws SQLException {
    PoolProperties poolProps = new PoolProperties();
    poolProps.setUsername(username);
    poolProps.setPassword(password);
    poolProps.setDataSource(new MockDataSource());
    ConnectionPool pool = new ConnectionPool(poolProps);
    PooledConnection pooledConnection = new PooledConnection(poolProps, pool);
    pooledConnection.connect();
    ProxyConnection proxyConnection = new ProxyConnection(null, pooledConnection, true) {};
    DisposableConnectionFacade invocationHandler = new DisposableConnectionFacade(proxyConnection) {};
    Connection connection = (Connection) Proxy.newProxyInstance(DisposableConnectionFacade.class.getClassLoader(), new Class[] {Connection.class}, invocationHandler);
    return connection;
}
 
Example 29
Source Project: Tomcat7.0.67   Source File: StatementCache.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void reset(ConnectionPool parent, PooledConnection con) {
    super.reset(parent, con);
    if (parent==null) {
        cacheSize = null;
        this.pcon = null;
    } else {
        cacheSize = cacheSizeMap.get(parent);
        this.pcon = con;
        if (!pcon.getAttributes().containsKey(STATEMENT_CACHE_ATTR)) {
            ConcurrentHashMap<String,CachedStatement> cache = new ConcurrentHashMap<String, CachedStatement>();
            pcon.getAttributes().put(STATEMENT_CACHE_ATTR,cache);
        }
    }
}
 
Example 30
Source Project: Tomcat7.0.67   Source File: ConnectionState.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void disconnected(ConnectionPool parent, PooledConnection con, boolean finalizing) {
    //we are resetting, reset our defaults
    autoCommit = null;
    transactionIsolation = null;
    readOnly = null;
    catalog = null;
    super.disconnected(parent, con, finalizing);
}