-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add setLockMode, forUpdate, and forShare query methods #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c0aa886
feat: Add setLockMode, forUpdate, and forShare query methods
QuinnB73 b1d298b
assert contents of hibernate criteria object, as this layer tests how…
QuinnB73 5f81156
clean up kotlin code in docs
QuinnB73 53b0620
update documentation
QuinnB73 4a21f70
detekt
QuinnB73 1a96f4b
add helpers to simplify tests and re-add newlines
QuinnB73 bed5428
detekt
QuinnB73 d586a3b
Update docs/basic_queries.md
QuinnB73 d075651
Merge branch 'main' into quinn.add-lock-mode-support
QuinnB73 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
42 changes: 42 additions & 0 deletions
42
yawn-api/src/main/kotlin/com/faire/yawn/query/YawnLockMode.kt
This file contains hidden or 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,42 @@ | ||
| package com.faire.yawn.query | ||
|
|
||
| /** | ||
| * Lock modes for Yawn queries, abstracting over SQL's locking mechanisms. | ||
| * | ||
| * These lock modes control how the database handles concurrent access to selected rows. | ||
| * When a lock mode is set, the generated SQL will include the appropriate locking clause | ||
| * (e.g., `FOR UPDATE` or `FOR SHARE`). | ||
| * | ||
| * @see org.hibernate.LockMode | ||
| */ | ||
| enum class YawnLockMode { | ||
| /** | ||
| * No lock. This is the default behavior. | ||
| */ | ||
| NONE, | ||
|
|
||
| /** | ||
| * A shared lock (SQL: `FOR SHARE`). | ||
| * | ||
| * Use for "find or create" patterns where you need to prevent concurrent creates | ||
| * but allow concurrent reads. This lock allows other transactions to read the rows | ||
| * but prevents them from acquiring an exclusive lock until this transaction completes. | ||
| * | ||
| * Example use case: Multiple execution paths (e.g., event consumption, multiple jobs) | ||
| * are calling a "find or create" path. Using this lock ensures the entity is only | ||
| * created once by making concurrent transactions wait. | ||
| */ | ||
| PESSIMISTIC_READ, | ||
|
|
||
| /** | ||
| * An exclusive lock (SQL: `FOR UPDATE`). | ||
| * | ||
| * Use when you intend to update the selected rows and want to prevent | ||
| * concurrent modifications. This is the strongest lock mode and will block | ||
| * other transactions from reading (with locks) or writing to the locked rows. | ||
| * | ||
| * Example use case: Event consumers that replicate data use this lock to prevent | ||
| * concurrent modifications during batch updates. | ||
| */ | ||
| PESSIMISTIC_WRITE, | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
111 changes: 111 additions & 0 deletions
111
yawn-database-test/src/test/kotlin/com/faire/yawn/database/YawnLockModeTest.kt
QuinnB73 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,111 @@ | ||
| package com.faire.yawn.database | ||
|
|
||
| import com.faire.yawn.YawnTableDef | ||
| import com.faire.yawn.criteria.builder.TypeSafeCriteriaBuilder | ||
| import com.faire.yawn.query.YawnLockMode | ||
| import com.faire.yawn.setup.entities.BaseEntity | ||
| import com.faire.yawn.setup.entities.BookTable | ||
| import com.faire.yawn.setup.hibernate.YawnTestCompiledQuery | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.hibernate.LockMode | ||
| import org.hibernate.internal.CriteriaImpl | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| internal class YawnLockModeTest : BaseYawnDatabaseTest() { | ||
| @Test | ||
| fun `forUpdate sets PESSIMISTIC_WRITE lock mode`() { | ||
| transactor.open { session -> | ||
| val compiledQuery = getCompiledQuery { | ||
| session.query(BookTable) { books -> | ||
| addEq(books.name, "The Hobbit") | ||
| }.forUpdate() | ||
| } | ||
|
|
||
| assertThat(compiledQuery.hibernateCriteriaImpl.lockModes.values).containsOnly(LockMode.PESSIMISTIC_WRITE) | ||
| assertThat(compiledQuery.list().single().name).isEqualTo("The Hobbit") | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `forShare sets PESSIMISTIC_READ lock mode`() { | ||
| transactor.open { session -> | ||
| val compiledQuery = getCompiledQuery { | ||
| session.query(BookTable) { books -> | ||
| addEq(books.name, "The Hobbit") | ||
| }.forShare() | ||
| } | ||
|
|
||
| assertThat(compiledQuery.hibernateCriteriaImpl.lockModes.values).containsOnly(LockMode.PESSIMISTIC_READ) | ||
| assertThat(compiledQuery.list().single().name).isEqualTo("The Hobbit") | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `setLockMode with PESSIMISTIC_WRITE`() { | ||
| transactor.open { session -> | ||
| val compiledQuery = getCompiledQuery { | ||
| session.query(BookTable) { books -> | ||
| addEq(books.name, "The Hobbit") | ||
| }.setLockMode(YawnLockMode.PESSIMISTIC_WRITE) | ||
| } | ||
|
|
||
| assertThat(compiledQuery.hibernateCriteriaImpl.lockModes.values).containsOnly(LockMode.PESSIMISTIC_WRITE) | ||
| assertThat(compiledQuery.list().single().name).isEqualTo("The Hobbit") | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `setLockMode with PESSIMISTIC_READ`() { | ||
| transactor.open { session -> | ||
| val compiledQuery = getCompiledQuery { | ||
| session.query(BookTable) { books -> | ||
| addEq(books.name, "The Hobbit") | ||
| }.setLockMode(YawnLockMode.PESSIMISTIC_READ) | ||
| } | ||
|
|
||
| assertThat(compiledQuery.hibernateCriteriaImpl.lockModes.values).containsOnly(LockMode.PESSIMISTIC_READ) | ||
| assertThat(compiledQuery.list().single().name).isEqualTo("The Hobbit") | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `setLockMode with NONE explicitly set`() { | ||
| transactor.open { session -> | ||
| val compiledQuery = getCompiledQuery { | ||
| session.query(BookTable) { books -> | ||
| addEq(books.name, "The Hobbit") | ||
| }.setLockMode(YawnLockMode.NONE) | ||
| } | ||
|
|
||
| assertThat(compiledQuery.hibernateCriteriaImpl.lockModes.values).containsOnly(LockMode.NONE) | ||
| assertThat(compiledQuery.list().single().name).isEqualTo("The Hobbit") | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `forUpdate can be combined with maxResults and offset`() { | ||
| transactor.open { session -> | ||
| val compiledQuery = getCompiledQuery { | ||
| session.query(BookTable) { books -> | ||
| orderAsc(books.name) | ||
| } | ||
| .forUpdate() | ||
| .maxResults(2) | ||
| .offset(1) | ||
| } | ||
|
|
||
| assertThat(compiledQuery.hibernateCriteriaImpl.lockModes.values).containsOnly(LockMode.PESSIMISTIC_WRITE) | ||
| assertThat(compiledQuery.list().map { it.name }) | ||
| .containsExactly("Lord of the Rings", "The Emperor's New Clothes") | ||
| } | ||
| } | ||
|
|
||
| private fun <T : BaseEntity<T>, DEF : YawnTableDef<T, T>> getCompiledQuery( | ||
| queryProvider: () -> TypeSafeCriteriaBuilder<T, DEF>, | ||
| ): YawnTestCompiledQuery<T> { | ||
| return queryProvider().compile() as YawnTestCompiledQuery<T> | ||
| } | ||
| } | ||
|
|
||
| private val YawnTestCompiledQuery<*>.hibernateCriteriaImpl: CriteriaImpl | ||
| get() = rawQuery as CriteriaImpl |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.