Java – how does the spring jdbctemplate record exception parameters?
Using spring's jdbctemplate, I've been trying to find a concise way to record exceptions in the Dao layer, but I can't seem to understand it I want to record the SQL statements and parameters used
For example, addstoresql is a parameterized statement
public int addStore(Store store) { return jdbcTemplate.update(addStoresql,store.getId(),store.getName()); }
What I did
public int addStore(Store store) { try{ return jdbcTemplate.update(addStoresql,store.getName()); } catch (DataAccessException ex) { logger.error("exception on deleting store - " + store.toString(),ex); throw ex; } }
My question is, is there any way to write any cleaning tools in many Dao methods? Maybe at the logger level or some spring libraries? Or is this the cleanest way (or is the code above even bad)?
I have several basically the same methods, accept an object, pass the field to the query and return the result
Solution
The difficulty of doing this with spring is that the JDBC object from which you want to get this information is not managed by spring, but created by the driver Therefore, spring AOP is not applicable (AspectJ is not used)
If you record the category "org. Springframework. JDBC. Core. Jdbctemplate" at the debug level and "org. Springframework. JDBC. Core. Statementcreator utils" at the trace level, spring can provide you with queries and parameters separately
The existing libraries log4 JDBC and p6spy implement the wrapper of jdbc driver to generate SQL statements with insertion parameters See this question. Using either of these should be to add jars to the project, change the JDBC URL to point to the wrapper, and adjust the logging to get the required level of information
The existing logging code is not good because it is a duplicate cut-n-paste code and will cause exceptions to be logged multiple times Logs will be harder to read and will scroll more frequently