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;
}
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());
}
}
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") } });
}
Aggregations