Package com.javaeye.jert.domain.connection

Source Code of com.javaeye.jert.domain.connection.DBCPConnectionManager

package com.javaeye.jert.domain.connection;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.dbcp.BasicDataSource;

import com.javaeye.jert.domain.Database;

/**
* @author Quake Wang
* @since 2004-12-22
* @version $Revision: 1.2 $
*/
public class DBCPConnectionManager implements ConnectionManager {
    private Map cache = new HashMap();

    public Connection getConnection(Database db) throws SQLException {
        Long id = db.getId();
        BasicDataSource datasource = (BasicDataSource) cache.get(id);
        if(datasource == null){
            String driver = db.getDriver();
            String url = db.getUrl();
            String username = db.getUsername();
            String password = db.getPassword();
           
            datasource = new BasicDataSource();
            datasource.setDriverClassName(driver);
            datasource.setUrl(url);
            datasource.setUsername(username);
            datasource.setPassword(password);
            cache.put(id, datasource);
        }
        return datasource.getConnection();
    }

    public void clearCache(Database db) {
        cache.remove(db.getId());
    }

}
TOP

Related Classes of com.javaeye.jert.domain.connection.DBCPConnectionManager

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.