Skip to content

Commit

Permalink
Separate transaction managers for Spring Data Neo4j and JPA
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelprinz committed Feb 11, 2025
1 parent 972767b commit 7c513f2
Show file tree
Hide file tree
Showing 99 changed files with 569 additions and 241 deletions.
8 changes: 8 additions & 0 deletions common/spring-data/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins {
id("org.orkg.gradle.kotlin-library")
}

dependencies {
api("org.springframework:spring-core")
api("org.springframework:spring-tx")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.orkg.spring.data.annotations

import org.springframework.core.annotation.AliasFor
import org.springframework.transaction.annotation.Isolation
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional

@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@Transactional(transactionManager = "jpaTransactionManager")
annotation class TransactionalOnJPA(
@get:AliasFor(annotation = Transactional::class, attribute = "readOnly") val readOnly: Boolean = false,
@get:AliasFor(annotation = Transactional::class, attribute = "propagation") val propagation: Propagation = Propagation.REQUIRED,
@get:AliasFor(annotation = Transactional::class, attribute = "isolation") val isolation: Isolation = Isolation.DEFAULT,
)

@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@Transactional(transactionManager = "neo4jTransactionManager")
annotation class TransactionalOnNeo4j(
@get:AliasFor(annotation = Transactional::class, attribute = "readOnly") val readOnly: Boolean = false,
@get:AliasFor(annotation = Transactional::class, attribute = "propagation") val propagation: Propagation = Propagation.REQUIRED,
@get:AliasFor(annotation = Transactional::class, attribute = "isolation") val isolation: Isolation = Isolation.DEFAULT,
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies {
api("org.springframework.data:spring-data-commons")
api("org.springframework.data:spring-data-jpa")
api("org.springframework:spring-context")
api("org.springframework:spring-tx")
api(project(":common:spring-data"))
implementation(project(":common:string-utils"))
api(project(":common:identifiers"))
api(project(":community:community-core-model"))
Expand All @@ -35,6 +35,8 @@ testing {
runtimeOnly(project(":migrations:liquibase"))
runtimeOnly("org.liquibase:liquibase-core")
runtimeOnly("org.postgresql:postgresql")
implementation("org.springframework.boot:spring-boot-test")
implementation("org.springframework:spring-orm")
implementation("org.springframework.boot:spring-boot-test-autoconfigure")
implementation("org.springframework:spring-test")
implementation(testFixtures(project(":common:testing")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import org.orkg.eventbus.EventBus
import org.orkg.eventbus.Listener
import org.orkg.eventbus.events.DisplayNameUpdated
import org.orkg.eventbus.events.UserRegistered
import org.orkg.spring.data.annotations.TransactionalOnJPA
import org.springframework.beans.factory.InitializingBean
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Component

@Component
@TransactionalOnJPA
class ContributorFromUserAdapter(
private val eventBus: EventBus,
private val postgresContributorRepository: PostgresContributorRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import org.orkg.community.adapter.output.jpa.internal.PostgresOrganizationReposi
import org.orkg.community.domain.ConferenceSeries
import org.orkg.community.domain.ConferenceSeriesId
import org.orkg.community.output.ConferenceSeriesRepository
import org.orkg.spring.data.annotations.TransactionalOnJPA
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Component

@Component
@TransactionalOnJPA
class SpringJpaPostgresConferenceSeriesAdapter(
private val postgresConferenceSeriesRepository: PostgresConferenceSeriesRepository,
private val postgresOrganizationRepository: PostgresOrganizationRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import org.orkg.community.adapter.output.jpa.internal.KeycloakEventStateEntity
import org.orkg.community.adapter.output.jpa.internal.PostgresKeycloakEventStateRepository
import org.orkg.community.domain.EventType
import org.orkg.community.output.KeycloakEventStateRepository
import org.orkg.spring.data.annotations.TransactionalOnJPA
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional

@Component
@Transactional
@TransactionalOnJPA
class SpringJpaPostgresKeycloakEventStateAdapter(
private val postgresRepository: PostgresKeycloakEventStateRepository,
) : KeycloakEventStateRepository {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import org.orkg.community.adapter.output.jpa.internal.toObservatory
import org.orkg.community.domain.Contributor
import org.orkg.community.domain.Observatory
import org.orkg.community.output.ObservatoryRepository
import org.orkg.spring.data.annotations.TransactionalOnJPA
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Component

@Component
@TransactionalOnJPA
class SpringJpaPostgresObservatoryAdapter(
private val postgresRepository: PostgresObservatoryRepository,
private val postgresOrganizationRepository: PostgresOrganizationRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import org.orkg.community.output.ObservatoryFilterRepository
import java.util.*
import org.orkg.common.ObservatoryId
import org.orkg.community.domain.ObservatoryFilterId
import org.orkg.spring.data.annotations.TransactionalOnJPA
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Component

@Component
@TransactionalOnJPA
class SpringJpaPostgresObservatoryFilterAdapter(
private val postgresRepository: PostgresObservatoryFilterRepository
) : ObservatoryFilterRepository {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import org.orkg.community.domain.Contributor
import org.orkg.community.domain.Organization
import org.orkg.community.domain.OrganizationType
import org.orkg.community.output.OrganizationRepository
import org.orkg.spring.data.annotations.TransactionalOnJPA
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Component

@Component
@TransactionalOnJPA
class SpringJpaPostgresOrganizationAdapter(
private val postgresOrganizationRepository: PostgresOrganizationRepository,
private val postgresObservatoryRepository: PostgresObservatoryRepository,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.orkg.community.adapter.output.jpa.configuration

import org.springframework.boot.autoconfigure.domain.EntityScan
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.data.jpa.repository.config.EnableJpaRepositories

@Configuration
@EnableJpaRepositories("org.orkg.community.adapter.output.jpa.internal")
@EnableJpaRepositories("org.orkg.community.adapter.output.jpa.internal", transactionManagerRef = "jpaTransactionManager")
@EntityScan("org.orkg.community.adapter.output.jpa.internal")
@ComponentScan(basePackages = ["org.orkg.community.adapter.output.jpa"])
class CommunityJpaConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.orkg.common.testing.fixtures.fixedClock
import org.orkg.community.adapter.output.jpa.configuration.CommunityJpaConfiguration
import org.orkg.community.adapter.output.jpa.configuration.CommunityJpaTestConfiguration
import org.orkg.eventbus.ReallySimpleEventBus
import org.orkg.eventbus.events.UserRegistered
import org.orkg.testing.MockUserId
Expand All @@ -19,10 +20,11 @@ import org.springframework.test.context.TestConstructor
@ContextConfiguration(
classes = [
CommunityJpaConfiguration::class,
CommunityJpaTestConfiguration::class,
ContributorFromUserAdapter::class,
ReallySimpleEventBus::class,
],
initializers = [PostgresContainerInitializer::class]
initializers = [PostgresContainerInitializer::class],
)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.orkg.community.adapter.output.jpa

import org.orkg.community.adapter.output.jpa.configuration.CommunityJpaConfiguration
import org.orkg.community.adapter.output.jpa.configuration.CommunityJpaTestConfiguration
import org.orkg.community.output.ObservatoryRepository
import org.orkg.community.output.OrganizationRepository
import org.orkg.community.testing.fixtures.ObservatoryRepositoryContracts
Expand All @@ -17,14 +18,14 @@ import org.springframework.test.context.TestConstructor
classes = [
SpringJpaPostgresObservatoryAdapter::class,
CommunityJpaConfiguration::class,
CommunityJpaTestConfiguration::class,
ReallySimpleEventBus::class,
],
initializers = [PostgresContainerInitializer::class]
)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
internal class PostgresObservatoryContractTest : ObservatoryRepositoryContracts {

@Autowired
private lateinit var adapter: SpringJpaPostgresObservatoryAdapter

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.orkg.community.adapter.output.jpa

import org.orkg.community.adapter.output.jpa.configuration.CommunityJpaConfiguration
import org.orkg.community.adapter.output.jpa.configuration.CommunityJpaTestConfiguration
import org.orkg.community.output.ObservatoryFilterRepository
import org.orkg.community.output.ObservatoryRepository
import org.orkg.community.output.OrganizationRepository
Expand All @@ -19,14 +20,14 @@ import org.springframework.test.context.TestConstructor
classes = [
SpringJpaPostgresObservatoryFilterAdapter::class,
CommunityJpaConfiguration::class,
CommunityJpaTestConfiguration::class,
ReallySimpleEventBus::class,
],
initializers = [PostgresContainerInitializer::class]
)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
internal class PostgresObservatoryFilterContractTest : ObservatoryFilterRepositoryContracts {

@Autowired
private lateinit var adapter: SpringJpaPostgresObservatoryFilterAdapter

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.orkg.community.adapter.output.jpa.configuration

import org.springframework.boot.test.context.TestConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.orm.jpa.JpaTransactionManager

@TestConfiguration
@ComponentScan("org.orkg.community.adapter.output.jpa")
class CommunityJpaTestConfiguration {
@Bean
fun jpaTransactionManager(): JpaTransactionManager = JpaTransactionManager()
}
2 changes: 1 addition & 1 deletion community/community-core-services/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
dependencies {
api("org.springframework.data:spring-data-commons")
api("org.springframework:spring-context")
api("org.springframework:spring-tx")
api(project(":common:spring-data"))
api(project(":common:identifiers"))
api(project(":community:community-core-model"))
api(project(":community:community-ports-input"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import org.orkg.common.OrganizationId
import org.orkg.community.input.ConferenceSeriesUseCases
import org.orkg.community.output.ConferenceSeriesRepository
import org.orkg.community.output.OrganizationRepository
import org.orkg.spring.data.annotations.TransactionalOnJPA
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional
@TransactionalOnJPA
class ConferenceSeriesService(
private val postgresConferenceSeriesRepository: ConferenceSeriesRepository,
private val postgresOrganizationRepository: OrganizationRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import org.orkg.common.ContributorId
import org.orkg.community.input.ContributorUseCases
import org.orkg.community.input.CreateContributorUseCase.CreateCommand
import org.orkg.community.output.ContributorRepository
import org.orkg.spring.data.annotations.TransactionalOnJPA
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional
@TransactionalOnJPA
class ContributorService(
private val repository: ContributorRepository,
) : ContributorUseCases {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package org.orkg.community.domain

import org.orkg.community.output.ObservatoryFilterRepository
import java.time.Clock
import java.time.LocalDateTime
import java.util.*
import org.orkg.common.ObservatoryId
import org.orkg.community.input.CreateObservatoryFilterUseCase
import org.orkg.community.input.ObservatoryFilterUseCases
import org.orkg.community.input.UpdateObservatoryFilterUseCase
import org.orkg.community.output.ObservatoryFilterRepository
import org.orkg.community.output.ObservatoryRepository
import org.orkg.graph.domain.ClassNotFound
import org.orkg.graph.domain.PredicateNotFound
import org.orkg.graph.output.ClassRepository
import org.orkg.graph.output.PredicateRepository
import org.orkg.spring.data.annotations.TransactionalOnJPA
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional
@TransactionalOnJPA
class ObservatoryFilterService(
private val repository: ObservatoryFilterRepository,
private val observatoryRepository: ObservatoryRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import org.orkg.graph.domain.Classes
import org.orkg.graph.domain.ResearchFieldNotFound
import org.orkg.graph.domain.Resources
import org.orkg.graph.output.ResourceRepository
import org.orkg.spring.data.annotations.TransactionalOnJPA
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional
@TransactionalOnJPA
class ObservatoryService(
private val postgresObservatoryRepository: ObservatoryRepository,
private val postgresOrganizationRepository: OrganizationRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import org.orkg.mediastorage.domain.Image
import org.orkg.mediastorage.domain.ImageId
import org.orkg.mediastorage.input.CreateImageUseCase
import org.orkg.mediastorage.input.ImageUseCases
import org.orkg.spring.data.annotations.TransactionalOnJPA
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional
@TransactionalOnJPA
class OrganizationService(
private val postgresOrganizationRepository: OrganizationRepository,
private val imageService: ImageUseCases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ testing {
runtimeOnly("org.springframework.boot:spring-boot-starter-test")
runtimeOnly("org.springframework.boot:spring-boot-starter-data-neo4j")
implementation("io.kotest:kotest-framework-api")
implementation("org.springframework.boot:spring-boot-test-autoconfigure")
implementation("org.springframework:spring-beans")
implementation("org.springframework:spring-test")
implementation("io.kotest:kotest-runner-junit5")
runtimeOnly("eu.michael-simons.neo4j:neo4j-migrations-spring-boot-starter")
implementation(project(":graph:graph-core-services"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import org.neo4j.cypherdsl.core.SymbolicName
import org.neo4j.driver.Record
import org.neo4j.driver.types.TypeSystem
import org.orkg.common.ContributorId
import org.orkg.common.ThingId
import org.orkg.contenttypes.domain.Certainty
import org.orkg.contenttypes.domain.RosettaStoneStatement
import org.orkg.contenttypes.domain.RosettaStoneStatementVersion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.orkg.contenttypes.adapter.output.neo4j.configuration

import org.springframework.boot.autoconfigure.domain.EntityScan
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories

@Configuration
@EnableNeo4jRepositories("org.orkg.contenttypes.adapter.output.neo4j.internal")
@EnableNeo4jRepositories("org.orkg.contenttypes.adapter.output.neo4j.internal", transactionManagerRef = "neo4jTransactionManager")
@EntityScan("org.orkg.contenttypes.adapter.output.neo4j.internal")
@ComponentScan(basePackages = ["org.orkg.contenttypes.adapter.output.neo4j.internal"])
class ContentTypesNeo4jConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,10 @@ import org.orkg.graph.output.LiteralRepository
import org.orkg.graph.output.PredicateRepository
import org.orkg.graph.output.ResourceRepository
import org.orkg.graph.output.StatementRepository
import org.orkg.testing.Neo4jContainerInitializer
import org.orkg.testing.annotations.Neo4jContainerUnitTest
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest
import org.springframework.cache.annotation.EnableCaching
import org.springframework.test.context.ContextConfiguration

@EnableAutoConfiguration
@DataNeo4jTest
@EnableCaching
@ContextConfiguration(
@Neo4jContainerUnitTest(
classes = [
SpringDataNeo4jContributionComparisonAdapter::class,
SpringDataNeo4jStatementAdapter::class,
Expand All @@ -34,11 +27,8 @@ import org.springframework.test.context.ContextConfiguration
SpringDataNeo4jResourceAdapter::class,
SpringDataNeo4jPredicateAdapter::class,
GraphNeo4jConfiguration::class,
ContentTypesNeo4jConfiguration::class
ContentTypesNeo4jConfiguration::class,
],
initializers = [
Neo4jContainerInitializer::class
]
)
internal class SpringDataNeo4jContributionComparisonAdapterContractTest(
@Autowired private val springDataNeo4jContributionComparisonAdapter: ContributionComparisonRepository,
Expand Down
Loading

0 comments on commit 7c513f2

Please sign in to comment.