-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multy-repository transactions support
- Loading branch information
Showing
10 changed files
with
383 additions
and
0 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
16 changes: 16 additions & 0 deletions
16
api/src/main/kotlin/org/taymyr/play/repository/domain/Transaction.kt
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,16 @@ | ||
package org.taymyr.play.repository.domain | ||
|
||
import akka.Done | ||
import java.util.concurrent.CompletionStage | ||
|
||
/** | ||
* DDD repository transaction | ||
*/ | ||
interface Transaction { | ||
|
||
/** | ||
* Commits transaction. | ||
*/ | ||
fun commit(): CompletionStage<Done> | ||
|
||
} |
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
120 changes: 120 additions & 0 deletions
120
jpa/src/main/kotlin/org/taymyr/play/repository/infrastructure/persistence/JPATransaction.kt
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,120 @@ | ||
package org.taymyr.play.repository.infrastructure.persistence | ||
|
||
import akka.Done | ||
import org.taymyr.play.repository.domain.Transaction | ||
import play.db.jpa.JPAApi | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.CompletionStage | ||
import java.util.function.Supplier | ||
import javax.inject.Inject | ||
import javax.persistence.EntityManager | ||
|
||
class JPATransaction( | ||
protected val jpaApi: JPAApi, | ||
protected val executionContext: DatabaseExecutionContext, | ||
protected val persistenceUnitName: String = "default" | ||
): Transaction { | ||
|
||
private val oparetionsLog: MutableList<Operation<*>> = mutableListOf() | ||
|
||
/** | ||
* Saves to transaction log remove operation for repository and aggregate. | ||
* | ||
* @param repository JPA repository | ||
* @param aggregate removed aggregate | ||
*/ | ||
fun <AGGREGATE : Any> remove(repository: JPARepository<AGGREGATE, *>, aggregate: AGGREGATE): CompletionStage<Done> { | ||
oparetionsLog.add(Remove(repository, listOf(aggregate))) | ||
return CompletableFuture.completedFuture(Done.getInstance()) | ||
} | ||
|
||
/** | ||
* Saves to transaction log remove operation for repository and aggregates. | ||
* | ||
* @param repository JPA repository | ||
* @param aggregate removed aggregates | ||
*/ | ||
fun <AGGREGATE : Any> remove(repository: JPARepository<AGGREGATE, *>, aggregates: Collection<AGGREGATE>): CompletionStage<Done> { | ||
oparetionsLog.add(Remove(repository, aggregates)) | ||
return CompletableFuture.completedFuture(Done.getInstance()) | ||
} | ||
|
||
/** | ||
* Saves to transaction log create operation for repository and aggregate. | ||
* | ||
* @param repository JPA repository | ||
* @param aggregate created aggregate | ||
*/ | ||
fun <AGGREGATE : Any> create(repository: JPARepository<AGGREGATE, *>, aggregate: AGGREGATE): CompletionStage<Done> { | ||
oparetionsLog.add(Create(repository, listOf(aggregate))) | ||
return CompletableFuture.completedFuture(Done.getInstance()) | ||
} | ||
|
||
/** | ||
* Saves to transaction log create operation for repository and aggregates. | ||
* | ||
* @param repository JPA repository | ||
* @param aggregate created aggregates | ||
*/ | ||
fun <AGGREGATE : Any> create(repository: JPARepository<AGGREGATE, *>, aggregates: Collection<AGGREGATE>): CompletionStage<Done> { | ||
oparetionsLog.add(Create(repository, aggregates)) | ||
return CompletableFuture.completedFuture(Done.getInstance()) | ||
} | ||
|
||
/** | ||
* Saves to transaction log save operation for repository and aggregate. | ||
* | ||
* @param repository JPA repository | ||
* @param aggregate saved aggregate | ||
*/ | ||
fun <AGGREGATE : Any> save(repository: JPARepository<AGGREGATE, *>, aggregate: AGGREGATE): CompletionStage<Done> { | ||
oparetionsLog.add(Save(repository, listOf(aggregate))) | ||
return CompletableFuture.completedFuture(Done.getInstance()) | ||
} | ||
|
||
/** | ||
* Saves to transaction log save operation for repository and aggregates. | ||
* | ||
* @param repository JPA repository | ||
* @param aggregate saved aggregates | ||
*/ | ||
fun <AGGREGATE : Any> save(repository: JPARepository<AGGREGATE, *>, aggregates: Collection<AGGREGATE>): CompletionStage<Done> { | ||
oparetionsLog.add(Save(repository, aggregates)) | ||
return CompletableFuture.completedFuture(Done.getInstance()) | ||
} | ||
|
||
override fun commit(): CompletionStage<Done> = execute { em -> | ||
oparetionsLog.forEach { it.process(em) } | ||
Done.getInstance() | ||
} | ||
|
||
protected fun <E> transaction(function: (EntityManager) -> E): E = jpaApi.withTransaction(persistenceUnitName, function) | ||
|
||
protected fun <E> execute(function: (EntityManager) -> E): CompletionStage<E> = | ||
CompletableFuture.supplyAsync(Supplier { transaction( function) }, executionContext) | ||
|
||
private abstract class Operation<AGGREGATE : Any>(open val repository: JPARepository<AGGREGATE, *>, open val aggregates: Collection<AGGREGATE>) { | ||
abstract fun process(em: EntityManager) | ||
} | ||
|
||
private data class Remove<AGGREGATE : Any>(override val repository: JPARepository<AGGREGATE, *>, override val aggregates: Collection<AGGREGATE>) : Operation<AGGREGATE>(repository, aggregates) { | ||
override fun process(em: EntityManager) { | ||
aggregates.forEach { | ||
if (em.contains(it)) em.remove(it) | ||
else em.remove(em.merge(it)) | ||
} | ||
} | ||
} | ||
|
||
private data class Create<AGGREGATE : Any>(override val repository: JPARepository<AGGREGATE, *>, override val aggregates: Collection<AGGREGATE>) : Operation<AGGREGATE>(repository, aggregates) { | ||
override fun process(em: EntityManager) { | ||
aggregates.forEach { em.persist(it) } | ||
} | ||
} | ||
|
||
private data class Save<AGGREGATE : Any>(override val repository: JPARepository<AGGREGATE, *>, override val aggregates: Collection<AGGREGATE>) : Operation<AGGREGATE>(repository, aggregates) { | ||
override fun process(em: EntityManager) { | ||
aggregates.forEach { em.merge(it) } | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
jpa/src/test/kotlin/org/taymyr/play/repository/domain/Address.kt
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,9 @@ | ||
package org.taymyr.play.repository.domain | ||
|
||
interface Address { | ||
val id: String | ||
val zip: String? | ||
val city: String | ||
val street: String | ||
val user: User | ||
} |
3 changes: 3 additions & 0 deletions
3
jpa/src/test/kotlin/org/taymyr/play/repository/domain/AddressRepository.kt
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,3 @@ | ||
package org.taymyr.play.repository.domain | ||
|
||
interface AddressRepository : Repository<Address, String> |
26 changes: 26 additions & 0 deletions
26
jpa/src/test/kotlin/org/taymyr/play/repository/infrastructure/persistence/AddressImpl.kt
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,26 @@ | ||
package org.taymyr.play.repository.infrastructure.persistence | ||
|
||
import org.taymyr.play.repository.domain.Address | ||
import javax.persistence.Entity | ||
import javax.persistence.FetchType | ||
import javax.persistence.Id | ||
import javax.persistence.JoinColumn | ||
import javax.persistence.ManyToOne | ||
import javax.persistence.Table | ||
|
||
@Entity | ||
@Table(name = "ADDRESS") | ||
data class AddressImpl( | ||
|
||
@Id override val id: String, | ||
|
||
override val zip: String?, | ||
|
||
override val city: String, | ||
|
||
override val street: String, | ||
|
||
@ManyToOne(fetch = FetchType.EAGER) | ||
@JoinColumn(name = "user_id") | ||
override val user: UserImpl | ||
) : Address |
15 changes: 15 additions & 0 deletions
15
...est/kotlin/org/taymyr/play/repository/infrastructure/persistence/AddressRepositoryImpl.kt
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,15 @@ | ||
package org.taymyr.play.repository.infrastructure.persistence | ||
|
||
import org.taymyr.play.repository.domain.Address | ||
import org.taymyr.play.repository.domain.AddressRepository | ||
import play.db.jpa.JPAApi | ||
import java.util.UUID | ||
import javax.inject.Inject | ||
|
||
class AddressRepositoryImpl @Inject constructor( | ||
jpaApi: JPAApi, | ||
executionContext: DatabaseExecutionContext | ||
) : JPARepository<Address, String>(jpaApi, executionContext, AddressImpl::class.java), AddressRepository { | ||
|
||
override fun nextIdentity(): String = UUID.randomUUID().toString() | ||
} |
Oops, something went wrong.