Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.InputStream;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.util.Properties;
- import com.mchange.v2.c3p0.ComboPooledDataSource;
- public class Storage {
- private static final String C3P0_PROPERTIES_FILE = "/web/WEB-INF/properties/pool.properties";
- private static Storage istance = null;
- private ComboPooledDataSource cpds;
- private Storage() {
- Properties properties = new Properties();
- this.cpds = new ComboPooledDataSource();
- InputStream inputStream = this.getClass().getResourceAsStream(
- C3P0_PROPERTIES_FILE);
- try {
- properties.load(inputStream);
- this.cpds.setJdbcUrl(properties.getProperty("jdbcUrl"));
- this.cpds.setUser(properties.getProperty("user"));
- this.cpds.setPassword(properties.getProperty("password"));
- this.cpds.setInitialPoolSize(Integer.parseInt(properties
- .getProperty("initialPoolSize")));
- this.cpds.setAcquireIncrement(Integer.parseInt(properties
- .getProperty("aquireIncrement")));
- this.cpds.setMaxPoolSize(Integer.parseInt(properties
- .getProperty("maxPoolSize")));
- this.cpds.setUnreturnedConnectionTimeout(Integer
- .parseInt(properties
- .getProperty("unreturedConnectionTimeout")));
- this.cpds.setMaxStatements(Integer.parseInt(properties
- .getProperty("maxStatement")));
- this.cpds.setMaxStatementsPerConnection(Integer.parseInt(properties
- .getProperty("maxStatementPerConnection")));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public synchronized static Connection getConnection() throws SQLException {
- if (istance == null) {
- istance = new Storage();
- }
- return istance.cpds.getConnection();
- }
- public synchronized static void releaseConnection(Connection pConnection) {
- if (pConnection == null)
- throw new IllegalArgumentException(
- "SpConnection can't be null");
- if (istance != null) {
- try {
- pConnection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment