Search in sources :

Example 1 with PGXADataSource

use of org.postgresql.xa.PGXADataSource in project opennms by OpenNMS.

the class TemporaryDatabasePostgreSQL method setupDatabase.

public void setupDatabase() throws TemporaryDatabaseException {
    try {
        setDataSource(new SimpleDataSource(m_driver, m_url + getTestDatabase(), m_adminUser, m_adminPassword));
        setAdminDataSource(new SimpleDataSource(m_driver, m_url + "template1", m_adminUser, m_adminPassword));
        m_xaDataSource = new PGXADataSource();
        m_xaDataSource.setServerName("localhost");
        m_xaDataSource.setDatabaseName(getTestDatabase());
        m_xaDataSource.setUser(m_adminUser);
        m_xaDataSource.setPassword(m_adminPassword);
        m_adminXaDataSource = new PGXADataSource();
        m_adminXaDataSource.setServerName("localhost");
        m_adminXaDataSource.setDatabaseName("template1");
        m_adminXaDataSource.setUser(m_adminUser);
        m_adminXaDataSource.setPassword(m_adminPassword);
    } catch (final ClassNotFoundException e) {
        throw new TemporaryDatabaseException("Failed to initialize driver " + m_driver, e);
    }
    if (!m_useExisting) {
        // to the template1 database
        synchronized (TEMPLATE1_MUTEX) {
            createTestDatabase();
        }
    }
    setJdbcTemplate(new JdbcTemplate(this));
    // Test connecting to test database and ensuring we can do a basic query
    try {
        getJdbcTemplate().queryForObject("SELECT now()", Date.class);
    } catch (DataAccessException e) {
        throw new TemporaryDatabaseException("Error occurred while testing database is connectable: " + e.getMessage(), e);
    }
    if (!m_useExisting) {
        setupBlame(getJdbcTemplate(), getBlame());
    }
}
Also used : SimpleDataSource(org.opennms.core.db.install.SimpleDataSource) PGXADataSource(org.postgresql.xa.PGXADataSource) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataAccessException(org.springframework.dao.DataAccessException)

Example 2 with PGXADataSource

use of org.postgresql.xa.PGXADataSource 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 PGXADataSource

use of org.postgresql.xa.PGXADataSource in project API by ca-cwds.

the class XASample method buildPGDataSource.

private static PGXADataSource buildPGDataSource(String user, String password, String serverName, int port, String databaseName) {
    PGXADataSource ds = new PGXADataSource();
    ds.setUser(user);
    ds.setPassword(password);
    ds.setServerName(serverName);
    ds.setPortNumber(port);
    ds.setDatabaseName(databaseName);
    try {
        ds.setProperty("user", user);
        ds.setProperty("password", password);
        ds.setProperty(PGProperty.PG_DBNAME, databaseName);
        ds.setProperty(PGProperty.PG_HOST, serverName);
        ds.setProperty(PGProperty.PG_PORT, String.valueOf(port));
    } catch (SQLException e) {
        e.printStackTrace();
        throw new ApiException("datasource property error", e);
    }
    System.out.println("pgxa data source");
    return ds;
}
Also used : SQLException(java.sql.SQLException) PGXADataSource(org.postgresql.xa.PGXADataSource) ApiException(gov.ca.cwds.rest.api.ApiException)

Aggregations

PGXADataSource (org.postgresql.xa.PGXADataSource)3 ApiException (gov.ca.cwds.rest.api.ApiException)1 URI (java.net.URI)1 SQLException (java.sql.SQLException)1 SimpleDataSource (org.opennms.core.db.install.SimpleDataSource)1 ConnectionPool (org.opennms.netmgt.config.opennmsDataSources.ConnectionPool)1 JdbcDataSource (org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource)1 DataAccessException (org.springframework.dao.DataAccessException)1 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)1