Performance Zone is brought to you in partnership with:

Walid El Sayed Aly is originally from the land of the pyramids and pharaohs and is now living and working in Germany. He is a software developer whose passion is Java/JEE. His philosophy: think like a man of action, act like a man of thought. Walid has posted 1 posts at DZone. You can read more from them at their website. View Full User Profile

Logging with Spring JDBC and Craftsman Spy

06.17.2012
| 9996 views |
  • submit to reddit

It was very interesting when I used the framework from Craftsman. This Framework is very useful for the JDBC Logging.

Craftsman Spy is an open source and free framework for JDBC logging, it’s a JDBC driver implementation, you can download it from http://zer0.free.fr/craftsman/spy.php, and also you bind it in your local-Repository for Maven: how it’s work? I will explain it in my next article.

What are the benefits of craftsman Spy?

Spy logger logs all SQL connection and processing’s with executing spent time, all the stored procedures with arguments, all the batch processing’s and the result sets.  Craftsman SPY Framework is very helpful for all JunitTests, because you see which runs under the roof and all hidden processes.  You can’t find all this facilities in the Spring JDBC Framework, Pity!

How Spring-JDBC works without Spy framework?

You can use the standard JDBCTemplate from Spring like following configuration, which I took it from Spring Data access with JDBC:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="corporateEventDao" class="com.example.JdbcCorporateEventDao">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="jdbc.properties"/> 
</beans> 

 

 And now you can work the Data source like this:

public class JdbcCorporateEventDao implements CorporateEventDao {
   private JdbcTemplate jdbcTemplate;
   public void setDataSource(DataSource dataSource) {
       this.jdbcTemplate = new JdbcTemplate(dataSource);
   } }


How Spring-JDBC works with Spy framework

It’s very easy to bind the Craftsman Spy and the Spring JDBC framework.  You must only  bind the download SPY Jar’s from http://zer0.free.fr/craftsman/spy.php or use Maven to get the Jars from your local repository. Second you must only change the Spring-JDBC Configuration:

  1. Change the name of the  driverClassName to craftsman.spy.SpyDriver
  2. And you must also change the url to able Spy framework

Like that:

Before:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/></bean> 

After:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="craftsman.spy.SpyDriver"
<property name="url" value="jdbc:spy:mysql://server.com:5000/database"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/> </bean>

 

I hope it was a useful post. :) 

 

 

Published at DZone with permission of its author, Walid El Sayed Aly.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)