前言
前面讲过了,这里简单的介绍下,如何使用Javamelody来监控JDBC以及SQL。
手码不易,转载请注明:
在网上搜索很多资料,仅有开源社区上的两篇帖子有点帮助,但对于监控SQL还是有很多问题,有不少的网友遇到了跟我同样的问题,监控页面打开可就是监控不到数据,SQL一栏无论如何都是0,要不就是NaN。
这个问题其实还是因为数据源的部分没有配置正确,这里介绍两种配置的方式。
第一种,直接配置数据源,添加额外的jdbc驱动
按照UserGuide的文档来说,可以使用Jndi配置数据源的方式,比如如果使用Hibernate,那么在hinernate.cfg.xml中配置
net.bull.javamelody.JdbcDriver com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/myschema myuser mypassword
注意这个地方,可能一般的hibernate.cfg.xml参数并不是像上面的配置,不要紧。
只要保证原有的connection.driver是真是的驱动,上面添加一个参数connection.driver_class是javamelody的那个jdbc驱动即可。即参考我下面诶之oracle的hibernate数据源文件
net.bull.javamelody.JdbcDriver oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@localhost:1521:orcl true gbk test test org.hibernate.connection.C3P0ConnectionProvider 20 1 120 0 60 2 true select sysdate from dual 120 1800 true org.hibernate.dialect.Oracle9Dialect update true true
参考上面这样的配置,就可以了。打开监控页面,就可以看到SQL相关的参数了。
另一种呢,就是使用spring,如果使用spring,是不需要额外设置驱动类的。
前提是,必须在加载web.xml的时候指定加载的spring配置文件。需要在web.xml中实现一个监听,这个监听回使web应用在读取web.xml时,加载指定的spring文件。
org.springframework.web.context.ContextLoaderListener
然后我们通过设置参数,设置启动的spring文件
contextConfigLocation classpath:net/bull/javamelody/monitoring-spring.xml classpath:context/services.xml classpath:context/data-access-layer.xml /WEB-INF/applicationContext.xml
注意monitoring-spring.xml与applicaitonContext.xml的位置,我好多次使用这种方式都没有成功,貌似就是这个位置的顺序颠倒了。是否是这个原因,还有待验证(明天测试,现在没有环境)。
整个web.xml的配置文件,参考下面:
contextConfigLocation classpath:net/bull/javamelody/monitoring-spring.xml classpath:bean.xml struts org.apache.struts2.dispatcher.FilterDispatcher struts.action.extension action struts /* monitoring net.bull.javamelody.MonitoringFilter log true monitoring /* net.bull.javamelody.SessionListener org.springframework.web.context.ContextLoaderListener index.jsp
另外,根据官方文档,如果你的应用与monitoring-spring.xml或者AOP之类的有冲突,那么使用monitoring-spring-datasource.xml文件替代monitoring-spring.xml就可以了,这个文件仅仅包含一个datasource的发送进程以及SpringDataSourceFactoryBean的一个例子。
手码不易,转载请注明:xingoo