Package com.carbonfive.db.jdbc.datasource

Source Code of com.carbonfive.db.jdbc.datasource.C3P0DataSourceFactory

package com.carbonfive.db.jdbc.datasource;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

public class C3P0DataSourceFactory implements DataSourceFactory
{
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    private String driver;
    private String url;
    private String username;
    private String password;

    public C3P0DataSourceFactory()
    {
    }

    public C3P0DataSourceFactory(String driver, String url, String username, String password)
    {
        this.driver = driver;
        this.url = url;
        this.username = username;
        this.password = password;
    }

    public void setDriver(String driver)
    {
        this.driver = driver;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public DataSource createDataSource(Object databaseKey)
    {
        String databaseName = databaseKey.toString();
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try
        {
            dataSource.setDriverClass(driver);
        }
        catch (PropertyVetoException e)
        {
            String msg = "Could not create data source with driver class '" + driver + "' for '" + databaseName + "'.";
            logger.error(msg, e);
            throw new DataSourceCreationException(msg, e);
        }
        dataSource.setJdbcUrl(url + databaseName);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
TOP

Related Classes of com.carbonfive.db.jdbc.datasource.C3P0DataSourceFactory

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.