simplest way to collect all SQL sent out

2020-06-27 09:23发布

问题:

For many reasons I prefer not to disclose (long and boring story), I need to capture the interactions of a complex application with the Database. The application builds on top of Spring/JdbcTemplate and I need to find all the SQL sent out by this application. How can I do that in the simplest possible way?

Creating a pseudo mock implementation of JdbcTemplate does not seem reasonable. First off JdbcTemplate is a class and not an interface. Second it has a large interface that makes it tedious to implement. I am thinking along the lines of mocking DataSource and Connection to get all the SQL sent out, but maybe there is an easier way to do this?

回答1:

This kind of problem is very neatly solved be eg. P6Spy. There is a good article on how to get it working with Spring.

Hope that helps.



回答2:

The only way to capture all SQL going out is to be the touch point between your application and the database. This means, decorating the DataSource, Connection, and all types of Statements from a library implementation that actually handles the JDBC interactions, and note down all the statements being run on your decorated connection from your decorated data source specified in the SimpleJdbcTemplate bean definition. It would be challenging from a maintainability perspective to capture this at any other point.