JDBC proxy by via JNDI rebind not working; fixed by reordering listeners in web.xml

108 views
Skip to first unread message

Brad Fritz

unread,
Oct 20, 2011, 7:38:11 PM10/20/11
to javamelody

I just spent several hours debugging a problem where my app, that
retrieves its DataSource via JNDI, was bypassing the JavaMelody
JDBC proxy despite the DataSource being rebound. Hopefully this
will save the next person a few hours...

TL;DR:
The JavaMelody SessionListener should be listed *before* any Spring
ContextLoaderListener in web.xml. Otherwise, the monitored app may
grab a DataSource before JavaMelody has a chance to rebind it with
a proxied version. Obvious in hindsight but an easy mistake to make
and painful to debug.

Here is a bit more information for the archives...

Problem:
Monitored webapp was using DataSource from JNDI to make database
calls but the "Statistics sql" section remained empty except for
calls made via the "Database page at /monitoring?part=database .

Setup:
* Apache DBCP BasicDataSource registered in JNDI via Jetty's
org.mortbay.jetty.plus.naming.Resource class.

* Application uses Spring's JndiObjectFactoryBean to lookup
DataSource and wire it into LocalSessionFactoryBean for
Hibernate.

* Spring context loaded via ContextLoaderListener listener-class
in web.xml.

* JavaMelody JDBC proxy and JNDI rebinding initialized via
SessionListener listener-class in web.xml

* SessionListener *incorrectly listed after* ContextLoaderListener.

Fix:
Declare the web.xml <listener> for SessionListener *before* Spring's
ContextLoaderListener so JavaMelody can rebind the JNDI DataSource
before Spring creates a bean with the unproxied version.

It might be worthwhile to update section 2 of the UserGuide wiki page
to emphasize the <listener> ordering. Does not look like I can do
that
myself, but I will add a comment at the bottom of the page. (Would be
really nice if JavaMelody could detect that scenario automatically and
log a warning.)

--Brad

Brad Fritz
Software Developer
Indianapolis, IN

Emeric Vernat

unread,
Oct 21, 2011, 5:34:16 PM10/21/11
to javam...@googlegroups.com
Hi,

Thanks for the debugging. I have added a note about that in the user guide.

I think that you could also have used, with the same result,
"monitoring-spring.xml" as said in the user guide:
http://code.google.com/p/javamelody/wiki/UserGuide#7._JDBC

bye,
Emeric


Le 21/10/2011 01:38, Brad Fritz a �crit :

Reply all
Reply to author
Forward
0 new messages