View Javadoc
1   package org.kuali.ole.docstore.common.util;
2   
3   import java.beans.PropertyVetoException;
4   import java.io.IOException;
5   import java.sql.Connection;
6   import java.sql.SQLException;
7   
8   import org.apache.commons.dbcp.BasicDataSource;
9   import org.kuali.rice.core.api.config.property.ConfigContext;
10  
11  
12  /**
13   * Created with IntelliJ IDEA.
14   * User: jayabharathreddy
15   * Date: 6/4/14
16   * Time: 3:36 PM
17   * To change this template use File | Settings | File Templates.
18   */
19  public class DataSource {
20      private static DataSource datasource;
21      private BasicDataSource ds;
22  
23      private DataSource() throws IOException, SQLException, PropertyVetoException {
24  
25          String dbVendor = ConfigContext.getCurrentContextConfig().getProperty("db.vendor");
26          String connectionUrl = ConfigContext.getCurrentContextConfig().getProperty("jdbc.url");
27          String userName = ConfigContext.getCurrentContextConfig().getProperty("jdbc.username");
28          String passWord = ConfigContext.getCurrentContextConfig().getProperty("jdbc.password");
29          String driverName = ConfigContext.getCurrentContextConfig().getProperty("jdbc.driver");
30  
31          ds = new BasicDataSource();
32          ds.setDriverClassName(driverName);
33          ds.setUsername(userName);
34          ds.setPassword(passWord);
35          ds.setUrl(connectionUrl);
36  
37          // the settings below are optional -- dbcp can work with defaults
38          ds.setMinIdle(5);
39          ds.setMaxIdle(20);
40          ds.setMaxOpenPreparedStatements(180);
41          ds.setMinEvictableIdleTimeMillis(180000000);
42          ds.setTimeBetweenEvictionRunsMillis(180000000);
43          ds.setNumTestsPerEvictionRun(3);
44          ds.setTestOnBorrow(true);
45          ds.setTestWhileIdle(true);
46          if(dbVendor.equalsIgnoreCase("oracle")){
47              ds.setValidationQuery("SELECT 1 from dual");
48          }else if(dbVendor.equalsIgnoreCase("mysql")){
49              ds.setValidationQuery("SELECT 1");
50          }
51          ds.setDefaultAutoCommit(false);
52      }
53  
54      public static DataSource getInstance() throws IOException, SQLException, PropertyVetoException {
55          if (datasource == null) {
56              datasource = new DataSource();
57              return datasource;
58          } else {
59              return datasource;
60          }
61      }
62  
63      public Connection getConnection() throws SQLException {
64          return this.ds.getConnection();
65      }
66  
67  
68  }