97 lines
2.8 KiB
Plaintext
97 lines
2.8 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Marking a non-`public` method `@Transactional` is both useless and misleading because Spring does not recognize non-`public` methods, and so makes no provision for their proper invocation.
|
|
Nor does Spring make provision for the methods invoked by the method it called.
|
|
|
|
|
|
Therefore marking a `private` method, for instance, `@Transactional` can only result in a runtime error or exception if the method is annotated as `@Transactional`.
|
|
|
|
== How to fix it
|
|
Make the method public or remove the `@Transactional` annotation.
|
|
|
|
=== Code examples
|
|
==== Noncompliant code example
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
@Transactional // Noncompliant
|
|
void doTheThing(ArgClass arg) {
|
|
// ...
|
|
}
|
|
|
|
@Transactional // Noncompliant
|
|
private void doTheOtherThing(ArgClass arg) {
|
|
// ...
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
@Transactional
|
|
public void doTheThing(ArgClass arg) {
|
|
// ...
|
|
}
|
|
|
|
private void doTheOtherThing(ArgClass arg) {
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Make this method "public" or remove the "@Transactional" annotation
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 21 Nov 2014, 12:28:29 Freddy Mallet wrote:
|
|
Two questions/remarks:
|
|
|
|
* Are we talking about private methods or about non-public methods ? If my feeling is correct this rule should only target private methods
|
|
* I would tag the rule with the label "spring"
|
|
* As this rule is associated to the Reliability characteristic, I think the default severity should be "Critical"
|
|
|
|
=== on 21 Nov 2014, 13:28:08 Ann Campbell wrote:
|
|
The Spring docs are pretty clear that only `public` method can actually be `@Transactional`
|
|
|
|
=== on 21 Nov 2014, 14:14:44 Freddy Mallet wrote:
|
|
Ok Ann, so I would replace :
|
|
|
|
|
|
"Therefore marking a private method"
|
|
|
|
|
|
by
|
|
|
|
|
|
"Therefore marking for instance a private method"
|
|
|
|
|
|
to prevent any misunderstanding
|
|
|
|
=== on 27 Nov 2018, 13:06:43 Semyon Danilov wrote:
|
|
\[~ann.campbell.2] Actually, any method can be Transactional if you're using AspectJ compiler, it's stated in the docs https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/transaction.html[here]. The excerpt:
|
|
|
|
|
|
----
|
|
Method visibility and @Transactional
|
|
|
|
When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.
|
|
----
|
|
|
|
=== on 27 Nov 2018, 13:33:30 Ann Campbell wrote:
|
|
FYI [~alexandre.gigleux] ^
|
|
|
|
endif::env-github,rspecator-view[]
|