Chris Smith
Chris Smith

Reputation: 3012

Is annotating a class with @Transactional the same as annotating all its methods as @Transactional?

If I annotate a class with @Transactional like so:

@Transactional
class MyService { ... }

Is that the same as annotating all of its methods with @Transactional like so:

class MyService {
    @Transactional
    void myFunction() { ... }
}

There are also some other things to consider such as: how does this propagate to sub classes, inner classes, and static methods?

Upvotes: 1

Views: 63

Answers (1)

Sascha Frinken
Sascha Frinken

Reputation: 3366

From the documentation

…The result is that all methods are wrapped in a transaction and automatic rollback occurs if a method throws an exception (both Checked or Runtime exceptions) or an Error…

So yes it is the same.

how does this propagate to sub classes

It is inherited - but it is recommended to annotate only concrete classes

inner classes

AFAIK not.
If any I would only define POJOs as an inner class - business logic always goes into services

static methods

AFAIK not

Upvotes: 1

Related Questions