This project contains a Java Agent that intercepts all calls to JDBC and logs statement executions.
To build this project simply invoke mvn package
.
This will create a self-contained jar file at target/sql-agent-1.0-SNAPSHOT.jar
.
To use the agent for your own programs just activate it using the -javaagent
option:
java -javaagent:/path/to/sql-agent-1.0-SNAPSHOT.jar ...
To use the agent with TomEE just set JAVA_OPTS
appropriatly:
export JAVA_OPTS=-javaagent:/path/to/sql-agent-1.0-SNAPSHOT.jar
Suppose you have this simple test program:
package foo.bar;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class AgentTest {
public static void main(String[] args) throws Exception {
Class.forName("org.h2.Driver");
try (Connection conn = DriverManager.getConnection("jdbc:h2:./test", "sa", "");
PreparedStatement preparedStatement = conn.prepareStatement("create table A (a varchar(32))")) {
Statement statement = conn.createStatement();
statement.execute("drop table a");
preparedStatement.execute();
}
}
}
The agent will then produce this output:
Nov 15, 2015 7:22:52 PM foo.bar.impl.SQLMonitorImpl notifyExecution INFORMATION: SQL drop table a took 6024845 ns Nov 15, 2015 7:22:52 PM foo.bar.impl.SQLMonitorImpl notifyExecution INFORMATION: SQL [create table A (a varchar(32))] took 1415393 ns