Scenario: For profiling/debugging of applications as they run in the Jboss container. Works with or without ORM/JPA, as long as datasources are managed by the container through JDBC driver.
#1. jars on classpath
#2. modify datasource
#3. configure container-level log4j
#4. (optional) command line options
#########################
#1 jars on classpath
#########################
Add the following to your classpath:
*log4jdbc[4-1.2alpha2].jar
*slf4j-api[-1.5.8].jar
*slf4j-log4j12[-1.5.8].jar
This can be done either in your JBOSS_HOME/server/<myserver>/lib folder, or if you use dynamic SAR deployment for your datasources, in the SAR/lib/ location.
#########################
#2 modify datasource
#########################
Modify your <datasource>-ds.xml file according to the log4jdbc documentation. For example:
=================================
<!-- production
<connectionurl>jdbc:sqlserver://myserver;DatabaseName=DB55</connection-url>
<driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
-->
<!-- profiling/debugging -->
<connection-url>jdbc:log4jdbc:sqlserver://myserver;DatabaseName=DB55</connection-url>
<driver-class>net.sf.log4jdbc.DriverSpy</driver-class>
=================================
##################
#3. configure container-level log4j
#################
Modify log4j.xml file at JBOSS_HOME/server/<myserver>/conf/log4j.xml according to normal log4jdbc documentation. Please note to make sure your appenders are all together at at the top of the log4j.xml file, and your category|loggers are all together.
See: http://code.google.com/p/log4jdbc/source/browse/trunk/doc/log4j.xml
##################
#4. (optional) command line options
#################
You can add the command line options in various locations. One place that you can add these options is in the JBOSS_HOME/bin/run.conf file by adding to the bottom of the file:
JAVA_OPTS="$JAVA_OPTS -Dlog4jdbc.debug.stack.prefix=com.myapp.daolayer"
###########################
In my experience, the sql-timing on DEBUG and the command-line stack.prefix gives me exactly the information I'm looking for!
----> 2009-08-05 12:00:00:00.000 com.myapp.SomeBean.getDataForSomething(SomeBean.java:44) 1. {call dbo.sproc_get_data(1)} {executed in 2 msec}
HTH,
-dhartford