simplest way to collect all SQL sent out

2020-06-27 09:08发布

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?

2条回答
孤傲高冷的网名
2楼-- · 2020-06-27 09:30

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.

查看更多
该账号已被封号
3楼-- · 2020-06-27 09:51

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.

查看更多
登录 后发表回答