Welcome!

Java Authors: Chris Muir, Marc Chanliau, Navdeep Sidhu, Brian McCallion, Yakov Fain

Related Topics: Java, SOA & WOA, Apache

Java: Article

Logging with Spring JDBC and Craftsman Spy

What are the benefits of Craftsman Spy?

I recently used the framework by Craftsman Spy and must say it was really exciting. This framework is very useful for JDBC logging.

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

What are the benefits of Craftsman Spy?
Spy logger logs all SQL processing and connections with executing spent time, all stored procedures with arguments, all batch processing and result sets.  Craftsman Spy Framework is very helpful for all JUnitTests because you see what runs under the roof and all hidden processes.  Unfortunately, you cannot find all these facilities in the Spring JDBC Framework.

How does Spring JDBC work without the Spy framework?
You can use the standard JDBC template from Spring like the following configuration that I took 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>

Now you work with the data source, e.g.:

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

}

How does Spring JDBC work with the Spy framework?
It is rather simple to bind the Craftsman Spy and the Spring JDBC frameworks together.  You only have to bind the downloaded SPY jars from http://zer0.free.fr/craftsman/spy.php or you use Maven to get the jars from your local repository. Secondly, you only change the Spring JDBC configuration:

  1. Change the name of the DriverClassName to craftsman.spy.SpyDriver.
  2. Also change the URL to activate the Spy framework.

Just 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 this was a useful piece of advice for you.

More Stories By Walid El Sayed Aly

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. His philosophy: think like a man of action, act like a man of thought.