2017-06-20 480 views
1

我正在通過org.springframework.jdbc.core.JdbcTemplate正在編寫與db一起使用的應用程序。我需要記錄這些查詢的執行情況。所以我有一個有一定的JdbcTemplate邏輯方法,例如記錄JdbcTemplate查詢執行時間

List<Map<String, Object>> data = jdbcTemplate.queryForList(queryTables); 

我需要登錄該查詢的執行時間,但並不是所有的方法。 我可以通過毫秒減法來做到這一點。但是,如果我有1500種方法甚至更多,情況並非如此。那麼是否有任何註釋或其他方法可以幫助我記錄執行時間?

+0

JdbcTemplate在哪裏有它的起源?使用Aspects可能是一種選擇,但這會產生至少必須記住的開銷。 – Markus

+0

總之沒有。您可以使用@Markus建議的內容或查看數據庫本身。它可能允許你以編程方式 – efekctive

+0

林不知道什麼是計算時間的最佳方式,但我做了一個示例代碼,其中我計算多個方法的執行時間。希望它的工作原理:https://stackoverflow.com/questions/27112162/why-is-multiplied-many-times-faster-than-taking-the-square-root – xsami

回答

1
  1. 爲MESURE執行時間彈簧使用StopWatch,也apache的具有相同StopWatch。秒錶示例 - https://www.javacodegeeks.com/2012/08/measure-execution-time-in-java-spring.html

  2. 使用aop執行時間。這裏是示例https://veerasundar.com/blog/2010/01/spring-aop-example-profiling-method-execution-time-tutorial/

    使用aop和自定義註釋可以標記要記錄執行時間的方法。

AOP - 它沒有那麼大的開銷,當您使用@Transactional還使用AOP,所以你已經有這個overhad


如果要記錄僅dbcTemplate.queryForList執行時間()和夫婦,使用秒錶。或者你可以創建你自己的util方法(wrap dbcTemplate.queryForList()),如下所示:

public static List<?> queryForListWithLogExecution(String sql){ 
    List<?> result; 
    //measuring elapsed time using Spring StopWatch 
     StopWatch watch = new StopWatch(); 
     watch.start(); 
     list = jdbcTemplate.queryForList(queryTables); 
     watch.stop(); 
     logger.info("Total execution time in millis: {0}", watch.getTotalTimeMillis()); 
     return result; 
}