
com.alterioncorp.perfjdbc.sql.PerfDriver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of perf-jdbc Show documentation
Show all versions of perf-jdbc Show documentation
Wrapper JDBC driver that monitors time spent inside JDBC code by thread.
JDBC driver URL:
jdbc:alterion:perf://class=<TARGET_DRIVER_CLASS>|url=<TARGET_JDBC_URL>
To get the time in millis spent inside JDBC for thread:
StopWatch.getTime()
The newest version!
package com.alterioncorp.perfjdbc.sql;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Logger;
import com.alterioncorp.perfjdbc.PerfProxy;
import com.alterioncorp.perfjdbc.PerfProxyFactory;
import com.alterioncorp.perfjdbc.StopWatch;
public class PerfDriver implements Driver, PerfProxy {
static final char URL_DATA_SEPARATOR = '|';
static final String PROPERTY_CLASS = "class";
static final String PROPERTY_URL = "url";
static final String URL_PREFIX = "jdbc:alterion:perf://";
static final String URL_TEMPLATE = constructUrl("", "");
static {
try {
DriverManager.registerDriver(new PerfDriver());
}
catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static final String constructUrl(String targetClassName, String targetUrl) {
return
URL_PREFIX +
PROPERTY_CLASS + "=" + targetClassName +
URL_DATA_SEPARATOR +
PROPERTY_URL + "=" + targetUrl;
}
@Override
public Connection connect(String url, Properties info) throws SQLException {
Connection connection = null;
if (this.acceptsURL(url)) {
StopWatch.start();
try {
connection = DriverManager.getConnection(this.getTargetUrl(url), info);
}
finally {
StopWatch.stop();
}
connection = PerfProxyFactory.createProxy(Connection.class, connection);
}
return connection;
}
@Override
public boolean acceptsURL(String url) throws SQLException {
return url.startsWith(URL_PREFIX);
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
throws SQLException {
DriverPropertyInfo[] propInfos = null;
if (this.acceptsURL(url)) {
String targetUrl = this.getTargetUrl(url);
propInfos = DriverManager.getDriver(targetUrl).getPropertyInfo(targetUrl, info);
}
return propInfos;
}
@Override
public int getMajorVersion() {
return 1;
}
@Override
public int getMinorVersion() {
return 0;
}
@Override
public boolean jdbcCompliant() {
return true;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException();
}
String getTargetUrl(String url) throws SQLException {
String urlSuffix = url.substring(URL_PREFIX.length());
int startIndex = 0;
int endIndex = 0;
String targetUrl = null;
String targetClassName = null;
while (startIndex < urlSuffix.length()) {
endIndex = urlSuffix.indexOf(URL_DATA_SEPARATOR, startIndex);
if (endIndex < 0) {
endIndex = urlSuffix.length();
}
String nameValue = urlSuffix.substring(startIndex, endIndex);
int indexOfEquals = nameValue.indexOf('=');
if (indexOfEquals <= 0) {
throw new SQLException("URL does not match format: " + URL_TEMPLATE);
}
String name = nameValue.substring(0, indexOfEquals);
String value = nameValue.substring(indexOfEquals + 1);
if (name.equals(PROPERTY_CLASS)) {
targetClassName = value;
}
else if (name.equals(PROPERTY_URL)) {
targetUrl = value;
}
startIndex = endIndex + 1;
}
if (targetClassName == null || targetClassName.length() == 0) {
throw new SQLException("URL does not match format: " + URL_TEMPLATE);
}
if (targetUrl == null || targetUrl.length() == 0) {
throw new SQLException("URL does not match format: " + URL_TEMPLATE);
}
try {
Class.forName(targetClassName);
}
catch (ClassNotFoundException e) {
try {
Class.forName(targetClassName, true, Thread.currentThread().getContextClassLoader());
}
catch (ClassNotFoundException e2) {
throw new SQLException(e2);
}
}
return targetUrl;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy