Example usage for org.springframework.jdbc.datasource.lookup JndiDataSourceLookup getDataSource

List of usage examples for org.springframework.jdbc.datasource.lookup JndiDataSourceLookup getDataSource

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource.lookup JndiDataSourceLookup getDataSource.

Prototype

@Override
    public DataSource getDataSource(String dataSourceName) throws DataSourceLookupFailureException 

Source Link

Usage

From source file:org.flowable.app.conf.FlowableAppDatasourceUtil.java

public static DataSource createDataSource(Environment environment) {

    String dataSourceJndiName = environment.getProperty("datasource.jndi.name");
    if (StringUtils.isNotEmpty(dataSourceJndiName)) {

        LOGGER.info("Using jndi datasource '{}'", dataSourceJndiName);
        JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
        dsLookup.setResourceRef(//from  w  w w.  j  av a  2 s.  c  o m
                environment.getProperty("datasource.jndi.resourceRef", Boolean.class, Boolean.TRUE));
        DataSource dataSource = dsLookup.getDataSource(dataSourceJndiName);
        return dataSource;

    } else {

        String url = environment.getProperty("datasource.url", "jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000");
        String driver = environment.getProperty("datasource.driver", "org.h2.Driver");
        String username = environment.getProperty("datasource.username", "sa");
        String password = environment.getProperty("datasource.password", "");

        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(url);
        dataSource.setDriverClassName(driver);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        /*
         * Connection pool settings (see https://github.com/brettwooldridge/HikariCP)
         */

        // Timeout to wait for a connection of the pool
        Long connectionTimeout = environment.getProperty("datasource.connection-timeout", Long.class);
        if (connectionTimeout == null) {
            // Backwards compatible property name (pre 6.3.0)
            connectionTimeout = environment.getProperty("datasource.connection.timeout", Long.class);
        }
        if (connectionTimeout != null) {
            dataSource.setConnectionTimeout(connectionTimeout);
        }

        // Minimum amount of connections to keep idle in the pool
        Integer minIdle = environment.getProperty("datasource.min-pool-size", Integer.class);
        if (minIdle == null) {
            // Backwards compatible property name (pre 6.3.0)
            minIdle = environment.getProperty("datasource.connection.minidle", Integer.class);
        }
        if (minIdle != null) {
            dataSource.setMinimumIdle(minIdle);
        }

        // Maximum amount of connections in the pool
        Integer maxPoolSize = environment.getProperty("datasource.max-pool-size", Integer.class);
        if (maxPoolSize == null) {
            // Backwards compatible property name (pre 6.3.0)
            maxPoolSize = environment.getProperty("datasource.connection.maxpoolsize", Integer.class);
        }
        if (maxPoolSize != null) {
            dataSource.setMaximumPoolSize(maxPoolSize);
        }

        // Time in milliseconds to indicate when a connection will be removed from the pool. 
        // Use 0 to never remove an idle connection from the pool 
        Long idleTimeout = environment.getProperty("datasource.max-idle-time", Long.class);
        if (idleTimeout != null) {
            // Property has been historically expressed in seconds, but Hikari expects milliseconds
            idleTimeout = idleTimeout * 1000;
        } else if (idleTimeout == null) {
            // Backwards compatible property name (pre 6.3.0)
            idleTimeout = environment.getProperty("datasource.connection.idletimeout", Long.class);
        }
        if (idleTimeout != null) {
            dataSource.setIdleTimeout(idleTimeout);
        }

        // The maximum lifetime of a connection in the pool
        Long maxLifetime = environment.getProperty("datasource.connection.max-lifetime", Long.class);
        if (maxLifetime == null) {
            maxLifetime = environment.getProperty("datasource.connection.maxlifetime", Long.class);
        }
        if (maxLifetime != null) {
            dataSource.setMaxLifetime(maxLifetime);
        }

        // Test query
        String testQuery = environment.getProperty("datasource.test-query");
        if (testQuery == null) {
            // Backwards compatible property name (pre 6.3.0)
            testQuery = environment.getProperty("datasource.preferred-test-query");
        }
        if (testQuery != null) {
            dataSource.setConnectionTestQuery(testQuery);
        }

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Configuring datasource with following properties");
            LOGGER.info("Datasource driver: {}", driver);
            LOGGER.info("Datasource url: {}", url);
            LOGGER.info("Datasource user name: {}", username);
            LOGGER.info("Min pool size | Max pool size | {} | {}", minIdle != null ? minIdle : "default",
                    maxPoolSize != null ? maxPoolSize : "default");
        }

        return dataSource;
    }

}

From source file:org.flowable.rest.conf.DatabaseConfiguration.java

@Bean
public DataSource dataSource() {

    String dataSourceJndiName = environment.getProperty("datasource.jndi.name");
    if (StringUtils.isNotEmpty(dataSourceJndiName)) {

        LOGGER.info("Using jndi datasource '{}'", dataSourceJndiName);
        JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
        dsLookup.setResourceRef(//  w  ww. ja  v  a  2s  . c  o  m
                environment.getProperty("datasource.jndi.resourceRef", Boolean.class, Boolean.TRUE));
        DataSource dataSource = dsLookup.getDataSource(dataSourceJndiName);
        return dataSource;

    } else {

        String jdbcUrl = environment.getProperty("datasource.url", "jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000");
        String jdbcDriver = environment.getProperty("datasource.driver", "org.h2.Driver");
        String jdbcUsername = environment.getProperty("datasource.username", "sa");
        String jdbcPassword = environment.getProperty("datasource.password", "");

        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(jdbcUrl);
        dataSource.setDriverClassName(jdbcDriver);
        dataSource.setUsername(jdbcUsername);
        dataSource.setPassword(jdbcPassword);

        /*
         * Connection pool settings (see https://github.com/brettwooldridge/HikariCP)
         */

        // Timeout to wait for a connection of the pool
        Long connectionTimeout = environment.getProperty("datasource.connection-timeout", Long.class);
        if (connectionTimeout == null) {
            // Backwards compatible property name (pre 6.3.0)
            connectionTimeout = environment.getProperty("datasource.connection.timeout", Long.class);
        }
        if (connectionTimeout != null) {
            dataSource.setConnectionTimeout(connectionTimeout);
        }

        // Minimum amount of connections to keep idle in the pool
        Integer minIdle = environment.getProperty("datasource.min-pool-size", Integer.class);
        if (minIdle == null) {
            // Backwards compatible property name (pre 6.3.0)
            minIdle = environment.getProperty("datasource.connection.minidle", Integer.class);
        }
        if (minIdle != null) {
            dataSource.setMinimumIdle(minIdle);
        }

        // Maximum amount of connections in the pool
        Integer maxPoolSize = environment.getProperty("datasource.max-pool-size", Integer.class);
        if (maxPoolSize == null) {
            // Backwards compatible property name (pre 6.3.0)
            maxPoolSize = environment.getProperty("datasource.connection.maxpoolsize", Integer.class);
        }
        if (maxPoolSize != null) {
            dataSource.setMaximumPoolSize(maxPoolSize);
        }

        // Time in milliseconds to indicate when a connection will be removed from the pool. 
        // Use 0 to never remove an idle connection from the pool 
        Long idleTimeout = environment.getProperty("datasource.max-idle-time", Long.class);
        if (idleTimeout != null) {
            // Property has been historically expressed in seconds, but Hikari expects milliseconds
            idleTimeout = idleTimeout * 1000;
        } else if (idleTimeout == null) {
            // Backwards compatible property name (pre 6.3.0)
            idleTimeout = environment.getProperty("datasource.connection.idletimeout", Long.class);
        }
        if (idleTimeout != null) {
            dataSource.setIdleTimeout(idleTimeout);
        }

        // The maximum lifetime of a connection in the pool
        Long maxLifetime = environment.getProperty("datasource.connection.max-lifetime", Long.class);
        if (maxLifetime == null) {
            maxLifetime = environment.getProperty("datasource.connection.maxlifetime", Long.class);
        }
        if (maxLifetime != null) {
            dataSource.setMaxLifetime(maxLifetime);
        }

        // Test query
        String testQuery = environment.getProperty("datasource.test-query");
        if (testQuery == null) {
            // Backwards compatible property name (pre 6.3.0)
            testQuery = environment.getProperty("datasource.preferred-test-query");
        }
        if (testQuery != null) {
            dataSource.setConnectionTestQuery(testQuery);
        }

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Configuring datasource with following properties");
            LOGGER.info("Datasource driver: {}", jdbcDriver);
            LOGGER.info("Datasource url: {}", jdbcUrl);
            LOGGER.info("Datasource user name: {}", jdbcUsername);
            LOGGER.info("Min pool size | Max pool size | {} | {}", minIdle != null ? minIdle : "default",
                    maxPoolSize != null ? maxPoolSize : "default");
        }

        return dataSource;
    }

}