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;
}
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();
}
}
}
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);
}
}
}
}
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);
}
}
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);
}
}
Aggregations