From bcbb5fcbfe1b6bbae0046fe3df178f0075ab493e Mon Sep 17 00:00:00 2001 From: Klaus Lehner <172195+klu2@users.noreply.github.com> Date: Wed, 7 Sep 2022 07:56:51 +0200 Subject: [PATCH] remove the rule that cacheable methods must not be transactional - this can better be solved be ensuring the order of @EnableCaching and @EnableTransactionManagement (#14) --- gradle.properties | 2 +- rules/spring.md | 11 ----------- .../archunit/rules/spring/SpringTransactionalRules.kt | 8 -------- .../spring/CloudflightSpringTransactionalRulesTest.kt | 7 ------- 4 files changed, 1 insertion(+), 27 deletions(-) diff --git a/gradle.properties b/gradle.properties index e5ffe84..80ca4a2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ kotlin.code.style=official -version=0.1.1 +version=0.2.0 diff --git a/rules/spring.md b/rules/spring.md index 14c558c..bab7afd 100644 --- a/rules/spring.md +++ b/rules/spring.md @@ -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)` - -### Functions should not be @Transactional and @Cacheable -`Rule-ID: spring.tx-transactional-methods-should-not-be-cacheable` - -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. - ### @Transactional annotated methods must not throw checked exceptions `Rule-ID: spring.tx-do-not-throw-exceptions` diff --git a/src/main/kotlin/io/cloudflight/cleancode/archunit/rules/spring/SpringTransactionalRules.kt b/src/main/kotlin/io/cloudflight/cleancode/archunit/rules/spring/SpringTransactionalRules.kt index a85925e..22bd121 100644 --- a/src/main/kotlin/io/cloudflight/cleancode/archunit/rules/spring/SpringTransactionalRules.kt +++ b/src/main/kotlin/io/cloudflight/cleancode/archunit/rules/spring/SpringTransactionalRules.kt @@ -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("not be annotated with @Cacheable") { diff --git a/src/test/kotlin/io/cloudflight/cleancode/archunit/rules/spring/CloudflightSpringTransactionalRulesTest.kt b/src/test/kotlin/io/cloudflight/cleancode/archunit/rules/spring/CloudflightSpringTransactionalRulesTest.kt index 606621f..c47685c 100644 --- a/src/test/kotlin/io/cloudflight/cleancode/archunit/rules/spring/CloudflightSpringTransactionalRulesTest.kt +++ b/src/test/kotlin/io/cloudflight/cleancode/archunit/rules/spring/CloudflightSpringTransactionalRulesTest.kt @@ -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) } @@ -79,11 +77,6 @@ class CloudflightSpringTransactionalRulesTest { .check(importerBadCases) } - assertThrows { - rules.methods_that_are_transactional_should_not_be_cacheable - .check(importerBadCases) - } - assertThrows { rulesRepository.repository_methods_should_only_be_accessed_by_transactional_methods .check(importerBadCases)