cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Specify read/write transaction before query execution in spring data neo4j

Hi,
I am using org.springframework.boot:spring-boot-starter-data-neo4j to connect with graph db.

I am looking for a way to specify read/write for a transaction so that write queries are routes to leader instance and reads are for other instances. This helps to reduce load on leader instance which in turn improve database performance.

How can we specify in code?

Here is my transaction manager with driver instantiation

@Configuration
    @EnableTransactionManagement
    class Neo4jConfig(
        @Value("\${spring.neo4j.uri}")
        val neo4jDBUri: String,

        @Value("\${spring.neo4j.authentication.username}")
        val neo4jDbUserName: String,

        @Value("\${spring.neo4j.authentication.password}")
        val neo4jDbUserPassword: String
    ) : AbstractNeo4jConfig() {


        @Bean
        override fun driver(): Driver {
            val config: Config = Config.builder()
                .withMaxTransactionRetryTime(60, TimeUnit.SECONDS)
                .withLeakedSessionsLogging()
                .withRoutingTablePurgeDelay(1, TimeUnit.SECONDS)
                .withConnectionAcquisitionTimeout(2 * 60, TimeUnit.SECONDS)
                .withConnectionLivenessCheckTimeout(2 * 60, TimeUnit.SECONDS)
                .withDriverMetrics()
                .withMaxConnectionLifetime(5, TimeUnit.MINUTES)
                .withMaxConnectionPoolSize(10)
                .build()

            return GraphDatabase
                .driver(neo4jDBUri, AuthTokens.basic(neo4jDbUserName, neo4jDbUserPassword), config)
        }
    }

    // Refer: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.4.0-M2-Release-Notes#neo4j-1
    @Bean(ReactiveNeo4jRepositoryConfigurationExtension.DEFAULT_TRANSACTION_MANAGER_BEAN_NAME)
    fun reactiveTransactionManager(
        driver: Driver,
        databaseNameProvider: ReactiveDatabaseSelectionProvider
    😞 ReactiveTransactionManager = ReactiveNeo4jTransactionManager(driver, databaseNameProvider)

    // Refer: https://hantsy.medium.com/data-auditing-with-spring-data-neo4j-11d6461146ff
    @Configuration(proxyBeanMethods = false)
    @EnableReactiveNeo4jAuditing
    internal class DataConfig {
        @Bean
        fun reactiveAuditorAware(): ReactiveAuditorAware<String> = ReactiveAuditorAware { Mono.just("Audit enabled") }
    }

query part is like this:

class CloudFilesDao(val client: ReactiveNeo4jClient) {
suspend fun addEvent(){
val query = "merge ****"
client
.query(query)
.run()
}
}
0 REPLIES 0