Package in.partake.model.dao.postgres9

Source Code of in.partake.model.dao.postgres9.Postgres9ConnectionPool

package in.partake.model.dao.postgres9;

import in.partake.base.PartakeRuntimeException;
import in.partake.model.dao.DAOException;
import in.partake.model.dao.PartakeConnection;
import in.partake.model.dao.PartakeConnectionPool;
import in.partake.resource.ServerErrorCode;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import javax.sql.DataSource;

import play.Logger;
import play.db.DB;

public class Postgres9ConnectionPool extends PartakeConnectionPool {
    private final DataSource dataSource;

    public Postgres9ConnectionPool() {
        super();
        dataSource = DB.getDataSource();
    }

    @Override
    protected PartakeConnection getConnectionImpl(String name) throws DAOException {
        if (dataSource == null)
            throw new PartakeRuntimeException(ServerErrorCode.DB_CONNECTION_POOL_INITIALIZATION_FAILURE);

        long now = new Date().getTime();
        try {
            Connection con = dataSource.getConnection();
            con.setAutoCommit(false);
            return new Postgres9Connection(name, con, this, now);
        } catch (SQLException e) {
            throw new DAOException(e);
        }
    }

    @Override
    protected void releaseConnectionImpl(PartakeConnection connection) {
        assert (connection instanceof Postgres9Connection);
        if (!(connection instanceof Postgres9Connection)) {
            Logger.warn("connection is not Postgres9Connection.");
            return;
        }

        Postgres9Connection con = (Postgres9Connection) connection;
        try {
            con.close();
        } catch (DAOException e) {
            Logger.warn("Connection cannot be released.", e);
        }
    }

    @Override
    public void willDestroy() {
        // DO NOTHING.
    }
}
TOP

Related Classes of in.partake.model.dao.postgres9.Postgres9ConnectionPool

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.