Guest User

Untitled

a guest
Dec 19th, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.18 KB | None | 0 0
  1. package com.vse.uslugi.utilities.sql;
  2.  
  3. import org.apache.commons.dbcp.BasicDataSource;
  4. import org.apache.commons.dbcp.BasicDataSourceFactory;
  5. import org.hibernate.HibernateException;
  6. import org.hibernate.cfg.Environment;
  7. import org.hibernate.connection.ConnectionProvider;
  8. import org.hibernate.connection.ConnectionProviderFactory;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11.  
  12. import java.sql.Connection;
  13. import java.sql.SQLException;
  14. import java.util.Iterator;
  15. import java.util.Map;
  16. import java.util.Properties;
  17.  
  18. public class DBCPConnectionProvider implements ConnectionProvider {
  19.     private static final Logger log = LoggerFactory.getLogger(DBCPConnectionProvider.class);
  20.     private static final String DBCP_PS_MAX_ACTIVE = "hibernate.dbcp.ps.maxActive";
  21.     private static final String AUTOCOMMIT = "hibernate.connection.autocommit";
  22.     private static final String PREFIX = "hibernate.dbcp.";
  23.     private BasicDataSource dataSource;
  24.  
  25.     public void configure(Properties properties) throws HibernateException {
  26.         try {
  27.             Properties dbcpProperties = new Properties();
  28.  
  29.             // DriverClass & url
  30.             String jdbcDriverClass = properties.getProperty(Environment.DRIVER);
  31.             String jdbcUrl = properties.getProperty(Environment.URL);
  32.             dbcpProperties.put("driverClassName", jdbcDriverClass);
  33.             dbcpProperties.put("url", jdbcUrl);
  34.  
  35.             // Username / password
  36.             String username = properties.getProperty(Environment.USER);
  37.             String password = properties.getProperty(Environment.PASS);
  38.             dbcpProperties.put("username", username);
  39.             dbcpProperties.put("password", password);
  40.  
  41.             // Isolation level
  42.             String isolationLevel = properties.getProperty(Environment.ISOLATION);
  43.             if ((isolationLevel != null)
  44.                     && (isolationLevel.trim().length() > 0)) {
  45.                 dbcpProperties.put("defaultTransactionIsolation",
  46.                         isolationLevel);
  47.             }
  48.  
  49.             // Turn off autocommit (unless autocommit property is set)
  50.             String autocommit = properties.getProperty(AUTOCOMMIT);
  51.             if ((autocommit != null) && (autocommit.trim().length() > 0)) {
  52.                 dbcpProperties.put("defaultAutoCommit", autocommit);
  53.             } else {
  54.                 dbcpProperties.put("defaultAutoCommit",
  55.                         String.valueOf(Boolean.FALSE));
  56.             }
  57.  
  58.             // Pool size
  59.             String poolSize = properties.getProperty(Environment.POOL_SIZE);
  60.             if ((poolSize != null) && (poolSize.trim().length() > 0)
  61.                     && (Integer.parseInt(poolSize) > 0)) {
  62.                 dbcpProperties.put("maxActive", poolSize);
  63.             }
  64.  
  65.             // Copy all "driver" properties into "connectionProperties"
  66.             Properties driverProperties = ConnectionProviderFactory.getConnectionProperties(properties);
  67.             if (driverProperties.size() > 0) {
  68.                 StringBuilder connectionProperties = new StringBuilder();
  69.                 for (Iterator iterator = driverProperties.entrySet().iterator(); iterator.hasNext(); ) {
  70.                     Map.Entry entry = (Map.Entry) iterator.next();
  71.                     String key = (String) entry.getKey();
  72.                     String value = (String) entry.getValue();
  73.                     connectionProperties.append(key).append('=').append(value);
  74.                     if (iterator.hasNext()) {
  75.                         connectionProperties.append(';');
  76.                     }
  77.                 }
  78.                 dbcpProperties.put("connectionProperties", connectionProperties.toString());
  79.             }
  80.  
  81.             // Copy all DBCP properties removing the prefix
  82.             for (Map.Entry<Object, Object> objectObjectEntry : properties.entrySet()) {
  83.                 Map.Entry entry = (Map.Entry) objectObjectEntry;
  84.                 String key = (String) entry.getKey();
  85.                 if (key.startsWith(PREFIX)) {
  86.                     String property = key.substring(PREFIX.length());
  87.                     String value = (String) entry.getValue();
  88.                     dbcpProperties.put(property, value);
  89.                 }
  90.             }
  91.  
  92.             // Backward-compatibility
  93.             if (properties.getProperty(DBCP_PS_MAX_ACTIVE) != null) {
  94.                 dbcpProperties.put("poolPreparedStatements", String.valueOf(Boolean.TRUE));
  95.                 dbcpProperties.put("maxOpenPreparedStatements", properties.getProperty(DBCP_PS_MAX_ACTIVE));
  96.             }
  97.  
  98.             // Let the factory create the pool
  99.             dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(dbcpProperties);
  100.  
  101.             // The BasicDataSource has lazy initialization
  102.             // borrowing a connection will start the DataSource
  103.             // and make sure it is configured correctly.
  104.             Connection connection = dataSource.getConnection();
  105.             connection.close();
  106.         } catch (Exception e) {
  107.             String message = "Could not create a DBCP pool";
  108.             log.error(message, e);
  109.             if (dataSource != null) {
  110.                 try {
  111.                     dataSource.close();
  112.                 } catch (Exception ignored) {
  113.                 }
  114.                 dataSource = null;
  115.             }
  116.             throw new HibernateException(message, e);
  117.         }
  118.         log.debug("Configure DBCPConnectionProvider complete");
  119.     }
  120.  
  121.     public Connection getConnection() throws SQLException {
  122.         return dataSource.getConnection();
  123.     }
  124.  
  125.     public void close() throws HibernateException {
  126.         try {
  127.             if (dataSource != null) {
  128.                 dataSource.close();
  129.                 dataSource = null;
  130.             } else {
  131.                 log.warn("Cannot close DBCP pool (not initialized)");
  132.             }
  133.         } catch (Exception e) {
  134.             throw new HibernateException("Could not close DBCP pool", e);
  135.         }
  136.     }
  137.  
  138.     @Override
  139.     public void closeConnection(Connection connection) throws SQLException {
  140.         connection.close();
  141.     }
  142.  
  143.     @Override
  144.     public boolean supportsAggressiveRelease() {
  145.         return false;
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment