Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.vse.uslugi.utilities.sql;
- import org.apache.commons.dbcp.BasicDataSource;
- import org.apache.commons.dbcp.BasicDataSourceFactory;
- import org.hibernate.HibernateException;
- import org.hibernate.cfg.Environment;
- import org.hibernate.connection.ConnectionProvider;
- import org.hibernate.connection.ConnectionProviderFactory;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Properties;
- public class DBCPConnectionProvider implements ConnectionProvider {
- private static final Logger log = LoggerFactory.getLogger(DBCPConnectionProvider.class);
- private static final String DBCP_PS_MAX_ACTIVE = "hibernate.dbcp.ps.maxActive";
- private static final String AUTOCOMMIT = "hibernate.connection.autocommit";
- private static final String PREFIX = "hibernate.dbcp.";
- private BasicDataSource dataSource;
- public void configure(Properties properties) throws HibernateException {
- try {
- Properties dbcpProperties = new Properties();
- // DriverClass & url
- String jdbcDriverClass = properties.getProperty(Environment.DRIVER);
- String jdbcUrl = properties.getProperty(Environment.URL);
- dbcpProperties.put("driverClassName", jdbcDriverClass);
- dbcpProperties.put("url", jdbcUrl);
- // Username / password
- String username = properties.getProperty(Environment.USER);
- String password = properties.getProperty(Environment.PASS);
- dbcpProperties.put("username", username);
- dbcpProperties.put("password", password);
- // Isolation level
- String isolationLevel = properties.getProperty(Environment.ISOLATION);
- if ((isolationLevel != null)
- && (isolationLevel.trim().length() > 0)) {
- dbcpProperties.put("defaultTransactionIsolation",
- isolationLevel);
- }
- // Turn off autocommit (unless autocommit property is set)
- String autocommit = properties.getProperty(AUTOCOMMIT);
- if ((autocommit != null) && (autocommit.trim().length() > 0)) {
- dbcpProperties.put("defaultAutoCommit", autocommit);
- } else {
- dbcpProperties.put("defaultAutoCommit",
- String.valueOf(Boolean.FALSE));
- }
- // Pool size
- String poolSize = properties.getProperty(Environment.POOL_SIZE);
- if ((poolSize != null) && (poolSize.trim().length() > 0)
- && (Integer.parseInt(poolSize) > 0)) {
- dbcpProperties.put("maxActive", poolSize);
- }
- // Copy all "driver" properties into "connectionProperties"
- Properties driverProperties = ConnectionProviderFactory.getConnectionProperties(properties);
- if (driverProperties.size() > 0) {
- StringBuilder connectionProperties = new StringBuilder();
- for (Iterator iterator = driverProperties.entrySet().iterator(); iterator.hasNext(); ) {
- Map.Entry entry = (Map.Entry) iterator.next();
- String key = (String) entry.getKey();
- String value = (String) entry.getValue();
- connectionProperties.append(key).append('=').append(value);
- if (iterator.hasNext()) {
- connectionProperties.append(';');
- }
- }
- dbcpProperties.put("connectionProperties", connectionProperties.toString());
- }
- // Copy all DBCP properties removing the prefix
- for (Map.Entry<Object, Object> objectObjectEntry : properties.entrySet()) {
- Map.Entry entry = (Map.Entry) objectObjectEntry;
- String key = (String) entry.getKey();
- if (key.startsWith(PREFIX)) {
- String property = key.substring(PREFIX.length());
- String value = (String) entry.getValue();
- dbcpProperties.put(property, value);
- }
- }
- // Backward-compatibility
- if (properties.getProperty(DBCP_PS_MAX_ACTIVE) != null) {
- dbcpProperties.put("poolPreparedStatements", String.valueOf(Boolean.TRUE));
- dbcpProperties.put("maxOpenPreparedStatements", properties.getProperty(DBCP_PS_MAX_ACTIVE));
- }
- // Let the factory create the pool
- dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(dbcpProperties);
- // The BasicDataSource has lazy initialization
- // borrowing a connection will start the DataSource
- // and make sure it is configured correctly.
- Connection connection = dataSource.getConnection();
- connection.close();
- } catch (Exception e) {
- String message = "Could not create a DBCP pool";
- log.error(message, e);
- if (dataSource != null) {
- try {
- dataSource.close();
- } catch (Exception ignored) {
- }
- dataSource = null;
- }
- throw new HibernateException(message, e);
- }
- log.debug("Configure DBCPConnectionProvider complete");
- }
- public Connection getConnection() throws SQLException {
- return dataSource.getConnection();
- }
- public void close() throws HibernateException {
- try {
- if (dataSource != null) {
- dataSource.close();
- dataSource = null;
- } else {
- log.warn("Cannot close DBCP pool (not initialized)");
- }
- } catch (Exception e) {
- throw new HibernateException("Could not close DBCP pool", e);
- }
- }
- @Override
- public void closeConnection(Connection connection) throws SQLException {
- connection.close();
- }
- @Override
- public boolean supportsAggressiveRelease() {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment