源码分析之spring-JdbcTemplate日志打印sql语句
对于开源的项目来说的好处就是我们遇到什么问题可以通过看源码来解决。
比如近期有个同事问我说,为啥JdbcTemplate中只有在Error的时候才打印出sql语句呢。我一想,这和log的配置有关系吧。 我们的系统中使用了slf4j作为日志管理工具,之前也好像看到过项目工程中配置的日志级别是error的,所以当代码错误时打印出sql语句应该也属于正常。但是想要正常运行时也打印出sql语句,相比和配置有关,但是应该配置那个级别呢? 应该要看下JdbcTemplate的源码怎么写的,这样可快速定位配置那个日志级别(当然你可以一个一个的试)。在maven工程内看源码及其方便(个人认为用maven的唯一好处)。
在eclipse中通过快捷键(windows系统中默认快捷键 ctrl+shift+T)打开Open Type窗口,输入JdbcTemplate后自动搜索到该类后点击进入,如果未下载过源码,maven会自动下载。在源码中找到我们经常用的execute(PreparedStatementCreator psc, PreparedStatementCallback<T> action)这个方法,然后看如下源码的第6、7、8行,使用了if(logger.isDebugEnabled)这个判断,意思是如果logger的日志级别为debug的,那么进入这个语句块,第7行获取sql的内容,第八行通过logger.debug将内容输出。由此可见我们需要配置debug级别的。然后在logger的配置文件中error修改为debug,正常输出sql语句。
public <T> T execute(PreparedStatementCreator psc, PreparedStatementCallback<T> action)
throws DataAccessException { Assert.notNull(psc, "PreparedStatementCreator must not be null");
Assert.notNull(action, "Callback object must not be null");
if (logger.isDebugEnabled()) {
String sql = getSql(psc);
logger.debug("Executing prepared SQL statement" + (sql != null ? " [" + sql + "]" : ""));
} Connection con = DataSourceUtils.getConnection(getDataSource());
PreparedStatement ps = null;
try {
Connection conToUse = con;
if (this.nativeJdbcExtractor != null &&
this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativePreparedStatements()) {
conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
}
ps = psc.createPreparedStatement(conToUse);
applyStatementSettings(ps);
PreparedStatement psToUse = ps;
if (this.nativeJdbcExtractor != null) {
psToUse = this.nativeJdbcExtractor.getNativePreparedStatement(ps);
}
T result = action.doInPreparedStatement(psToUse);
handleWarnings(ps);
return result;
}
catch (SQLException ex) {
// Release Connection early, to avoid potential connection pool deadlock
// in the case when the exception translator hasn't been initialized yet.
if (psc instanceof ParameterDisposer) {
((ParameterDisposer) psc).cleanupParameters();
}
String sql = getSql(psc);
psc = null;
JdbcUtils.closeStatement(ps);
ps = null;
DataSourceUtils.releaseConnection(con, getDataSource());
con = null;
throw getExceptionTranslator().translate("PreparedStatementCallback", sql, ex);
}
finally {
if (psc instanceof ParameterDisposer) {
((ParameterDisposer) psc).cleanupParameters();
}
JdbcUtils.closeStatement(ps);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
以上我们说的只是个例子,其实这个猜一下也大概知道应该是日志级别的配置问题。通过这个小问题我只想说的是,有时候遇到问题,不一定急着去问朋友、同事,或者google,百度,何况google那么难上去。开源的程序的话自己看下源码,问题说不行就解决了。这样对自己来说好处多多
源码分析之spring-JdbcTemplate日志打印sql语句的更多相关文章
- mybatis日志,打印sql语句,输出sql
mybatis日志,打印sql语句,输出sql<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE ...
- Spring源码分析:Spring IOC容器初始化
概述: Spring 对于Java 开发来说,以及算得上非常基础并且核心的框架了,在有一定开发经验后,阅读源码能更好的提高我们的编码能力并且让我们对其更加理解.俗话说知己知彼,百战不殆.当你对Spri ...
- Springboot源码分析之Spring循环依赖揭秘
摘要: 若你是一个有经验的程序员,那你在开发中必然碰到过这种现象:事务不生效.或许刚说到这,有的小伙伴就会大惊失色了.Spring不是解决了循环依赖问题吗,它是怎么又会发生循环依赖的呢?,接下来就让我 ...
- logback 打印mybatis sql mybatis 日志打印sql语句和返回结果
logback 打印sql语句: 在logback日志文件中开启debug模式 <logger name="com.ibatis" level="DEBUG&quo ...
- ------- Tor 源码分析第三部分—— 日志设施与智能链表 --------
------------------------------------------------------------------------------------ init_logging()( ...
- 【spring源码分析】spring关于循环依赖的问题
引言:循环依赖就是N个类中循环嵌套引用,如果在日常开发中我们用new 对象的方式发生这种循环依赖的话程序会在运行时一直循环调用,直至内存溢出报错.下面说一下Spring是如果解决循环依赖的. 第一种: ...
- 【spring源码分析】spring ioc容器之前生今世--DefaultListableBeanFactory源码解读
spring Ioc容器的实现,从根源上是beanfactory,但真正可以作为一个可以独立使用的ioc容器还是DefaultListableBeanFactory,因此可以这么说, DefaultL ...
- SpringBoot源码分析:spring的基本架构
在深入了解springboot之前,我们需要了解spring,springboot本身就是基于spring而构建:是微服务架构中一个比较流行的框架:类似spring提供了一套完整的微服务方案如spri ...
- spring源码分析之spring注解@Aspect是如何工作的?
1.@Aspect 在xml定义:<aop:aspectj-autoproxy />,其定义在http://www.springframework.org/schema/aop/sprin ...
随机推荐
- (Relax 数论1.8)POJ 1284 Primitive Roots(欧拉函数的应用: 以n为模的本原根的个数phi(n-1))
/* * POJ_2407.cpp * * Created on: 2013年11月19日 * Author: Administrator */ #include <iostream> # ...
- IT江湖
我喜欢看武侠电影,尤其的70-80年代的邵氏电影,在这个期间,邵氏公司将金庸和古老很多小说都改拍成了电影,可以说,看这些电影是一种享受,真的! 对于现实中的IT世界,也像是一个江湖,当你掌握了一些技能 ...
- hdu1869六度分离(dijkstra)
Problem Description 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象(small world phenomenon)”的著名假说,大意是说,任何2个素不相 ...
- UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))
Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...
- [React Testing] Conditional className with Shallow Rendering
Often our components have output that shows differently depending on the props it is given; in this ...
- [Javascript] Array methods in depth - some
some returns a boolean value after passing each item in the source array through the test function t ...
- Android 屏幕适配方案
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/45460089: 本文出自:[张鸿洋的博客] 1.概述 大家在Android开发 ...
- C语音指针Introduction.
指针是C语言中广泛使用的一种数据类型. 运用指针编程是C语言最主要的风格之一.利用指针变量可以表示各种数据结构: 能很方便地使用数组和字符串: 并能象汇编语言一样处理内存地址,从而编出精练而高效的程序 ...
- Handler Looper 原理 详解
演示代码 public class MainActivity extends ListActivity { private TextView tv_info; private CalT ...
- 解决SQL Server 占用80端口
停用掉下面的服务就可以了: