JAMon監控SQL執行時間

水之原發表於2014-06-13

JAMon監控web工程方法的呼叫效能 http://www.cnblogs.com/zfc2201/p/3786365.html

這往往篇文章主要告訴大家如何監控web方法呼叫時間,在這個基礎這上,如果我們想要監控sql的執行時間,需要增加如下的配置:

1.增加一個類,假設是com.allen.bookshop.common.MonitorDataSource

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.jamonapi.proxy.MonProxyFactory;

public class MonitorDataSource implements DataSource
{
    private DataSource realDataSource;

    public void setRealDataSource( DataSource realDataSource )
    {
        this.realDataSource = realDataSource;
    }

    public DataSource getRealDataSource()
    {
        return realDataSource;
    }

    public Connection getConnection() throws SQLException
    {
        // 表示由jamon來代理realDataSource返回的Connection
        return MonProxyFactory.monitor( realDataSource.getConnection() );
    }

    public Connection getConnection( String username, String password )
            throws SQLException
    {
        // 表示由jamon來代理realDataSource返回的Connection

        return MonProxyFactory.monitor( realDataSource.getConnection( username,
                password ) );
    }

    public PrintWriter getLogWriter() throws SQLException
    {
        return realDataSource.getLogWriter();
    }

    public int getLoginTimeout() throws SQLException
    {
        return realDataSource.getLoginTimeout();
    }

    public void setLogWriter( PrintWriter out ) throws SQLException
    {
        realDataSource.setLogWriter( out );
    }

    public void setLoginTimeout( int seconds ) throws SQLException
    {
        realDataSource.setLoginTimeout( seconds );
    }
}

 

2.對資料來源進行配置:

    <bean id="dataSource" class="com.allen.bookshop.common.MonitorDataSource" destroy-method="close">  
        <property name="realDataSource" ref="basicDataSource"/>  
    </bean>
   <bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@192.168.0.11:1521:orcl" />
        <property name="username" value="orcl" />
        <property name="password" value="orcl" />
        <property name="initialSize" value="20" />
        <property name="maxActive" value="50" />
        <property name="defaultAutoCommit" value="true" />   
    </bean>

至此,配置完成,現在可以訪問:http://localhost:8080/bookshop/jamon/sql.jsp檢視sql的執行時間了。

相關文章