longzenvo / log4jdbc

Automatically exported from code.google.com/p/log4jdbc
0 stars 0 forks source link

Support for DataSource #6

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
It would be nice if log4jdbc supported the javax.sql.DataSource api.

Original issue reported on code.google.com by jpsch...@gmail.com on 10 May 2009 at 3:39

GoogleCodeExporter commented 9 years ago
This is a real blocker for use as we would like to use this in our Tomcat 
datasources

Original comment by ps.gr...@gmail.com on 27 Apr 2010 at 7:12

GoogleCodeExporter commented 9 years ago
this is my implements: DataSourceSpy 

public class DataSourceSpy implements DataSource{
    private DataSource realDataSource;
    private boolean enabled = Boolean.parseBoolean(System.getProperty("log4jdbc.enabled","true"));

    public DataSourceSpy() {
    }

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

    public void setRealDataSource(DataSource realDataSource) {
        this.realDataSource = realDataSource;
    }    

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public Connection getConnection() throws SQLException {
        if(enabled) {
            return new ConnectionSpy(this.realDataSource.getConnection());
        }else {
            return this.realDataSource.getConnection();
        }
    }

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

    public int getLoginTimeout() throws SQLException {
        return realDataSource.getLoginTimeout();
    }

    public PrintWriter getLogWriter() throws SQLException {
        return realDataSource.getLogWriter();
    }

    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return realDataSource.isWrapperFor(iface);
    }

    public void setLoginTimeout(int seconds) throws SQLException {
        realDataSource.setLoginTimeout(seconds);
    }

    public void setLogWriter(PrintWriter out) throws SQLException {
        realDataSource.setLogWriter(out);
    }

    public <T> T unwrap(Class<T> iface) throws SQLException {
        return realDataSource.unwrap(iface);
    }

}

Original comment by bad...@gmail.com on 13 Aug 2010 at 5:12

GoogleCodeExporter commented 9 years ago
this is my DataSourceSpy patch. please intergate 

Original comment by bad...@gmail.com on 14 Aug 2010 at 4:05

Attachments:

GoogleCodeExporter commented 9 years ago
badqiu, could you post your jar file containing this patch?
Thanks

Original comment by franklam...@gmail.com on 24 Nov 2010 at 5:08

GoogleCodeExporter commented 9 years ago
My final implemention is :

/**
 * 
 * spring dataSource config:
 * <pre>
 * <bean id="dataSource" class="net.sf.log4jdbc.DataSourceSpy">
 *     <property name="realDataSource" ref="realDataSource"/>
 * </bean>
 * </pre>
 * 
 * log4j.properties
 * <pre>
 * log4j.logger.jdbc.sqlonly=OFF
 * log4j.logger.jdbc.sqltiming=INFO
 * log4j.logger.jdbc.audit=OFF
 * log4j.logger.jdbc.resultset=OFF
 * log4j.logger.jdbc.connection=OFF
 * </pre>
 * 
 * 
 * @author badqiu
 *
 */
public class DataSourceSpy implements DataSource{
    private DataSource realDataSource;
    private RdbmsSpecifics rdbmsSpecifics = null;

    public DataSourceSpy() {
    }

    public DataSourceSpy(DataSource realDataSource) {
        setRealDataSource(realDataSource);
    }

    public void setRealDataSource(DataSource realDataSource) {
        this.realDataSource = realDataSource;
    }    

    private RdbmsSpecifics getRdbmsSpecifics() throws SQLException {
        if(rdbmsSpecifics == null) {
            Connection conn = realDataSource.getConnection();
            rdbmsSpecifics = DriverSpy.getRdbmsSpecifics(conn);
            conn.close();
        }
        return rdbmsSpecifics;
    }

    public Connection getConnection() throws SQLException {
        if(SpyLogFactory.getSpyLogDelegator().isJdbcLoggingEnabled()) {
            return new ConnectionSpy(this.realDataSource.getConnection(),getRdbmsSpecifics());
        }else {
            return this.realDataSource.getConnection();
        }
    }

    public Connection getConnection(String username, String password) throws SQLException {
        if(SpyLogFactory.getSpyLogDelegator().isJdbcLoggingEnabled()) {
            return new ConnectionSpy(realDataSource.getConnection(username, password),getRdbmsSpecifics());
        }else {
            return realDataSource.getConnection(username, password);
        }
    }

    public int getLoginTimeout() throws SQLException {
        return realDataSource.getLoginTimeout();
    }

    public PrintWriter getLogWriter() throws SQLException {
        return realDataSource.getLogWriter();
    }

    public void setLoginTimeout(int seconds) throws SQLException {
        realDataSource.setLoginTimeout(seconds);
    }

    public void setLogWriter(PrintWriter out) throws SQLException {
        realDataSource.setLogWriter(out);
    }

}

attachments is my patch

Original comment by bad...@gmail.com on 24 Nov 2010 at 5:19

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks! will try it  out.

Original comment by franklam...@gmail.com on 5 Dec 2010 at 7:32

GoogleCodeExporter commented 9 years ago
Thanks, I will try this.

Original comment by ggb...@gmail.com on 20 Sep 2012 at 1:56

GoogleCodeExporter commented 9 years ago
c'mon this is a show stopper. any chance to have it merged to the project?

Original comment by butko...@gmail.com on 16 Aug 2013 at 1:30

GoogleCodeExporter commented 9 years ago
I suppose we could always fork.

Original comment by jpsch...@gmail.com on 16 Aug 2013 at 1:32

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
There are even two forks with spring datasource support:
https://code.google.com/p/log4jdbc-remix/
https://code.google.com/p/log4jdbc-log4j2/

Original comment by vkopiche...@gmail.com on 16 Oct 2013 at 8:19