/** * Releases all database resources. This method is guaranteed not to throw transactions and * should always be called in a finally block whenever this class is used. */ public void close() { if (transaction != null) { try { txnManager.rollback(transaction); } finally { transaction = null; } } dataSourceManager.close(); }
/** * Obtains a data source. * * @return The database source. */ public DataSource getDataSource() { if (dataSource == null) { String jndiLocation; jndiLocation = credentials.getDatasourceJndiLocation(); if (jndiLocation != null) { LOG.finer("Retrieving a data source from JNDI."); loadDatasource(); } else { LOG.finer("Creating a new locally managed data source."); createDataSource(); } } return dataSource; }
/** * Obtains a single connection. * * @return The connection. */ public Connection getConnection() { String jndiLocation; Connection connection; jndiLocation = credentials.getDatasourceJndiLocation(); if (dataSource == null && jndiLocation != null) { LOG.finer("Retrieving a data source from JNDI."); loadDatasource(); } if (dataSource == null) { LOG.finer("Creating a new database connection from DriverManager."); connection = createConnectionFromDriverManager(); } else { try { connection = dataSource.getConnection(); } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to obtain a connection from the datasource.", e); } } return connection; }
/** * 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)"); } }