Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a way to configure the database being used.
This fixes #124 by providing the a Neo4jDatabaseNameProvider. The Neo4jDatabaseNameProvider allows for configuring the database used by all transactions managers and all templates. The configured database will be passed on to the Neo4j templates, both reactive and imperative. The Neo4j clients are still be able to use all databases, they don't know a fixed database name provider. The database name provider must of course be provided to the transaction managers. Both reactive and imperative transaction managers keep on synchronising on the driver, but check the target database and will prevent switching the database in-between an application level transaction. For those scenarios where a client wants to use a different database than the one configured with the default transaction manager, it must run in an implicit (aka auto commit transaction) or run a second transaction manager, configured to use the correct database. In case interactions spawn multiple databases, several transaction managers must be applied and the propagation behaviour needs to be `REQUIRES_NEW`, meaning an ongoing transaction will be suspended when the database is switched and resumed when the inner transaction finishes. The commit also contains the requried properties for the Spring Boot autoconfiguration.
- Loading branch information
1 parent
f093001
commit 505d965
Showing
27 changed files
with
572 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...ta-neo4j/src/main/java/org/neo4j/springframework/data/core/Neo4jDatabaseNameProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) 2019 "Neo4j," | ||
* Neo4j Sweden AB [https://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.neo4j.springframework.data.core; | ||
|
||
import java.util.Optional; | ||
|
||
import org.apiguardian.api.API; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* A provider interface that knows in which database repositories or either the reactive or imperative template should work. | ||
* <p>An instance of a database name provider is only relevant when SDN-RX is used with a Neo4j 4.0+ cluster or server. | ||
* <p>To select the default database, return an empty optional. If you return a database name, it must not be empty. | ||
* The empty optional indicates an unset database name on the client, so that the server can decide on the default to use. | ||
* <p>The provider is asked before any interaction of a repository or template with the cluster or server. That means you can | ||
* in theory return different database names for each interaction. Be aware that you might end up with no data on queries | ||
* or data stored to wrong database if you don't pay meticulously attention to the database you interact with. | ||
* | ||
* @author Michael J. Simons | ||
* @soundtrack N.W.A. - Straight Outta Compton | ||
* @since 1.0 | ||
*/ | ||
@API(status = API.Status.STABLE, since = "1.0") | ||
@FunctionalInterface | ||
public interface Neo4jDatabaseNameProvider { | ||
|
||
/** | ||
* @return The optional name of the database name to interact with. Use the empty optional to indicate the default database. | ||
*/ | ||
Optional<String> getCurrentDatabaseName(); | ||
|
||
/** | ||
* Creates a statically configured database name provider always answering with the configured {@code databaseName}. | ||
* | ||
* @param databaseName The database name to use, must not be null nor empty. | ||
* @return A statically configured database name provider. | ||
*/ | ||
static Neo4jDatabaseNameProvider createStaticDatabaseNameProvider(String databaseName) { | ||
|
||
Assert.notNull(databaseName, "The database name must not be null."); | ||
Assert.hasText(databaseName, "The database name must not be empty."); | ||
|
||
return () -> Optional.of(databaseName); | ||
} | ||
|
||
/** | ||
* A database name provider always returning the empty optional. | ||
* | ||
* @return A provider for the default database name. | ||
*/ | ||
static Neo4jDatabaseNameProvider getDefaultDatabaseNameProvider() { | ||
|
||
return DefaultNeo4jDatabaseNameProvider.INSTANCE; | ||
} | ||
} | ||
|
||
enum DefaultNeo4jDatabaseNameProvider implements Neo4jDatabaseNameProvider { | ||
INSTANCE; | ||
|
||
@Override | ||
public Optional<String> getCurrentDatabaseName() { | ||
return Optional.empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.