private SymfonyDbFactory() throws Exception { try { Class.forName("org.h2.Driver"); } catch (ClassNotFoundException e) { Logger.logException(e); } IPath dbPath = SymfonyIndex.getDefault().getStateLocation(); String connString = getConnectionString(dbPath); pool = JdbcConnectionPool.create(connString, DB_USER, DB_PASS); Schema schema = new Schema(); boolean initializeSchema = false; int tries = 2; // Tries for opening database Connection connection = null; do { try { connection = pool.getConnection(); try { Statement statement = connection.createStatement(); try { statement.executeQuery("SELECT COUNT(*) FROM SERVICES WHERE 1=0;"); initializeSchema = !schema.isCompatible(); } catch (SQLException e) { // Basic table doesn't exist initializeSchema = true; } finally { statement.close(); } if (initializeSchema) { connection.close(); pool.dispose(); // Destroy schema by removing DB (if exists) DeleteDbFiles.execute(dbPath.toOSString(), DB_NAME, true); pool = JdbcConnectionPool.create(connString, DB_USER, DB_PASS); connection = pool.getConnection(); schema.initialize(connection); } } finally { if (connection != null) { connection.close(); } } } catch (SQLException e) { Logger.logException(e); // remove corrupted DB try { DeleteDbFiles.execute(dbPath.toOSString(), DB_NAME, true); } catch (Exception e1) { Logger.logException(e1); throw e1; } } } while (connection == null && --tries > 0); }
public Connection getConnection() throws Exception { return connectionPool.getConnection(); }
public Connection createConnection() throws SQLException { return pool == null ? null : pool.getConnection(); }
/** * Gets a new connection from the connection pool if one is available. If not waits for a free * slot until timeout.<br> * <br> * <strong>Important: Every connection must be closed after use</strong> * * @return the new connection * @throws SQLException */ public Connection getConnection() throws SQLException { return cp.getConnection(); }
private Connection getConnection() throws SQLException { return cp.getConnection(); }
@Override public Connection getConnection() throws SQLException { return pool.getConnection(); }
@Override public Object getObject() throws Exception { if (connectionPool == null) { // locate the repository directory String repositoryDirectoryPath = properties.getProperty(NiFiProperties.REPOSITORY_DATABASE_DIRECTORY); // ensure the repository directory is specified if (repositoryDirectoryPath == null) { throw new NullPointerException("Database directory must be specified."); } // create a handle to the repository directory File repositoryDirectory = new File(repositoryDirectoryPath); // create a handle to the database directory and file File databaseFile = new File(repositoryDirectory, AUDIT_DATABASE_FILE_NAME); String databaseUrl = getDatabaseUrl(databaseFile); // create the pool connectionPool = JdbcConnectionPool.create(databaseUrl, NF_USERNAME_PASSWORD, NF_USERNAME_PASSWORD); connectionPool.setMaxConnections(MAX_CONNECTIONS); Connection connection = null; ResultSet rs = null; Statement statement = null; try { // get a connection connection = connectionPool.getConnection(); connection.setAutoCommit(false); // create a statement for creating/updating the database statement = connection.createStatement(); // determine if the tables need to be created rs = connection.getMetaData().getTables(null, null, "USER", null); if (!rs.next()) { logger.info("Database not built for repository: " + databaseUrl + ". Building now..."); // create the tables statement.execute(CREATE_USER_TABLE); statement.execute(CREATE_AUTHORITY_TABLE); // seed the anonymous user statement.execute(INSERT_ANONYMOUS_USER); statement.execute(INSERT_ANONYMOUS_MONITOR_AUTHORITY); statement.execute(INSERT_ANONYMOUS_DFM_AUTHORITY); statement.execute(INSERT_ANONYMOUS_ADMIN_AUTHORITY); statement.execute(INSERT_ANONYMOUS_NIFI_AUTHORITY); } else { logger.info("Existing database found and connected to at: " + databaseUrl); } // close the previous result set RepositoryUtils.closeQuietly(rs); // merge in the provenance role to handle existing databases rs = statement.executeQuery(SELECT_ANONYMOUS_PROVENANCE_AUTHORITY); if (!rs.next()) { statement.execute(INSERT_ANONYMOUS_PROVENANCE_AUTHORITY); } // commit any changes connection.commit(); } catch (SQLException sqle) { RepositoryUtils.rollback(connection, logger); throw sqle; } finally { RepositoryUtils.closeQuietly(rs); RepositoryUtils.closeQuietly(statement); RepositoryUtils.closeQuietly(connection); } } return connectionPool; }