A note about DataSources

185 views
Skip to first unread message

David

unread,
Jun 8, 2009, 7:13:06 AM6/8/09
to log4jdbc
Arthur:

I had a need for support to enable my JPA application to retrieve the
SQL sent to the database (never mind why - you wouldn't believe it if
I told you). I found the following to work OK:

public class SpiedUponDataSource implements DataSource
{
private DataSource realDataSource;

public SpiedUponDataSource(DataSource realDataSource)
{
this.realDataSource = realDataSource;
}

public Connection getConnection() throws SQLException
{
return new ConnectionSpy(realDataSource.getConnection());
}

public Connection getConnection(String username, String password)
throws SQLException
{
return new ConnectionSpy(realDataSource.getConnection(username,
password));
}

// ... all other methods simply delegate to realDataSource

In Spring, this is very simple to set up (I'm retrieving my datasource
from Tomcat JNDI):

<bean id="dataSource"
class="com.datasourceinc.sats.util.SpiedUponDataSource">
<constructor-arg>
<bean class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/testDS"/>
</bean>
</constructor-arg>
</bean>

I don't like the class name, but otherwise, this seems to work just
fine. Now I have to figure out how to configure SLF4J to just return
the SQL statement to me (the one with the substituted values - really
like that feature!). Any advice about that, perchance?

David Sills

Arthur Blake

unread,
Jun 8, 2009, 9:40:52 AM6/8/09
to log4...@googlegroups.com
You could use the patch provided in
http://code.google.com/p/log4jdbc/issues/detail?id=5

I will probably include this patch or some variation of it in the next
release (whenever I get around to working on that.)

See http://groups.google.com/group/log4jdbc/browse_thread/thread/21e97eda7a48b033
for discussion.

Then write your own SpyLogDelegator instance. This way you can do
anything you want with the SQL. If you still want to log SQL to
SLF4J, your SpyLogDelegator could extend the Slf4jSpyLogDelegator and
just override the method(s) you are interested in, calling super at
the beginning of the methods.
Reply all
Reply to author
Forward
0 new messages