当前位置:Gxlcms >
数据库问题 >
源码分析之spring-JdbcTemplate日志打印sql语句
源码分析之spring-JdbcTemplate日志打印sql语句
时间:2021-07-01 10:21:17
帮助过:4人阅读
public <T> T execute(PreparedStatementCreator psc, PreparedStatementCallback<T>
action)
2 throws DataAccessException {
3
4 Assert.notNull(psc, "PreparedStatementCreator must not be null"
);
5 Assert.notNull(action, "Callback object must not be null"
);
6 if (logger.isDebugEnabled()) {
7 String sql = getSql(psc);
8 logger.debug("Executing prepared SQL statement" + (sql != null ? " [" + sql + "]" : ""));
9 }
10
11 Connection con =
DataSourceUtils.getConnection(getDataSource());
12 PreparedStatement ps =
null;
13 try {
14 Connection conToUse =
con;
15 if (
this.nativeJdbcExtractor !=
null &&
16 this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativePreparedStatements()) {
17 conToUse =
this.nativeJdbcExtractor.getNativeConnection(con);
18 }
19 ps =
psc.createPreparedStatement(conToUse);
20 applyStatementSettings(ps);
21 PreparedStatement psToUse =
ps;
22 if (
this.nativeJdbcExtractor !=
null) {
23 psToUse =
this.nativeJdbcExtractor.getNativePreparedStatement(ps);
24 }
25 T result =
action.doInPreparedStatement(psToUse);
26 handleWarnings(ps);
27 return result;
28 }
29 catch (SQLException ex) {
30 // Release Connection early, to avoid potential connection pool deadlock
31 // in the case when the exception translator hasn‘t been initialized yet.
32 if (psc
instanceof ParameterDisposer) {
33 ((ParameterDisposer) psc).cleanupParameters();
34 }
35 String sql =
getSql(psc);
36 psc =
null;
37 JdbcUtils.closeStatement(ps);
38 ps =
null;
39 DataSourceUtils.releaseConnection(con, getDataSource());
40 con =
null;
41 throw getExceptionTranslator().translate("PreparedStatementCallback"
, sql, ex);
42 }
43 finally {
44 if (psc
instanceof ParameterDisposer) {
45 ((ParameterDisposer) psc).cleanupParameters();
46 }
47 JdbcUtils.closeStatement(ps);
48 DataSourceUtils.releaseConnection(con, getDataSource());
49 }
50 }
以上我们说的只是个例子,其实这个猜一下也大概知道应该是日志级别的配置问题。通过这个小问题我只想说的是,有时候遇到问题,不一定急着去问朋友、同事,或者google,百度,何况google那么难上去。开源的程序的话自己看下源码,问题说不行就解决了。这样对自己来说好处多多
源码分析之spring-JdbcTemplate日志打印sql语句
标签: