2
votes

I am trying to integrate JDBC Monitoring in JavaMelody. I use Tomcat jdbc pooled connections. My setup is like this (simplified):

Properties props = new Properties();
props.setProperty("driver", "com.mysql.jdbc.Driver");

PoolConfiguration p = new PoolProperties();
p.setDriverClassName("net.bull.javamelody.JdbcDriver");
p.setDbProperties(props);
p.setUsername("myusername");
p.setPassword("mypassword");
p.setUrl(connectionParameters.getUrl());

org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();

dataSource.setPoolProperties(p);

(I prefer this setup over a <context> description in web.xml)

I get following Exception

java.sql.SQLException: No suitable driver found for jdbc:mysql://127.0.0.1:3306/mydatabase?autoReconnect=true&characterEncoding=UTF-8 at java.sql.DriverManager.getConnection(DriverManager.java:596) at java.sql.DriverManager.getConnection(DriverManager.java:187) at net.bull.javamelody.JdbcDriver.connect(JdbcDriver.java:83) at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:278) at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182) at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:701) at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:635) at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:486) at org.apache.tomcat.jdbc.pool.ConnectionPool.(ConnectionPool.java:144) at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:116) at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:103) at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:127) at  ... (my methods)

Without javamelody its running fine, javamelody in general is running fine.

What am I doing wrong? Any ideas?

1
Have you tried to add Class.forName("com.mysql.jdbc.Driver") ?evernat

1 Answers

0
votes

The class net.bull.javamelody.JdbcDriver supports to get a "driver" property to create a proxy connection here. I didn't experience the "no suitable driver" complaint with just the same code as you posted, just no errors, but not works as expected. My connections were not counted.

So I find any use cases from the repository and finally, I found this test code. Following is my working case:

import javax.sql.DataSource;
import net.bull.javamelody.JdbcWrapper;

PoolProperties p = new PoolProperties();
p.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
p.setDriverClassName("org.postgresql.Driver");
p.setUrl(getLoginUrl());

org.apache.tomcat.jdbc.pool.DataSource orgDataSource = new org.apache.tomcat.jdbc.pool.DataSource();
orgDataSource.setPoolProperties(p);

DriverManager.registerDriver(new net.bull.javamelody.JdbcDriver());
datasource = JdbcWrapper.SINGLETON.createDataSourceProxy(app_name, orgDataSource);

The trick in this code is JDBC wrapper.SINGLETON.createDataSourceProxy. It creates another pool object which proxies the original pool settings and handling the driver connection there.