JAMon监控SQL执行时间

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的执行时间了。

JAMon监控SQL执行时间,布布扣,bubuko.com

JAMon监控SQL执行时间

上一篇:用GDB调试程序(一)


下一篇:JDBC连接MySQL数据库及演示样例