use of org.apache.tomcat.jdbc.pool.ConnectionPool in project tomcat by apache.
the class TestSlowQueryReport method testSlowSqlJmx.
@Test
public void testSlowSqlJmx() throws Exception {
int count = 1;
Connection con = this.datasource.getConnection();
for (int i = 0; i < count; i++) {
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(superSlowSql);
rs.close();
st.close();
}
Map<String, SlowQueryReport.QueryStats> map = SlowQueryReport.getPoolStats(datasource.getPool().getName());
Assert.assertNotNull(map);
Assert.assertEquals(1, map.size());
String key = map.keySet().iterator().next();
SlowQueryReport.QueryStats stats = map.get(key);
System.out.println("Stats:" + stats);
ClientListener listener = new ClientListener();
ConnectionPool pool = datasource.getPool();
ManagementFactory.getPlatformMBeanServer().addNotificationListener(new SlowQueryReportJmx().getObjectName(SlowQueryReportJmx.class, pool.getName()), listener, null, null);
for (int i = 0; i < count; i++) {
PreparedStatement st = con.prepareStatement(superSlowSql);
ResultSet rs = st.executeQuery();
rs.close();
st.close();
}
System.out.println("Stats:" + stats);
for (int i = 0; i < count; i++) {
CallableStatement st = con.prepareCall(superSlowSql);
ResultSet rs = st.executeQuery();
rs.close();
st.close();
}
System.out.println("Stats:" + stats);
Assert.assertEquals("Expecting to have received " + (2 * count) + " notifications.", 2 * count, listener.notificationCount.get());
con.close();
tearDown();
//make sure we actually did clean up when the pool closed
Assert.assertNull(SlowQueryReport.getPoolStats(pool.getName()));
}
use of org.apache.tomcat.jdbc.pool.ConnectionPool in project tomcat by apache.
the class JmxPasswordTest method setUp.
@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);
}
use of org.apache.tomcat.jdbc.pool.ConnectionPool in project dropwizard by dropwizard.
the class ManagedPooledDataSource method start.
@Override
public void start() throws Exception {
final ConnectionPool connectionPool = createPool();
metricRegistry.register(name(getClass(), connectionPool.getName(), "active"), (Gauge<Integer>) connectionPool::getActive);
metricRegistry.register(name(getClass(), connectionPool.getName(), "idle"), (Gauge<Integer>) connectionPool::getIdle);
metricRegistry.register(name(getClass(), connectionPool.getName(), "waiting"), (Gauge<Integer>) connectionPool::getWaitCount);
metricRegistry.register(name(getClass(), connectionPool.getName(), "size"), (Gauge<Integer>) connectionPool::getSize);
}
use of org.apache.tomcat.jdbc.pool.ConnectionPool in project dropwizard by dropwizard.
the class CloseableLiquibaseTest method testWhenClosingAllConnectionsInPoolIsReleased.
@Test
public void testWhenClosingAllConnectionsInPoolIsReleased() throws Exception {
ConnectionPool pool = dataSource.getPool();
assertThat(pool.getActive()).isEqualTo(1);
liquibase.close();
assertThat(pool.getActive()).isZero();
assertThat(pool.getIdle()).isZero();
assertThat(pool.isClosed()).isTrue();
}
use of org.apache.tomcat.jdbc.pool.ConnectionPool in project tomcat by apache.
the class Bug51582 method main.
public static void main(String[] args) throws SQLException {
org.apache.tomcat.jdbc.pool.DataSource datasource = null;
PoolConfiguration p = new DefaultProperties();
p.setJmxEnabled(true);
p.setTestOnBorrow(false);
p.setTestOnReturn(false);
p.setValidationInterval(1000);
p.setTimeBetweenEvictionRunsMillis(2000);
p.setMaxWait(2000);
p.setMinEvictableIdleTimeMillis(1000);
datasource = new org.apache.tomcat.jdbc.pool.DataSource();
datasource.setPoolProperties(p);
datasource.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReportJmx(threshold=200)");
ConnectionPool pool = datasource.createPool();
Connection con = pool.getConnection();
Statement st = con.createStatement();
try {
st.execute("DROP ALIAS SLEEP");
} catch (Exception ignore) {
// Ignore
}
st.execute("CREATE ALIAS SLEEP AS $$\nboolean sleep() {\n try {\n Thread.sleep(10000);\n return true; } catch (Exception x) {\n return false;\n }\n}\n$$;");
st.close();
con.close();
int iter = 0;
while ((iter++) < 10) {
final Connection connection = pool.getConnection();
final CallableStatement s = connection.prepareCall("{CALL SLEEP()}");
List<Thread> threadList = new ArrayList<>();
for (int l = 0; l < 3; l++) {
final int i = l;
Thread thread = new Thread() {
@Override
public void run() {
try {
if (i == 0) {
Thread.sleep(1000);
s.cancel();
} else if (i == 1) {
// or use some other statement which will block
// for a longer time
long start = System.currentTimeMillis();
System.out.println("[" + getName() + "] Calling SP SLEEP");
s.execute();
System.out.println("[" + getName() + "] Executed SP SLEEP [" + (System.currentTimeMillis() - start) + "]");
} else {
Thread.sleep(1000);
connection.close();
}
} catch (InterruptedException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
};
threadList.add(thread);
thread.start();
}
for (Thread t : threadList) {
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Aggregations