Source code Analysis of spring-jdbctemplate log print SQL statement

Source: Internet
Author: User
Tags assert

The benefit of open source projects is that we are faced with what problems can be solved by looking at the source code.

For example, a colleague recently asked me why the SQL statements were only printed when the error was jdbctemplate. I think, this is related to the configuration of log. We used slf4j as a log management tool in our system, and it was as if we had seen that the log level configured in Project engineering was error, so it should be normal to print out the SQL statement when the code was wrong. But if you want to print out SQL statements while you're working, you should configure that level instead of configuring it. Should look at the jdbctemplate of the source code how to write, so you can quickly locate the configuration of the log level (of course you can try one). Look at the source code and its convenience within the MAVEN project (the only benefit that the individual sees with Maven).

Open the Open Type window with the shortcut key (default shortcut key ctrl+shift+t in Windows system) in Eclipse, click Enter after entering JdbcTemplate, and if the source has not been downloaded, maven will download it automatically. Find in the source code we often use the Execute (PreparedStatementCreator psc, preparedstatementcallback<t> Action) This method, and then see the following source code 6th, 7, 8 lines, Using the IF (logger.isdebugenabled) decision, meaning that if the logger log level is debug, then enter this block of statements, the 7th row gets the contents of SQL, and the eighth line outputs the content through Logger.debug. This shows that we need to configure the debug level. Then in the logger configuration file, error is modified to debug, the normal output SQL statement.

1  Public<T> T Execute (PreparedStatementCreator psc, preparedstatementcallback<t>action)2             throwsDataAccessException {3 4Assert.notnull (PSC, "PreparedStatementCreator must not is null");5Assert.notnull (Action, "Callback object must not is null"); 6if  (logger.isdebugenabled ()) {  7 String sql = getsql (PSC); 8 Logger.debug ("Executing prepared SQL statement" + (SQL! = null ?) "[" + SQL + "]": ")); 9 }          Ten  OneConnection con =datasourceutils.getconnection (Getdatasource ()); APreparedStatement PS =NULL; -         Try { -Connection Contouse =con; the             if( This. nativejdbcextractor! =NULL&& -                      This. Nativejdbcextractor.isnativeconnectionnecessaryfornativepreparedstatements ()) { -Contouse = This. Nativejdbcextractor.getnativeconnection (con); -             } +PS =psc.createpreparedstatement (contouse); - Applystatementsettings (PS); +PreparedStatement Pstouse =PS; A             if( This. nativejdbcextractor! =NULL) { atPstouse = This. Nativejdbcextractor.getnativepreparedstatement (PS); -             } -T result =action.doinpreparedstatement (pstouse); - Handlewarnings (PS); -             returnresult; -         } in         Catch(SQLException ex) { -             //Release Connection Early, to avoid potential Connection pool deadlock to             //In the case when the exception translator hasn ' t been initialized yet. +             if(PSCinstanceofParameterdisposer) { - ((Parameterdisposer) PSC). Cleanupparameters (); the             } *String sql =GetSQL (PSC); $PSC =NULL;Panax Notoginseng Jdbcutils.closestatement (PS); -PS =NULL; the datasourceutils.releaseconnection (Con, Getdatasource ()); +Con =NULL; A             ThrowGetexceptiontranslator (). Translate ("PreparedStatementCallback", SQL, ex); the         } +         finally { -             if(PSCinstanceofParameterdisposer) { $ ((Parameterdisposer) PSC). Cleanupparameters (); $             } - Jdbcutils.closestatement (PS); - datasourceutils.releaseconnection (Con, Getdatasource ()); the         } -}

The above is just an example, in fact, this guess also probably know that should be the log level configuration issues. Through this small problem I just want to say is, sometimes encountered problems, not necessarily anxious to ask friends, colleagues, or Google, Baidu, not to mention Google so difficult to go up. Open source program to look at the source code, the problem said no can solve. It's a lot of good for you.

Source code Analysis of spring-jdbctemplate log print SQL statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.