ConnectionWrapper.java 2 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
 * Copyright (C) 2004 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.database;

import org.jivesoftware.database.AbstractConnection;
import org.jivesoftware.database.ConnectionPool;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * An implementation of the Connection interface that wraps an underlying
 * Connection object. It releases the connection back to a connection pool
 * when Connection.close() is called.
 *
 * @author Jive Software
 */
public class ConnectionWrapper extends AbstractConnection {

    public ConnectionPool pool;
    public boolean checkedout = false;
    public long createTime;
    public long lockTime;
    public long checkinTime;
    public Exception exception;
    public boolean hasLoggedException = false;

    public ConnectionWrapper(Connection connection, ConnectionPool pool) {
        super(connection);

        this.pool = pool;
        createTime = System.currentTimeMillis();
        lockTime = createTime;
        checkinTime = lockTime;
    }

    public void setConnection(Connection connection) {
        super.connection = connection;
    }

    /**
     * Instead of closing the underlying connection, we simply release
     * it back into the pool.
     */
    public void close() throws SQLException {
        synchronized (this) {
            checkedout = false;
            checkinTime = System.currentTimeMillis();
        }

        pool.freeConnection();

        // Release object references. Any further method calls on the connection will fail.
        // super.connection = null;
    }

    public String toString() {
        if (connection != null) {
            return connection.toString();
        }
        else {
            return "Jive Software Connection Wrapper";
        }
    }

    public synchronized boolean isCheckedOut() {
        return checkedout;
    }
}