private Connection getConnectionFromDriverManager() { try { return DriverManager.getConnection( "jdbc:postgresql://" + loginCredentials.getHost() + "/" + loginCredentials.getDatabase(), // + "?logLevel=2" loginCredentials.getUser(), loginCredentials.getPassword() ); } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to establish a new database connection.", e); } }
/** * Returns the database type currently in use. This should only be used when it is not possible * to write database agnostic statements. * * @return The database type. */ public DatabaseType getDatabaseType() { return loginCredentials.getDbType(); }
/** * @return The mysql database connection. */ private Connection getMysqlConnection() { Connection newConnection = null; try { String url; url = "jdbc:mysql://" + loginCredentials.getHost() + "/" + loginCredentials.getDatabase() + "?user=" + loginCredentials.getUser() + "&password=" + loginCredentials.getPassword(); if (loginCredentials.getForceUtf8()) { url += "&useUnicode=true&characterEncoding=UTF-8"; } if (loginCredentials.getProfileSql()) { url += "&profileSql=true"; } newConnection = DriverManager.getConnection(url); newConnection.setAutoCommit(autoCommit); } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to establish a database connection.", e); } return newConnection; }
loginCredentials = new DatabaseLoginCredentials(DatabaseConstants.TASK_DEFAULT_HOST, DatabaseConstants.TASK_DEFAULT_DATABASE, DatabaseConstants.TASK_DEFAULT_USER, DatabaseConstants.TASK_DEFAULT_PASSWORD, DatabaseConstants.TASK_DEFAULT_FORCE_UTF8, loginCredentials.setHost(getStringArgument(taskConfig, DatabaseConstants.TASK_ARG_HOST, loginCredentials .getHost())); loginCredentials.setDatabase(getStringArgument(taskConfig, DatabaseConstants.TASK_ARG_DATABASE, loginCredentials.getDatabase())); loginCredentials.setUser(getStringArgument(taskConfig, DatabaseConstants.TASK_ARG_USER, loginCredentials .getUser())); loginCredentials.setPassword(getStringArgument(taskConfig, DatabaseConstants.TASK_ARG_PASSWORD, loginCredentials.getPassword())); loginCredentials.setForceUtf8(getBooleanArgument(taskConfig, DatabaseConstants.TASK_ARG_FORCE_UTF8, loginCredentials.getForceUtf8())); loginCredentials.setProfileSql(getBooleanArgument(taskConfig, DatabaseConstants.TASK_ARG_PROFILE_SQL, loginCredentials.getProfileSql())); loginCredentials.setDbType(getStringArgument(taskConfig, DatabaseConstants.TASK_ARG_DB_TYPE, loginCredentials .getDbType().toString())); loginCredentials.setPostgresSchema(getStringArgument(taskConfig, DatabaseConstants.TASK_ARG_POSTGRES_SCHEMA, loginCredentials.getPostgresSchema().toString()));
/** * Provides a fully configured set of database login credentials based on the configuration. * * @return The database login credentials. */ public DatabaseLoginCredentials getDatabaseLoginCredentials() { return new DatabaseLoginCredentials(getHost(), getDatabase(), getUser(), getPassword(), false, false, getDbType()); }
/** * Creates a new instance. * * @param loginCredentials Contains all information required to connect to the database. */ public DatabaseContext(DatabaseLoginCredentials loginCredentials) { dataSourceManager = new DataSourceManager(loginCredentials); dataSource = dataSourceManager.getDataSource(); txnManager = new DataSourceTransactionManager(dataSource); txnTemplate = new TransactionTemplate(txnManager); jdbcTemplate = new JdbcTemplate(dataSource); setStatementFetchSizeForStreaming(); if (loginCredentials.getPostgresSchema() != "") { // JJ: should prepend to old search_path jdbcTemplate.execute("SELECT set_config('search_path', '" + loginCredentials.getPostgresSchema() + ",' || current_setting('search_path'), false)"); } }
loginCredentials = new DatabaseLoginCredentials(DatabaseConstants.TASK_DEFAULT_HOST, DatabaseConstants.TASK_DEFAULT_DATABASE, DatabaseConstants.TASK_DEFAULT_USER, DatabaseConstants.TASK_DEFAULT_PASSWORD, DatabaseConstants.TASK_DEFAULT_FORCE_UTF8, loginCredentials.setHost(getStringArgument(taskConfig, DatabaseConstants.TASK_ARG_HOST, loginCredentials .getHost())); loginCredentials.setDatabase(getStringArgument(taskConfig, DatabaseConstants.TASK_ARG_DATABASE, loginCredentials.getDatabase())); loginCredentials.setUser(getStringArgument(taskConfig, DatabaseConstants.TASK_ARG_USER, loginCredentials .getUser())); loginCredentials.setPassword(getStringArgument(taskConfig, DatabaseConstants.TASK_ARG_PASSWORD, loginCredentials.getPassword())); loginCredentials.setForceUtf8(getBooleanArgument(taskConfig, DatabaseConstants.TASK_ARG_FORCE_UTF8, loginCredentials.getForceUtf8())); loginCredentials.setProfileSql(getBooleanArgument(taskConfig, DatabaseConstants.TASK_ARG_PROFILE_SQL, loginCredentials.getProfileSql())); loginCredentials.setDbType(getStringArgument(taskConfig, DatabaseConstants.TASK_ARG_DB_TYPE, loginCredentials .getDbType().toString())); loginCredentials.setPostgresSchema(getStringArgument(taskConfig, DatabaseConstants.TASK_ARG_POSTGRES_SCHEMA, loginCredentials.getPostgresSchema().toString()));
/** * Creates a new database context pointing at the test database. * * @return A fully configured database context. */ public DatabaseContext createDatabaseContext() { AuthenticationPropertiesLoader credentialsLoader; DatabaseLoginCredentials credentials; credentials = new DatabaseLoginCredentials(DatabaseConstants.TASK_DEFAULT_HOST, DatabaseConstants.TASK_DEFAULT_DATABASE, DatabaseConstants.TASK_DEFAULT_USER, DatabaseConstants.TASK_DEFAULT_PASSWORD, DatabaseConstants.TASK_DEFAULT_FORCE_UTF8, DatabaseConstants.TASK_DEFAULT_PROFILE_SQL, DatabaseConstants.TASK_DEFAULT_DB_TYPE); credentialsLoader = new AuthenticationPropertiesLoader(getAuthorizationFile()); credentialsLoader.updateLoginCredentials(credentials); return new DatabaseContext(credentials); }
/** * @return postgres connection */ private Connection getPostgresConnection() { Connection newConnection = null; try { LOG.finer("Creating a new database connection."); newConnection = DriverManager.getConnection( "jdbc:postgresql://" + loginCredentials.getHost() + "/" + loginCredentials.getDatabase(), // + "?logLevel=2" loginCredentials.getUser(), loginCredentials.getPassword() ); newConnection.setAutoCommit(autoCommit); } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to establish a database connection.", e); } return newConnection; }
/** * @return The mysql database connection. */ private Connection getMysqlConnection() { Connection newConnection = null; try { String url; url = "jdbc:mysql://" + loginCredentials.getHost() + "/" + loginCredentials.getDatabase() + "?user=" + loginCredentials.getUser() + "&password=" + loginCredentials.getPassword(); if (loginCredentials.getForceUtf8()) { url += "&useUnicode=true&characterEncoding=UTF-8"; } if (loginCredentials.getProfileSql()) { url += "&profileSql=true"; } newConnection = DriverManager.getConnection(url); newConnection.setAutoCommit(autoCommit); } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to establish a database connection.", e); } return newConnection; }
/** * Returns the database type currently in use. This should only be used when it is not possible * to write database agnostic statements. * * @return The database type. */ public DatabaseType getDatabaseType() { return loginCredentials.getDbType(); }
/** * Creates a new database context pointing at the test database. * * @return A fully configured database context. */ public DatabaseContext createDatabaseContext() { AuthenticationPropertiesLoader credentialsLoader; DatabaseLoginCredentials credentials; credentials = new DatabaseLoginCredentials(DatabaseConstants.TASK_DEFAULT_HOST, DatabaseConstants.TASK_DEFAULT_DATABASE, DatabaseConstants.TASK_DEFAULT_USER, DatabaseConstants.TASK_DEFAULT_PASSWORD, DatabaseConstants.TASK_DEFAULT_FORCE_UTF8, DatabaseConstants.TASK_DEFAULT_PROFILE_SQL, DatabaseConstants.TASK_DEFAULT_DB_TYPE); credentialsLoader = new AuthenticationPropertiesLoader(getAuthorizationFile()); credentialsLoader.updateLoginCredentials(credentials); return new DatabaseContext(credentials); }
/** * @return postgres connection */ private Connection getPostgresConnection() { Connection newConnection = null; try { LOG.finer("Creating a new database connection."); newConnection = DriverManager.getConnection( "jdbc:postgresql://" + loginCredentials.getHost() + "/" + loginCredentials.getDatabase(), // + "?logLevel=2" loginCredentials.getUser(), loginCredentials.getPassword() ); newConnection.setAutoCommit(autoCommit); } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to establish a database connection.", e); } return newConnection; }
private OsmosisRuntimeException createUnknownDbTypeException() { return new OsmosisRuntimeException("Unknown database type " + loginCredentials.getDbType() + "."); }
private Connection createConnectionFromDriverManager() { try { // Register the database driver. try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { throw new OsmosisRuntimeException("Unable to find database driver.", e); } return DriverManager.getConnection( "jdbc:postgresql://" + credentials.getHost() + "/" + credentials.getDatabase(), // + "?logLevel=2" credentials.getUser(), credentials.getPassword() ); } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to establish a new database connection.", e); } }
private OsmosisRuntimeException createUnknownDbTypeException() { return new OsmosisRuntimeException("Unknown database type " + loginCredentials.getDbType() + "."); }
private void createDataSource() { localDataSource = new BasicDataSource(); localDataSource.setDriverClassName("org.postgresql.Driver"); localDataSource.setUrl("jdbc:postgresql://" + credentials.getHost() + "/" + credentials.getDatabase() /*+ "?loglevel=2"*/); localDataSource.setUsername(credentials.getUser()); localDataSource.setPassword(credentials.getPassword()); dataSource = localDataSource; }
private void setStatementFetchSizeForStreaming(Statement streamingStatement) { try { switch (loginCredentials.getDbType()) { case POSTGRESQL: streamingStatement.setFetchSize(10000); break; case MYSQL: streamingStatement.setFetchSize(Integer.MIN_VALUE); break; default: throw createUnknownDbTypeException(); } } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to update statement fetch size.", e); } }
switch (credentials.getDbType()) { case POSTGRESQL: dataSource.setDriverClassName("org.postgresql.Driver"); dataSource.setUrl("jdbc:postgresql://" + credentials.getHost() + "/" + credentials.getDatabase() dataSource.setUrl("jdbc:mysql://" + credentials.getHost() + "/" + credentials.getDatabase()); break; default: throw new OsmosisRuntimeException("Unknown database type " + credentials.getDbType() + "."); dataSource.setUsername(credentials.getUser()); dataSource.setPassword(credentials.getPassword());
private void setStatementFetchSizeForStreaming(Statement streamingStatement) { try { switch (loginCredentials.getDbType()) { case POSTGRESQL: streamingStatement.setFetchSize(10000); break; case MYSQL: streamingStatement.setFetchSize(Integer.MIN_VALUE); break; default: throw createUnknownDbTypeException(); } } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to update statement fetch size.", e); } }