My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Wiki pages
Links

About

Provide a datasource proxy class to intercept executing queries.

Features

  • obtain all executed query & parameter
  • obtain query execuion time
  • obtain total query execution time, num of database call, and num of queries
  • log all of the above obtained information
  • inject custom logic before/after all query execution
  • jdbc query metrics

How to use

maven

<dependency>
  <groupId>net.ttddyy</groupId>
  <artifactId>datasource-proxy</artifactId>
  <version>1.1</version>
</dependency>

>> sample application config

spring

<bean id="dataSource" class="net.ttddyy.dsproxy.support.ProxyDataSource">
  <property name="dataSource" ref="[ACTUAL DATASOURCE BEAN]"/>
  <property name="listener" ref="listeners"/>
</bean>

<bean id="listeners" class="net.ttddyy.dsproxy.listener.ChainListener">
  <property name="listeners">
    <list>
      <bean class="net.ttddyy.dsproxy.listener.CommonsQueryLoggingListener">
      <!-- <bean class="net.ttddyy.dsproxy.listener.SLF4JQueryLoggingListener"> -->
        <property name="logLevel" value="INFO"/> <!-- Default is DEBUG -->
      </bean>
      <bean class="net.ttddyy.dsproxy.listener.DataSourceQueryCountListener"/>
    </list>
  </property>
</bean>

>> more config samples

programmatic

DataSource

DataSource dataSource = (DataSource)((new InitialContext()).lookup("java:comp/env/ref/ds"));

ProxyDataSource proxyDS = new ProxyDataSource();
proxyDS.setDataSource(dataSource);
proxyDS.setListener(new CommonsQueryLoggingListener());
proxyDS.setDataSourceName("MyDataSource");

DriverManager

Class.forName("org.hsqldb.jdbcDriver");
Connection realConnection = DriverManager.getConnection("jdbc:hsqldb:mem:aname");
Connection proxyConnection = JdbcProxyFactory.createConnection(realConnection, new CommonsQueryLoggingListener());

>> more config samples

taglib (optional)

<%@ taglib prefix="dsp" uri="http://www.ttddyy.net/dsproxy/tags" %>

<dsp:metrics metric="select"/>  - Select
<dsp:metrics metric="update" dataSource="FOO" />  - Num of update queries for datasource FOO
<dsp:metrics metric="total"/>  - Total Queries
<dsp:metrics metric="call"/>  - Num of DB Call
<dsp:metrics metric="elapsedTime"/>  - Total TIme

Logging Example

query statistics metrics

DataSource:MyDatasourceA ElapsedTime:13 Call:7 Query:7 (Select:3 Insert:2 Update:1 Delete:0 Other:1)
DataSource:MyDatasourceB ElapsedTime:1 Call:1 Query:1 (Select:1 Insert:0 Update:0 Delete:0 Other:0)

query execution

Time:13, Num:1, Query:{[create table emp ( id integer primary key, name varchar(10) );][]}
Time:10, Num:1, Query:{[insert into emp ( id, name )values (?, ?);][1, foo]}
Time:1, Num:1, Query:{[select this_.id as id0_0_, this_.name as name0_0_, this_.value as value0_0_ from emp this_ where (this_.id=? and this_.name=?)][1,bar]}

Library Dependency

No dependencies to other libraries, everything is optional.

For example, if you use SLF4JQueryLoggingListener, then you need slf4j library.

Architecture

Powered by Google Project Hosting