daily pastebin goal
23%
SHARE
TWEET

Untitled

a guest Jan 25th, 2016 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.sql.Connection;
  2. import org.apache.tomcat.jdbc.pool.DataSource;
  3. import org.apache.tomcat.jdbc.pool.PoolProperties;
  4. public class DataSourceConfig
  5. {
  6.   public static final String DATA_SOURCE_CLASS_NAME = "org.postgresql.Driver";
  7.     public static final String DATA_SOURCE_URL = "jdbc:postgresql://%s:%s/%s";
  8.     public static final String JDBC_INTERCEPTORS = "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
  9.             + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer";
  10.  
  11.     private String dataSourceClassName = DATA_SOURCE_CLASS_NAME;
  12.     private String hostName;            
  13.     private String portNumber;          
  14.     private String userName;            
  15.     private String password;            
  16.     private String dbName;
  17.     // Connection pool
  18.     private PoolProperties p = new PoolProperties();
  19.     private DataSource datasource = new DataSource();
  20.     public String getHostName() {
  21.       return hostName;
  22.     }
  23.     public void setHostName(String hostName) {
  24.       this.hostName = hostName;
  25.     }
  26.     /**
  27.      * @return the portNumber
  28.      */
  29.     public String getPortNumber() {
  30.         return portNumber;
  31.     }
  32.     /**
  33.      * @param portNumber the portNumber to set
  34.      */
  35.     public void setPortNumber(String portNumber) {
  36.         this.portNumber = portNumber;
  37.     }
  38.     /**
  39.      * @return the userName
  40.      */
  41.     public String getUserName() {
  42.         return userName;
  43.     }
  44.     /**
  45.      * @param userName the userName to set
  46.      */
  47.     public void setUserName(String userName) {
  48.         this.userName = userName;
  49.     }
  50.     /**
  51.      * @return the password
  52.      */
  53.     public String getPassword() {
  54.         return password;
  55.     }
  56.     /**
  57.      * @param password the password to set
  58.      */
  59.     public void setPassword(String password) {
  60.         this.password = password;
  61.     }
  62.     public String getDbName(){
  63.         return this.dbName;
  64.     }
  65.     public void setDbName(String dbName){
  66.         this.dbName = dbName;
  67.     }
  68.     public void setupPool() {
  69.         try {
  70.             String url = String.format(DATA_SOURCE_URL, hostName, portNumber, dbName);
  71.             p.setUrl(url);
  72.             p.setDriverClassName(DATA_SOURCE_CLASS_NAME);
  73.             p.setUsername(userName);
  74.             p.setPassword(password);
  75.             p.setJmxEnabled(true);
  76.             p.setTestWhileIdle(false);
  77.             p.setTestOnBorrow(true);
  78.             p.setValidationQuery("SELECT 1");
  79.             p.setTestOnReturn(false);
  80.             p.setValidationInterval(30000);
  81.             p.setTimeBetweenEvictionRunsMillis(30000);
  82.             p.setMaxActive(100);
  83.             p.setInitialSize(10);
  84.             p.setMaxWait(10000);
  85.             p.setRemoveAbandonedTimeout(60000);
  86.             p.setMinEvictableIdleTimeMillis(30000);
  87.             p.setMinIdle(10);
  88.             p.setLogAbandoned(true);
  89.             p.setRemoveAbandoned(true);
  90.             p.setJdbcInterceptors(JDBC_INTERCEPTORS);
  91.             datasource.setPoolProperties(p);
  92.         } catch(Exception ex) {
  93.             ex.printStackTrace();
  94.         }
  95.     }
  96.     public Connection getConnection() {
  97.         Connection connection = null;
  98.         try {
  99.             System.out.println("Waiting for getting connection!");
  100.             connection = datasource.getConnection();
  101.         } catch(Exception ex) {
  102.             ex.printStackTrace();
  103.             return null;
  104.         }
  105.         return connection;
  106.     }
  107. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top