Search in sources :

Example 1 with ConnectionPool

use of org.opennms.netmgt.config.opennmsDataSources.ConnectionPool in project opennms by OpenNMS.

the class DataSourceFactory method parseDataSource.

private static ClosableDataSource parseDataSource(final String dsName) {
    String factoryClass = null;
    ConnectionPool connectionPool = m_dataSourceConfigFactory.getConnectionPool();
    factoryClass = connectionPool.getFactory();
    ClosableDataSource dataSource = null;
    final String defaultClassName = DEFAULT_FACTORY_CLASS.getName();
    try {
        final Class<?> clazz = Class.forName(factoryClass);
        final Constructor<?> constructor = clazz.getConstructor(new Class<?>[] { JdbcDataSource.class });
        dataSource = (ClosableDataSource) constructor.newInstance(new Object[] { m_dataSourceConfigFactory.getJdbcDataSource(dsName) });
    } catch (final Throwable t) {
        LOG.debug("Unable to load {}, falling back to the default dataSource ({})", factoryClass, defaultClassName, t);
        try {
            final Constructor<?> constructor = ((Class<?>) DEFAULT_FACTORY_CLASS).getConstructor(new Class<?>[] { JdbcDataSource.class });
            dataSource = (ClosableDataSource) constructor.newInstance(new Object[] { m_dataSourceConfigFactory.getJdbcDataSource(dsName) });
        } catch (final Throwable cause) {
            LOG.error("Unable to load {}.", DEFAULT_FACTORY_CLASS.getName(), cause);
            throw new IllegalArgumentException("Unable to load " + defaultClassName + ".", cause);
        }
    }
    if (connectionPool != null) {
        dataSource.setIdleTimeout(connectionPool.getIdleTimeout());
        try {
            dataSource.setLoginTimeout(connectionPool.getLoginTimeout());
        } catch (SQLException e) {
            LOG.warn("Exception thrown while trying to set login timeout on datasource", e);
        }
        dataSource.setMinPool(connectionPool.getMinPool());
        dataSource.setMaxPool(connectionPool.getMaxPool());
        dataSource.setMaxSize(connectionPool.getMaxSize());
    }
    return dataSource;
}
Also used : ConnectionPool(org.opennms.netmgt.config.opennmsDataSources.ConnectionPool) SQLException(java.sql.SQLException) Constructor(java.lang.reflect.Constructor) JdbcDataSource(org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource)

Example 2 with ConnectionPool

use of org.opennms.netmgt.config.opennmsDataSources.ConnectionPool in project opennms by OpenNMS.

the class XADataSourceFactory method init.

/**
	 * <p>init</p>
	 *
	 * @param dsName a {@link java.lang.String} object.
	 */
public static synchronized void init(final String dsName) {
    if (isLoaded(dsName)) {
        // init already called, return
        return;
    }
    final JdbcDataSource ds = m_dataSourceConfigFactory.getJdbcDataSource(dsName);
    final ConnectionPool pool = m_dataSourceConfigFactory.getConnectionPool();
    String urlString = ds.getUrl();
    if (urlString.startsWith("jdbc:")) {
        urlString = urlString.substring("jdbc:".length());
    }
    URI url = URI.create(urlString);
    // TODO: Add support for more XADataSources (hsqldb, derby)
    if ("postgresql".equalsIgnoreCase(url.getScheme())) {
        PGXADataSource xaDataSource = new PGXADataSource();
        xaDataSource.setServerName(url.getHost());
        xaDataSource.setPortNumber(url.getPort());
        xaDataSource.setDatabaseName(ds.getDatabaseName());
        xaDataSource.setUser(ds.getUserName());
        xaDataSource.setPassword(ds.getPassword());
        if (pool != null) {
            if (pool.getLoginTimeout() > 0) {
                xaDataSource.setLoginTimeout(pool.getLoginTimeout());
            }
            if (pool.getIdleTimeout() > 0) {
                // Set the socket timeout so that connections that are stuck reading from
                // the database will be closed after the timeout
                xaDataSource.setSocketTimeout(pool.getIdleTimeout());
            }
        }
        setInstance(dsName, xaDataSource);
    } else {
        throw new UnsupportedOperationException("Data source scheme not supported: " + url.getScheme());
    }
}
Also used : ConnectionPool(org.opennms.netmgt.config.opennmsDataSources.ConnectionPool) JdbcDataSource(org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource) PGXADataSource(org.postgresql.xa.PGXADataSource) URI(java.net.URI)

Example 3 with ConnectionPool

use of org.opennms.netmgt.config.opennmsDataSources.ConnectionPool in project opennms by OpenNMS.

the class DataSourceConfigurationTest method data.

@Parameters
public static Collection<Object[]> data() throws Exception {
    DataSourceConfiguration config = new DataSourceConfiguration();
    ConnectionPool connectionPool = new ConnectionPool();
    connectionPool.setFactory("org.opennms.core.db.HikariCPConnectionFactory");
    connectionPool.setIdleTimeout(600);
    connectionPool.setLoginTimeout(3);
    connectionPool.setMinPool(50);
    connectionPool.setMaxPool(50);
    connectionPool.setMaxSize(50);
    config.setConnectionPool(connectionPool);
    JdbcDataSource opennmsDs = new JdbcDataSource();
    opennmsDs.setName("opennms");
    opennmsDs.setClassName("org.postgresql.Driver");
    opennmsDs.setUrl("jdbc:postgresql://localhost:5432/template1");
    opennmsDs.setUserName("opennms");
    opennmsDs.setPassword("opennms");
    config.addJdbcDataSource(opennmsDs);
    JdbcDataSource opennmsDeuceDs = new JdbcDataSource();
    opennmsDeuceDs.setName("opennms2");
    opennmsDeuceDs.setClassName("org.postgresql.Driver");
    opennmsDeuceDs.setUrl("jdbc:postgresql://localhost:5432/template1");
    opennmsDeuceDs.addParam(new Param("user", "opennms"));
    opennmsDeuceDs.addParam(new Param("password", "opennms"));
    config.addJdbcDataSource(opennmsDeuceDs);
    return Arrays.asList(new Object[][] { { config, new File("src/test/resources/org/opennms/core/db/opennms-datasources.xml") } });
}
Also used : ConnectionPool(org.opennms.netmgt.config.opennmsDataSources.ConnectionPool) JdbcDataSource(org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource) Param(org.opennms.netmgt.config.opennmsDataSources.Param) DataSourceConfiguration(org.opennms.netmgt.config.opennmsDataSources.DataSourceConfiguration) File(java.io.File) Parameters(org.junit.runners.Parameterized.Parameters)

Aggregations

ConnectionPool (org.opennms.netmgt.config.opennmsDataSources.ConnectionPool)3 JdbcDataSource (org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource)3 File (java.io.File)1 Constructor (java.lang.reflect.Constructor)1 URI (java.net.URI)1 SQLException (java.sql.SQLException)1 Parameters (org.junit.runners.Parameterized.Parameters)1 DataSourceConfiguration (org.opennms.netmgt.config.opennmsDataSources.DataSourceConfiguration)1 Param (org.opennms.netmgt.config.opennmsDataSources.Param)1 PGXADataSource (org.postgresql.xa.PGXADataSource)1