2

I'm writing a method that tries to log db calls, form connecting to it, to after the query, there are many places that calls the method connect() to start and the cleanUp() method to end, I can't and don't want to modify every place. So the sequence is like this :

  Connection con = ...
  connect();
  s = con.createStatement();
  ResultSet rs = s.executeQuery(" select * from xyz ");
  rs.next();
  cleanUp();

There are many methods that uses this sequence, so how do I, somehow in cleanUp(), get the sql query string [ in this case : select * from xyz ] from all the methods that run their queries, is there a way to get that info from the "con", does the con object know what query it has just ran ?

3
  • 1
    Could you use a proper JDBC logging framework? Commented Oct 5, 2016 at 21:03
  • This article gives some examples how to monitor statements through a data source proxy. If you happen to be using Oracle, the driver also has some built-in diagnostic capabilities. Commented Oct 5, 2016 at 21:10
  • I like this answer the best, how to pick it as my choice ^_^ ?! Commented Oct 7, 2016 at 14:30

2 Answers 2

1

I suggest to you to use an aspect, AspectJ would be perfect for this case. With an aspect, you could trigger a behavior once your monitored method has been executed. Here are a couple examples you could check: http://www.yegor256.com/2014/06/01/aop-aspectj-java-method-logging.html and https://mathewjhall.wordpress.com/2011/03/31/tracing-java-method-execution-with-aspectj/

Sign up to request clarification or add additional context in comments.

Comments

1

You can get it in MySQL from Statement.toString().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.