2
\$\begingroup\$

I wrote wrapper for Connection. It uses my Connection pool implementation. When the wrapper closes, the connection goes back to pool.

public class ConnectionWrap implements Connection {
    private Connection delegate;
    private ConnectionPool pool;

    public ConnectionWrap(Connection delegate, ConnectionPool pool) {
        this.delegate = delegate;
        this.pool = pool;
    }

    @Override
    public Statement createStatement() throws SQLException {
        return delegate.createStatement();
    }

    @Override
    public void setAutoCommit(boolean autoCommit) throws SQLException {
        delegate.setAutoCommit(autoCommit);
    }

    @Override
    public void commit() throws SQLException {
        delegate.commit();
    }

    @Override
    public void rollback() throws SQLException {
        delegate.rollback();
    }

    @Override
    public void close() throws SQLException {
        pool.putConnection(delegate);
 //and more other methods
       }
    }

Connection have over 20 methods and iam delegate it is all. Can this be done in a shorter way?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.