Example usage for org.hibernate.cfg Environment isolationLevelToString

List of usage examples for org.hibernate.cfg Environment isolationLevelToString

Introduction

In this page you can find the example usage for org.hibernate.cfg Environment isolationLevelToString.

Prototype

@Deprecated
public static String isolationLevelToString(int isolation) 

Source Link

Usage

From source file:com.glaf.jbpm.connection.DbcpConnectionProvider.java

License:Apache License

@SuppressWarnings({ "rawtypes", "unchecked" })
public void configure(Properties props) throws RuntimeException {
    String jdbcDriverClass = props.getProperty(Environment.DRIVER);
    String jdbcUrl = props.getProperty(Environment.URL);
    Properties connectionProps = ConnectionProviderFactory.getConnectionProperties(props);

    log.info("DBCP using driver: " + jdbcDriverClass + " at URL: " + jdbcUrl);
    log.info("Connection properties: " + PropertiesHelper.maskOut(connectionProps, "password"));

    autocommit = PropertiesHelper.getBoolean(Environment.AUTOCOMMIT, props);
    log.info("autocommit mode: " + autocommit);

    if (jdbcDriverClass == null) {
        log.warn("No JDBC Driver class was specified by property " + Environment.DRIVER);
    } else {//  ww w .  j av  a 2s . co m
        try {
            Class.forName(jdbcDriverClass);
        } catch (ClassNotFoundException cnfe) {
            try {
                ClassUtils.classForName(jdbcDriverClass);
            } catch (Exception e) {
                String msg = "JDBC Driver class not found: " + jdbcDriverClass;
                log.error(msg, e);
                throw new RuntimeException(msg, e);
            }
        }
    }

    String dbUser = props.getProperty(Environment.USER);
    String dbPassword = props.getProperty(Environment.PASS);

    if (dbUser == null) {
        dbUser = "";
    }

    if (dbPassword == null) {
        dbPassword = "";
    }

    Properties properties = new Properties();

    for (Iterator<Object> ii = props.keySet().iterator(); ii.hasNext();) {
        String key = (String) ii.next();
        if (key.startsWith("hibernate.dbcp.")) {
            String newKey = key.substring(15);
            properties.put(newKey, props.get(key));
        }
    }

    // Create the actual pool of connections
    ObjectPool connectionPool = new GenericObjectPool(null);

    if (props.getProperty("hibernate.connection.maxIdle") != null) {
        int value = PropertiesHelper.getInt("hibernate.connection.maxIdle", props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setMaxIdle(value);
        }
    }

    if (props.getProperty("hibernate.connection.minIdle") != null) {
        int value = PropertiesHelper.getInt("hibernate.connection.minIdle", props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setMinIdle(value);
        }
    }

    if (props.getProperty("hibernate.connection.maxActive") != null) {
        int value = PropertiesHelper.getInt("hibernate.connection.maxActive", props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setMaxActive(value);
        }
    }

    if (props.getProperty("hibernate.connection.maxWait") != null) {
        int value = PropertiesHelper.getInt("hibernate.connection.maxWait", props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setMaxWait(value);
        }
    }

    // how often should the evictor run (if ever, default is -1 = off)
    if (props.getProperty("hibernate.connection.timeBetweenEvictionRunsMillis") != null) {
        int value = PropertiesHelper.getInt("hibernate.connection.timeBetweenEvictionRunsMillis", props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setTimeBetweenEvictionRunsMillis(value);

            // in each eviction run, ecict at least a fourth of "maxIdle"
            // connections
            int maxIdle = ((GenericObjectPool) connectionPool).getMaxIdle();
            int numTestsPerEvictionRun = (int) Math.ceil(((double) maxIdle / 4));
            ((GenericObjectPool) connectionPool).setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        }
    }
    // how long may a connection sit idle in the pool before it may be
    // evicted
    if (props.getProperty("hibernate.connection.minEvictableIdleTimeMillis") != null) {
        int value = PropertiesHelper.getInt("hibernate.connection.minEvictableIdleTimeMillis", props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setMinEvictableIdleTimeMillis(value);
        }
    }

    // Create a factory to be used by the pool to create the connections
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcUrl, dbUser, dbPassword);

    // Create a factory for caching the PreparedStatements
    KeyedObjectPoolFactory kpf = null;

    if (props.getProperty("hibernate.connection.maxStatements") != null) {
        int value = PropertiesHelper.getInt("hibernate.connection.maxStatements", props, 0);
        if (value > 0) {
            kpf = new StackKeyedObjectPoolFactory(null, value);
        }
    } else {
        kpf = new StackKeyedObjectPoolFactory(null, 200);
    }

    // Wrap the connections and statements with pooled variants
    try {
        String testSQL = null;
        if (props.getProperty("hibernate.connection.testSQL") != null) {
            testSQL = PropertiesHelper.getString("hibernate.connection.testSQL", props, null);
        }
        new PoolableConnectionFactory(connectionFactory, connectionPool, kpf, testSQL, false, false);
        if (testSQL != null) {
            ((GenericObjectPool) connectionPool).setTestOnBorrow(true);
        }
    } catch (Exception e) {
        throw new ConnectionPoolException("DBCP", jdbcDriverClass, jdbcUrl, e);
    }

    ds = new PoolingDataSource(connectionPool);

    Connection conn = null;
    try {
        conn = ds.getConnection();
        if (conn == null) {
            throw new RuntimeException("DBCP connection pool can't get jdbc connection");
        }
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    } finally {
        JdbcUtils.close(conn);
    }

    String i = props.getProperty(Environment.ISOLATION);
    if (i == null) {
        isolation = null;
    } else {
        isolation = new Integer(i);
        log.info("JDBC isolation level: " + Environment.isolationLevelToString(isolation.intValue()));
    }

}

From source file:com.glaf.jbpm.connection.HikariCPConnectionProvider.java

License:Apache License

public void configure(Properties props) throws RuntimeException {
    Properties properties = new Properties();
    properties.putAll(props);/*w w  w .ja  v a2  s . c o  m*/

    for (Iterator<?> ii = props.keySet().iterator(); ii.hasNext();) {
        String key = (String) ii.next();
        if (key.startsWith("hikari.")) {
            String newKey = key.substring(7);
            properties.put(newKey, props.get(key));
        }
    }

    String jdbcDriverClass = properties.getProperty(Environment.DRIVER);
    String jdbcUrl = properties.getProperty(Environment.URL);

    Properties connectionProps = ConnectionProviderFactory.getConnectionProperties(properties);

    log.info("HikariCP using driver: " + jdbcDriverClass + " at URL: " + jdbcUrl);
    log.info("Connection properties: " + PropertiesHelper.maskOut(connectionProps, Environment.PASS));

    autocommit = PropertiesHelper.getBoolean(Environment.AUTOCOMMIT, props);
    log.info("autocommit mode: " + autocommit);

    if (jdbcDriverClass == null) {
        log.warn("No JDBC Driver class was specified by property " + Environment.DRIVER);
    } else {
        try {
            Class.forName(jdbcDriverClass);
        } catch (ClassNotFoundException cnfe) {
            try {
                ClassUtils.classForName(jdbcDriverClass);
            } catch (Exception e) {
                String msg = "JDBC Driver class not found: " + jdbcDriverClass;
                log.error(msg, e);
                throw new RuntimeException(msg, e);
            }
        }
    }

    try {

        String validationQuery = properties.getProperty(ConnectionConstants.PROP_VALIDATIONQUERY);

        Integer initialPoolSize = PropertiesHelper.getInteger(ConnectionConstants.PROP_INITIALSIZE, properties);
        Integer minPoolSize = PropertiesHelper.getInteger(ConnectionConstants.PROP_MINACTIVE, properties);
        Integer maxPoolSize = PropertiesHelper.getInteger(ConnectionConstants.PROP_MAXACTIVE, properties);
        if (initialPoolSize == null && minPoolSize != null) {
            properties.put(ConnectionConstants.PROP_INITIALSIZE, String.valueOf(minPoolSize).trim());
        }

        Integer maxWait = PropertiesHelper.getInteger(ConnectionConstants.PROP_MAXWAIT, properties);

        if (maxPoolSize == null) {
            maxPoolSize = 50;
        }

        String dbUser = properties.getProperty(Environment.USER);
        String dbPassword = properties.getProperty(Environment.PASS);

        if (dbUser == null) {
            dbUser = "";
        }

        if (dbPassword == null) {
            dbPassword = "";
        }

        HikariConfig config = new HikariConfig();
        config.setDriverClassName(jdbcDriverClass);
        config.setJdbcUrl(jdbcUrl);
        config.setUsername(dbUser);
        config.setPassword(dbPassword);
        config.setMaximumPoolSize(maxPoolSize);
        config.setDataSourceProperties(properties);

        if (StringUtils.isNotEmpty(validationQuery)) {
            config.setConnectionTestQuery(validationQuery);
        }
        if (maxWait != null) {
            config.setConnectionTimeout(maxWait * 1000L);
        }

        config.setMaxLifetime(1000L * 3600 * 8);

        String isolationLevel = properties.getProperty(Environment.ISOLATION);
        if (isolationLevel == null) {
            isolation = null;
        } else {
            isolation = new Integer(isolationLevel);
            log.info("JDBC isolation level: " + Environment.isolationLevelToString(isolation.intValue()));
        }

        if (StringUtils.isNotEmpty(isolationLevel)) {
            config.setTransactionIsolation(isolationLevel);
        }

        ds = new HikariDataSource(config);
    } catch (Exception ex) {
        ex.printStackTrace();
        log.error("could not instantiate HikariCP connection pool", ex);
        throw new RuntimeException("Could not instantiate HikariCP connection pool", ex);
    }

}

From source file:com.vladmihalcea.service.impl.StoreServiceImpl.java

License:Apache License

@Override
@Transactional(isolation = Isolation.SERIALIZABLE)
public void purchase(Long productId) {
    Product product = entityManager.find(Product.class, 1L);
    Session session = (Session) entityManager.getDelegate();
    session.doWork(new Work() {
        @Override/*  w w  w  .  ja va  2  s  .com*/
        public void execute(Connection connection) throws SQLException {
            LOGGER.debug("Transaction isolation level is {}",
                    Environment.isolationLevelToString(connection.getTransactionIsolation()));
        }
    });
    product.setQuantity(product.getQuantity() - 1);
}

From source file:net.sf.jasperreports.data.hibernate.HibernateConnectionProvider.java

License:Open Source License

@Override
public void configure(Properties props) throws HibernateException {

    String driverClass = props.getProperty(Environment.DRIVER);

    poolSize = PropertiesHelper.getInt(Environment.POOL_SIZE, props, 20); //default pool size 20
    log.info("Using Hibernate built-in connection pool (not for production use!)");
    log.info("Hibernate connection pool size: " + poolSize);

    autocommit = PropertiesHelper.getBoolean(Environment.AUTOCOMMIT, props);
    log.info("autocommit mode: " + autocommit);

    isolation = PropertiesHelper.getInteger(Environment.ISOLATION, props);
    if (isolation != null)
        log.info("JDBC isolation level: " + Environment.isolationLevelToString(isolation.intValue()));

    if (driverClass == null) {
        log.warn("no JDBC Driver class was specified by property " + Environment.DRIVER);
    } else {//from  w  w  w. ja  v a2 s  .  c om
        try {
            // trying via forName() first to be as close to DriverManager's semantics
            // NOTE for JSS: we use the context class loader because it will be able to locate the database drivers
            // already loaded in our plug-ins or projects
            driver = (Driver) Class.forName(driverClass, true, Thread.currentThread().getContextClassLoader())
                    .newInstance();
        } catch (Exception e) {
            try {
                driver = (Driver) ReflectHelper.classForName(driverClass).newInstance();
            } catch (Exception e1) {
                log.error(e1.getMessage());
                throw new HibernateException(e1);
            }
        }
    }

    url = props.getProperty(Environment.URL);
    if (url == null) {
        String msg = "JDBC URL was not specified by property " + Environment.URL;
        log.error(msg);
        throw new HibernateException(msg);
    }

    connectionProps = ConnectionProviderFactory.getConnectionProperties(props);

    log.info("using driver: " + driverClass + " at URL: " + url);
    // if debug level is enabled, then log the password, otherwise mask it
    if (log.isDebugEnabled()) {
        log.info("connection properties: " + connectionProps);
    } else if (log.isInfoEnabled()) {
        log.info("connection properties: " + PropertiesHelper.maskOut(connectionProps, "password"));
    }

}