Guest User

Storage.java

a guest
Nov 18th, 2013
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.98 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import java.util.Properties;
  5.  
  6. import com.mchange.v2.c3p0.ComboPooledDataSource;
  7.  
  8. public class Storage {
  9.     private static final String C3P0_PROPERTIES_FILE = "/web/WEB-INF/properties/pool.properties";
  10.     private static Storage istance = null;
  11.     private ComboPooledDataSource cpds;
  12.  
  13.     private Storage() {
  14.         Properties properties = new Properties();
  15.         this.cpds = new ComboPooledDataSource();
  16.  
  17.         InputStream inputStream = this.getClass().getResourceAsStream(
  18.                 C3P0_PROPERTIES_FILE);
  19.  
  20.         try {
  21.             properties.load(inputStream);
  22.  
  23.             this.cpds.setJdbcUrl(properties.getProperty("jdbcUrl"));
  24.             this.cpds.setUser(properties.getProperty("user"));
  25.             this.cpds.setPassword(properties.getProperty("password"));
  26.             this.cpds.setInitialPoolSize(Integer.parseInt(properties
  27.                     .getProperty("initialPoolSize")));
  28.             this.cpds.setAcquireIncrement(Integer.parseInt(properties
  29.                     .getProperty("aquireIncrement")));
  30.             this.cpds.setMaxPoolSize(Integer.parseInt(properties
  31.                     .getProperty("maxPoolSize")));
  32.             this.cpds.setUnreturnedConnectionTimeout(Integer
  33.                     .parseInt(properties
  34.                             .getProperty("unreturedConnectionTimeout")));
  35.             this.cpds.setMaxStatements(Integer.parseInt(properties
  36.                     .getProperty("maxStatement")));
  37.             this.cpds.setMaxStatementsPerConnection(Integer.parseInt(properties
  38.                     .getProperty("maxStatementPerConnection")));
  39.         } catch (Exception e) {
  40.             e.printStackTrace();
  41.         }
  42.     }
  43.     public synchronized static Connection getConnection() throws SQLException {
  44.         if (istance == null) {
  45.             istance = new Storage();
  46.         }
  47.  
  48.         return istance.cpds.getConnection();
  49.     }
  50.     public synchronized static void releaseConnection(Connection pConnection) {
  51.         if (pConnection == null)
  52.             throw new IllegalArgumentException(
  53.                     "SpConnection can't be null");
  54.  
  55.         if (istance != null) {
  56.             try {
  57.                 pConnection.close();
  58.             } catch (SQLException e) {
  59.                 e.printStackTrace();
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment