Intercept SQL s on a custom JDBC

Asked

Viewed 92 times

3

You can create a custom implementation based on JDBC to intercept all sql s executed in the application?

I did not find anything very concrete about this in Google, I made a simple test just inheriting the interface of the driver I wanted and with that I made the connection to the bank and the querys perform perfectly but I still did not find how to intercept what was executed, if anyone has any idea or example that can help decipher the riddle, or what the best way to do so, or if it is impossible.

Here is an example of the simple interface:

package br.com.jdbc;
import org.firebirdsql.jdbc.FirebirdDriver;

public interface FRDriver extends FirebirdDriver {


}

Test file:

package br.com.jdbc;

import java.sql.DriverManager;

import org.firebirdsql.jdbc.FBConnection;

public class Teste {
    public static void main(String[] args) throws Exception {
        Class.forName("br.com.jdbc.FRDriver");
        System.out.println("Connecting to database...");

        FBConnection conn = (FBConnection) DriverManager.getConnection("jdbc:firebirdsql:localhost:BANCOTESTE?encoding=WIN1252","SYSDBA","masterkey");
        System.out.println("CONEXAO OK");
        conn.close();
        System.out.println("CONEXAO ENCERRADA");
    }
}
  • No, actually the connection uses the interface FRDriver which would be the driver inherited from Firebird.

  • Is the project java web? if you can use Interceptors, there are several ways, either with spring, cdi, etc... this link can help you: http://www.devmedia.com.br/trabando-com-o-design-pattern-interceptor-no-java-ee/32842

  • @Geferson I’ll take a look at the link to see if it opens any idea here the project would be in web yes with spring.

  • Here are some examples of interceptors with spring mvc, I think it should help you: http://www.concretepage.com/spring/spring-mvc/spring-handlerineptor-annotation-example-webmvcconfigureradapter

  • @Geferson what I need is more like this http://www.rgagnon.com/javadetails/java-0602.html, but without an external library, I don’t know if only the Interceptor could. But I’ll try anyway.

  • What would be your goal exactly? generate sql log and save them?

  • At first it would be this, but if everything goes well run the Inserts, delete and updates in another database.

  • With cdi(spring should also have), I used a @Aroundinvoke to intercept the methods that did something in the bank, and injected a transaction into it, the operation of this case I think would solve your problem. the rest would implement the Interceptor.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.