001package org.kuali.ole.docstore.common.util;
002
003import java.beans.PropertyVetoException;
004import java.io.IOException;
005import java.sql.Connection;
006import java.sql.SQLException;
007
008import org.apache.commons.dbcp.BasicDataSource;
009import org.kuali.rice.core.api.config.property.ConfigContext;
010
011
012/**
013 * Created with IntelliJ IDEA.
014 * User: jayabharathreddy
015 * Date: 6/4/14
016 * Time: 3:36 PM
017 * To change this template use File | Settings | File Templates.
018 */
019public class DataSource {
020    private static DataSource datasource;
021    private BasicDataSource ds;
022
023    private DataSource() throws IOException, SQLException, PropertyVetoException {
024
025        String dbVendor = ConfigContext.getCurrentContextConfig().getProperty("db.vendor");
026        String connectionUrl = ConfigContext.getCurrentContextConfig().getProperty("jdbc.url");
027        String userName = ConfigContext.getCurrentContextConfig().getProperty("jdbc.username");
028        String passWord = ConfigContext.getCurrentContextConfig().getProperty("jdbc.password");
029        String driverName = ConfigContext.getCurrentContextConfig().getProperty("jdbc.driver");
030
031        ds = new BasicDataSource();
032        ds.setDriverClassName(driverName);
033        ds.setUsername(userName);
034        ds.setPassword(passWord);
035        ds.setUrl(connectionUrl);
036
037        // the settings below are optional -- dbcp can work with defaults
038        ds.setMinIdle(5);
039        ds.setMaxIdle(20);
040        ds.setMaxOpenPreparedStatements(180);
041        ds.setMinEvictableIdleTimeMillis(180000000);
042        ds.setTimeBetweenEvictionRunsMillis(180000000);
043        ds.setNumTestsPerEvictionRun(3);
044        ds.setTestOnBorrow(true);
045        ds.setTestWhileIdle(true);
046        if(dbVendor.equalsIgnoreCase("oracle")){
047            ds.setValidationQuery("SELECT 1 from dual");
048        }else if(dbVendor.equalsIgnoreCase("mysql")){
049            ds.setValidationQuery("SELECT 1");
050        }
051        ds.setDefaultAutoCommit(false);
052    }
053
054    public static DataSource getInstance() throws IOException, SQLException, PropertyVetoException {
055        if (datasource == null) {
056            datasource = new DataSource();
057            return datasource;
058        } else {
059            return datasource;
060        }
061    }
062
063    public Connection getConnection() throws SQLException {
064        return this.ds.getConnection();
065    }
066
067
068}