Skip to content

Commit 20b42b8

Browse files
author
Yasuharu NAKANO
committed
added description about flushing and transaction to documentation
1 parent f4f9623 commit 20b42b8

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/docs/guide/optimisticLocking.gdoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ sampleDomain.withOptimisticLocking(modificationBaseVersion) { Object domain ->
2020
}
2121
{code}
2222

23+
See also [Optimistic and Pessimistic locking|http://grails.org/doc/latest/guide/single.html#locking].
24+
2325

2426
h3. Type of modificationBaseVersion
2527

@@ -88,3 +90,19 @@ In case of conflict, a return value of @onConflict@'s closure is returned.
8890
{code}
8991
assert result.returnValue == "NG"
9092
{code}
93+
94+
95+
h3. Flushing session
96+
97+
In case of using persistence methods (e.g. @save@ method), you must be flush a session.
98+
99+
{code}
100+
def result = sampleDomain.withOptimisticLocking(modificationBaseVersion) { Object domain ->
101+
domain.save(flush: true)
102+
}.onConflict { Object domain, Throwable caused ->
103+
return "NG" // In case of error, you can catch and handle it here.
104+
}
105+
{code}
106+
107+
Otherwise, a conflict error occurs on flushing at outside of controller action and you cannot handle the error.
108+
Instead, you will see the 500 error page at development mode.

src/docs/guide/pessimisticLocking.gdoc

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ SampleDomain.withPessimisticLock(id) { Object lockedDomain ->
99

1010
// operation to require a pessimistic lock
1111

12-
}.onNotFound { ->
12+
}.onNotFound {domainId ->
1313

1414
// operation when the target is not found
1515
}
1616
{code}
1717

18+
See also [Optimistic and Pessimistic locking|http://grails.org/doc/latest/guide/single.html#locking] and [lock method|http://grails.org/doc/latest/ref/Domain%20Classes/lock.html] of domain class.
19+
1820

1921
h3. onNotFound is optional
2022

@@ -31,15 +33,23 @@ In this case, when target isn't found, all the plugin has to do is nothing.
3133

3234
h3. Closure arguments
3335

34-
The @lockedDomain@ argument of @onNotFound@'s closure is the domain instance which is found by @SampleDomain.lock(id)@ method.
36+
The @lockedDomain@ argument of @withPessimisticLock@'s closure is the domain instance which is found by @SampleDomain.lock(id)@ method.
3537

3638
{code}
3739
SampleDomain.withPessimisticLock(id) { Object lockedDomain ->
3840
assert lockedDomain.id == id
3941
}
4042
{code}
4143

42-
There are no argument for @onNotFound@'s closure.
44+
The domainId argument of @onNotFound@'s closure is the domain id which is specified to @withPessimisticLock@'s first argument.
45+
46+
{code}
47+
SampleDomain.withPessimisticLock(id) { Object lockedDomain ->
48+
// ...
49+
}.onNotFound {domainId ->
50+
assert domainId == id
51+
}
52+
{code}
4353

4454

4555
h3. Return value
@@ -59,3 +69,10 @@ In case that no row is found, a return value of @onNotFound@'s closure is return
5969
{code}
6070
assert result.returnValue == "NG"
6171
{code}
72+
73+
74+
h3. Transaction is required
75+
76+
When you want to use a pessimistic lock, you must need a transaction.
77+
The aquired lock is automatically released when the transaction commits.
78+

0 commit comments

Comments
 (0)