Search in sources :

Example 1 with ConnectionException

use of org.neo4j.ogm.exception.ConnectionException in project neo4j-ogm by neo4j.

the class BoltDriver method newSession.

private Session newSession(Transaction.Type type, Iterable<String> bookmarks) {
    Session boltSession;
    try {
        AccessMode accessMode = type.equals(Transaction.Type.READ_ONLY) ? AccessMode.READ : AccessMode.WRITE;
        SessionConfig.Builder sessionConfigBuilder = SessionConfig.builder().withDefaultAccessMode(accessMode).withBookmarks(bookmarksFromStrings(bookmarks));
        if (this.database != null) {
            sessionConfigBuilder = sessionConfigBuilder.withDatabase(database);
        }
        boltSession = boltDriver.session(sessionConfigBuilder.build());
    } catch (ClientException ce) {
        throw new ConnectionException("Error connecting to graph database using Bolt: " + ce.code() + ", " + ce.getMessage(), ce);
    } catch (Exception e) {
        throw new ConnectionException("Error connecting to graph database using Bolt", e);
    }
    return boltSession;
}
Also used : ClientException(org.neo4j.driver.exceptions.ClientException) ConnectionException(org.neo4j.ogm.exception.ConnectionException) ConnectionException(org.neo4j.ogm.exception.ConnectionException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ClientException(org.neo4j.driver.exceptions.ClientException)

Example 2 with ConnectionException

use of org.neo4j.ogm.exception.ConnectionException in project neo4j-ogm by neo4j.

the class BoltDriver method initializeDriver.

private void initializeDriver() {
    final String serviceUnavailableMessage = "Could not create driver instance";
    Driver driver = null;
    try {
        if (credentials != null) {
            UsernamePasswordCredentials usernameAndPassword = (UsernamePasswordCredentials) this.credentials;
            AuthToken authToken = AuthTokens.basic(usernameAndPassword.getUsername(), usernameAndPassword.getPassword());
            driver = createDriver(authToken);
        } else {
            LOGGER.debug("Bolt Driver credentials not supplied");
            driver = createDriver(AuthTokens.none());
        }
        driver.verifyConnectivity();
        boltDriver = driver;
        // set null to skip close() in finally
        driver = null;
    } catch (ServiceUnavailableException e) {
        throw new ConnectionException(serviceUnavailableMessage, e);
    } finally {
        if (driver != null) {
            driver.close();
        }
    }
}
Also used : AbstractConfigurableDriver(org.neo4j.ogm.driver.AbstractConfigurableDriver) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ConnectionException(org.neo4j.ogm.exception.ConnectionException) UsernamePasswordCredentials(org.neo4j.ogm.config.UsernamePasswordCredentials)

Example 3 with ConnectionException

use of org.neo4j.ogm.exception.ConnectionException in project neo4j-ogm by neo4j.

the class BoltTransaction method commit.

@Override
public void commit() {
    final boolean canCommit = transactionManager.canCommit();
    try {
        if (canCommit) {
            LOGGER.debug("Committing native transaction: {}", nativeTransaction);
            if (nativeTransaction.isOpen()) {
                nativeTransaction.commit();
                nativeTransaction.close();
                nativeSession.close();
            } else {
                throw new IllegalStateException("Transaction is already closed");
            }
        }
    } catch (ClientException ce) {
        closeNativeSessionIfPossible();
        if (ce.code().startsWith(NEO_CLIENT_ERROR_SECURITY)) {
            throw new ConnectionException("Security Error: " + ce.code() + ", " + ce.getMessage(), ce);
        }
        throw new CypherException(ce.code(), ce.getMessage(), ce);
    } catch (Exception e) {
        closeNativeSessionIfPossible();
        throw new TransactionException(e.getLocalizedMessage(), e);
    } finally {
        super.commit();
        if (canCommit) {
            Bookmark bookmark = nativeSession.lastBookmark();
            if (bookmark != null) {
                String bookmarkAsString = String.join(BOOKMARK_SEPARATOR, ((InternalBookmark) bookmark).values());
                transactionManager.bookmark(bookmarkAsString);
            }
        }
    }
}
Also used : TransactionException(org.neo4j.ogm.exception.TransactionException) InternalBookmark(org.neo4j.driver.internal.InternalBookmark) Bookmark(org.neo4j.driver.Bookmark) ClientException(org.neo4j.driver.exceptions.ClientException) CypherException(org.neo4j.ogm.exception.CypherException) ConnectionException(org.neo4j.ogm.exception.ConnectionException) CypherException(org.neo4j.ogm.exception.CypherException) ConnectionException(org.neo4j.ogm.exception.ConnectionException) TransactionException(org.neo4j.ogm.exception.TransactionException) ClientException(org.neo4j.driver.exceptions.ClientException)

Example 4 with ConnectionException

use of org.neo4j.ogm.exception.ConnectionException in project neo4j-ogm by neo4j.

the class EmbeddedDriver method configure.

@Override
public synchronized void configure(Configuration newConfiguration) {
    super.configure(newConfiguration);
    this.database = this.configuration.getDatabase();
    try {
        String fileStoreUri = newConfiguration.getURI();
        // This is effectively what the ImpermanentDatabase does.
        if (fileStoreUri == null) {
            fileStoreUri = createTemporaryFileStore();
        } else {
            createPermanentFileStore(fileStoreUri);
        }
        File file = new File(new URI(fileStoreUri));
        if (!file.exists()) {
            throw new RuntimeException("Could not create/open filestore: " + fileStoreUri);
        }
        DatabaseManagementServiceBuilder graphDatabaseBuilder = getGraphDatabaseFactory(newConfiguration, file);
        graphDatabaseBuilder.setConfigRaw(Collections.singletonMap("dbms.backup.enabled", "false"));
        graphDatabaseBuilder.setConfigRaw(Collections.singletonMap("dbms.clustering.enable", "false"));
        graphDatabaseBuilder.setConfigRaw(Collections.singletonMap("dbms.memory.pagecache.warmup.enable", "false"));
        graphDatabaseBuilder.setConfigRaw(Collections.singletonMap("metrics.enabled", "false"));
        String neo4jConfLocation = newConfiguration.getNeo4jConfLocation();
        if (neo4jConfLocation != null) {
            URL neo4ConfUrl = newConfiguration.getResourceUrl(neo4jConfLocation);
            Path tmp = Files.createTempFile("neo4j-ogm-embedded", ".conf");
            try (BufferedReader in = new BufferedReader(new InputStreamReader(neo4ConfUrl.openStream()));
                BufferedWriter out = Files.newBufferedWriter(tmp, StandardCharsets.UTF_8)) {
                in.transferTo(out);
                out.flush();
            }
            graphDatabaseBuilder = graphDatabaseBuilder.loadPropertiesFromFile(tmp.toFile().getAbsolutePath());
        }
        this.databaseManagementService = graphDatabaseBuilder.build();
        this.graphDatabaseService = this.databaseManagementService.database(getDatabase());
    } catch (Exception e) {
        throw new ConnectionException("Error connecting to embedded graph", e);
    }
}
Also used : Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) BufferedReader(java.io.BufferedReader) File(java.io.File) URI(java.net.URI) URL(java.net.URL) ConnectionException(org.neo4j.ogm.exception.ConnectionException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) ConnectionException(org.neo4j.ogm.exception.ConnectionException) BufferedWriter(java.io.BufferedWriter)

Example 5 with ConnectionException

use of org.neo4j.ogm.exception.ConnectionException in project neo4j-ogm by neo4j.

the class BoltDriver method buildDriverConfig.

private Config buildDriverConfig() {
    // Done outside the try/catch and explicity catch the illegalargument exception of singleURI
    // so that exception semantics are not changed since we introduced that feature.
    // 
    // GraphDatabase.routingDriver asserts `neo4j` scheme for each URI, so our trust settings
    // have to be applied in this case.
    final boolean shouldApplyEncryptionAndTrustSettings;
    if (isRoutingConfig()) {
        shouldApplyEncryptionAndTrustSettings = true;
    } else {
        // Otherwise we check if it comes with the scheme or not.
        URI singleUri = null;
        try {
            singleUri = getSingleURI();
        } catch (IllegalArgumentException e) {
        }
        shouldApplyEncryptionAndTrustSettings = singleUri == null || isSimpleScheme(singleUri.getScheme());
    }
    try {
        Config.ConfigBuilder configBuilder = Config.builder();
        configBuilder.withMaxConnectionPoolSize(configuration.getConnectionPoolSize());
        if (shouldApplyEncryptionAndTrustSettings) {
            applyEncryptionAndTrustSettings(configBuilder);
        }
        if (configuration.getConnectionLivenessCheckTimeout() != null) {
            configBuilder.withConnectionLivenessCheckTimeout(configuration.getConnectionLivenessCheckTimeout(), TimeUnit.MILLISECONDS);
        }
        getBoltLogging().ifPresent(configBuilder::withLogging);
        return configBuilder.build();
    } catch (Exception e) {
        throw new ConnectionException("Unable to build driver configuration", e);
    }
}
Also used : URI(java.net.URI) ConnectionException(org.neo4j.ogm.exception.ConnectionException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ClientException(org.neo4j.driver.exceptions.ClientException) ConnectionException(org.neo4j.ogm.exception.ConnectionException)

Aggregations

ConnectionException (org.neo4j.ogm.exception.ConnectionException)6 ClientException (org.neo4j.driver.exceptions.ClientException)3 ServiceUnavailableException (org.neo4j.driver.exceptions.ServiceUnavailableException)3 IOException (java.io.IOException)2 URI (java.net.URI)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 InputStreamReader (java.io.InputStreamReader)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 Path (java.nio.file.Path)1 HttpEntity (org.apache.http.HttpEntity)1 NoHttpResponseException (org.apache.http.NoHttpResponseException)1 StatusLine (org.apache.http.StatusLine)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 HttpResponseException (org.apache.http.client.HttpResponseException)1