use of org.postgresql.ds.PGSimpleDataSource in project jdbi by jdbi.
the class JdbiExternalPostgresExtension method createDataSource.
@Override
protected DataSource createDataSource() throws Exception {
final PGSimpleDataSource datasource = new PGSimpleDataSource();
datasource.setServerNames(new String[] { hostname });
if (port != null) {
datasource.setPortNumbers(new int[] { port });
}
datasource.setUser(username);
datasource.setPassword(password);
datasource.setDatabaseName(database);
datasource.setApplicationName(this.getClass().getSimpleName());
return datasource;
}
use of org.postgresql.ds.PGSimpleDataSource in project sis by apache.
the class TestDatabase method createOnPostgreSQL.
/**
* Creates a connection to an existing PostgreSQL database.
* This method returns only if all the following conditions are true:
*
* <ol>
* <li>{@link TestCase#RUN_EXTENSIVE_TESTS} is {@code true} (for reducing the risk of messing with user installation).</li>
* <li>A PostgreSQL server is running on the local host and listening to the default port.</li>
* <li>A database named {@value #NAME} exists.</li>
* <li>A role with Unix user name exists and can connect to the database without password.</li>
* <li>The database does not contain any schema of the given name.</li>
* </ol>
*
* If the {@code create} argument is {@code false}, then the callers is responsible for creating the schema
* soon after this method call. That schema will be deleted by {@link #close()}.
*
* @param schema temporary schema to create. Shall not contain {@code '_'} or {@code '%'} characters.
* @param create whether the schema should be created by this method.
* @return connection to a PostgreSQL database
* @throws SQLException if an error occurred while connecting to the database or creating the schema.
*
* @see <a href="http://sis.apache.org/source.html#postgres">Configuring PostgreSQL for Apache SIS tests</a>
*
* @since 1.0
*/
public static TestDatabase createOnPostgreSQL(final String schema, final boolean create) throws SQLException {
assumeTrue("Extensive tests not enabled.", TestCase.RUN_EXTENSIVE_TESTS);
final PGSimpleDataSource ds = new PGSimpleDataSource();
// Server default to "localhost".
ds.setDatabaseName(NAME);
ds.setApplicationName("Apache SIS test database");
ds.setCurrentSchema(schema);
// For avoiding warning when no PostgreSQL server is running.
ds.setProperty(PGProperty.LOGGER_LEVEL, "OFF");
/*
* Current version does not use pooling on the assumption that connections to local host are fast enough.
* We verify that the schema does not exist, even if the `create` argument is `false`, because we assume
* that the test is going to create the schema soon (see the contract documented in method javadoc).
* This is also a safety for avoiding to delete a schema that was not created by the test case.
*/
try (Connection c = ds.getConnection()) {
try (ResultSet reflect = c.getMetaData().getSchemas(null, schema)) {
if (reflect.next()) {
throw new SQLDataException("Schema \"" + schema + "\" already exists in \"" + NAME + "\".");
}
}
if (create) {
try (Statement s = c.createStatement()) {
s.execute("CREATE SCHEMA \"" + schema + '"');
}
}
} catch (SQLException e) {
final String state = e.getSQLState();
assumeFalse("This test needs a PostgreSQL server running on the local host.", "08001".equals(state));
assumeFalse("This test needs a PostgreSQL database named \"" + NAME + "\".", "3D000".equals(state));
throw e;
}
return new TestDatabase(ds) {
@Override
public void close() throws SQLException {
final PGSimpleDataSource ds = (PGSimpleDataSource) source;
try (Connection c = ds.getConnection()) {
try (Statement s = c.createStatement()) {
/*
* Prevents test to hang indefinitely if connections are not properly released in test cases.
* If the limit (in seconds) is exceeded, an SQLTimeoutException is thrown and test fails.
*/
s.setQueryTimeout(10);
s.execute("DROP SCHEMA \"" + ds.getCurrentSchema() + "\" CASCADE");
}
}
}
};
}
use of org.postgresql.ds.PGSimpleDataSource in project micrometer by micrometer-metrics.
the class PostgreSQLDatabaseMetricsIntegrationTest method createDataSource.
private DataSource createDataSource() {
final PGSimpleDataSource dataSource = new PGSimpleDataSource();
dataSource.setURL(postgres.getJdbcUrl());
dataSource.setUser(postgres.getUsername());
dataSource.setPassword(postgres.getPassword());
dataSource.setDatabaseName(postgres.getDatabaseName());
return dataSource;
}
use of org.postgresql.ds.PGSimpleDataSource in project trellis by trellis-ldp.
the class DBTestUtils method initialize.
static void initialize() {
postgres.start();
final PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setURL(postgres.getJdbcUrl());
ds.setUser(postgres.getUsername());
ds.setPassword(postgres.getPassword());
try {
// Set up database migrations
try (final Connection c = ds.getConnection()) {
final Liquibase liquibase = new Liquibase("org/trellisldp/jdbc/migrations.yml", new ClassLoaderResourceAccessor(), new JdbcConnection(c));
final Contexts ctx = null;
liquibase.update(ctx);
}
} catch (final SQLException | LiquibaseException ex) {
LOGGER.error("Error setting up tests", ex);
}
datasource = ds;
}
use of org.postgresql.ds.PGSimpleDataSource in project jobrunr by jobrunr.
the class PostgresJacksonBackgroundJobContainer method initStorageProvider.
@Override
protected StorageProvider initStorageProvider(JdbcDatabaseContainer sqlContainer) {
PGSimpleDataSource dataSource = new PGSimpleDataSource();
dataSource.setURL(sqlContainer.getJdbcUrl());
dataSource.setUser(sqlContainer.getUsername());
dataSource.setPassword(sqlContainer.getPassword());
return SqlStorageProviderFactory.using(dataSource);
}
Aggregations