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());
}
}