Skip to content

Commit

Permalink
remove the rule that cacheable methods must not be transactional - th…
Browse files Browse the repository at this point in the history
…is can better be solved be ensuring the order of @EnableCaching and @EnableTransactionManagement (#14)
  • Loading branch information
klu2 authored Sep 7, 2022
1 parent b1bf35b commit bcbb5fc
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 27 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
kotlin.code.style=official

version=0.1.1
version=0.2.0
11 changes: 0 additions & 11 deletions rules/spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,6 @@ Whenever you access the database via Spring Data repositories, ensure you are wi
from a method that is annotated with `@Transactional`) to ensure ACID criteria when reading data from the DB. In case
you are only reading from the database (and not writing), consider to use `@Transactional(readOnly=true)`

<a id="tx-transactional-methods-should-not-be-cacheable"></a>
### Functions should not be @Transactional and @Cacheable
<sup>`Rule-ID: spring.tx-transactional-methods-should-not-be-cacheable`</sup>

The `@Cacheable` Annotation enables a caching optimization for the corresponding function call.
When applied the function may return a cached result from an earlier call, instead of actually evaluating the function.
Hence, a `@Cacheable` function should not have side effects!

The `@Transactional` annotation opens database transactions.
Transactions bundle several write operations so that they apply atomically. In case of an error, they will be rolled back collectively.

<a id="tx-do-not-throw-exceptions"></a>
### @Transactional annotated methods must not throw checked exceptions
<sup>`Rule-ID: spring.tx-do-not-throw-exceptions`</sup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,6 @@ class SpringTransactionalRules {
)


@ArchTest
val methods_that_are_transactional_should_not_be_cacheable =
archRuleWithId(
"spring.tx-transactional-methods-should-not-be-cacheable",
methods()
.that(AreTransactional())
.should(NotBeCacheable())
)

private class NotBeCacheable :
ArchCondition<JavaMethod>("not be annotated with @Cacheable") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ class CloudflightSpringTransactionalRulesTest {
assertThat(e.message!!.split("\n").size).isEqualTo(3)
}


rules.methods_that_are_transactional_should_not_be_cacheable.check(importerGoodCases)
rulesRepository.repository_methods_should_only_be_accessed_by_transactional_methods.check(importerGoodCases)
}

Expand Down Expand Up @@ -79,11 +77,6 @@ class CloudflightSpringTransactionalRulesTest {
.check(importerBadCases)
}

assertThrows<AssertionError> {
rules.methods_that_are_transactional_should_not_be_cacheable
.check(importerBadCases)
}

assertThrows<AssertionError> {
rulesRepository.repository_methods_should_only_be_accessed_by_transactional_methods
.check(importerBadCases)
Expand Down

0 comments on commit bcbb5fc

Please sign in to comment.