Skip to content

luohuaruxue/sql-agent

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQL Agent

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

Limitations

  • Not working yet on JBoss.

TODO

  • Collect and log parameters to PreparedStatements

About

A Java Agent that logs JDBC calls.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%