Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Logging with Spring JDBC and Craftsman Spy

DZone's Guide to

Logging with Spring JDBC and Craftsman Spy

Free Resource

Transform incident management with machine learning and analytics to help you maintain optimal performance and availability while keeping pace with the growing demands of digital business with this eBook, brought to you in partnership with BMC.

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. :) 

 

 

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}