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


Chris Smith

If I annotate a class @Transactionallike this :

@Transactional
class MyService { ... }

Is this the same as annotating all its methods @Transactional:

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

There are a few other things to consider, eg: how does this propagate to subclasses, inner classes and static methods?

Sasha Flinken

from the documentation

...the result is that all methods are wrapped in a transaction, and if a method throws an exception (checked or runtime exception) or an error, an automatic rollback occurs...

So yes, it's the same.

How does this propagate to subclasses

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

inner class

AFAIK not.
If anything, I would only define POJOs as inner classes - business logic always goes into services

static method

AFAIK no

Related


Avoid annotating all methods with @ResponseBody

Gabriel Sanmartin I am creating a REST api where I am returning domain objects as JSON entities. So far I'm creating a controller where for each method I have to annotate it like this: @RequestMapping(value="/entity/{id}", produces = "application/json; charset

Avoid annotating all methods with @ResponseBody

Gabriel Sanmartin I am creating a REST api where I am returning domain objects as JSON entities. So far I am creating a controller where I have to annotate each method like this: @RequestMapping(value="/entity/{id}", produces = "application/json; charset=utf-8

Avoid annotating all methods with @ResponseBody

Gabriel Sanmartin I am creating a REST api where I am returning domain objects as JSON entities. So far I'm creating a controller where for each method I have to annotate it like this: @RequestMapping(value="/entity/{id}", produces = "application/json; charset

Annotating unstable classes/methods for Javadoc

iceberg When developing new classes/methods for a Java project, sometimes you want people to try out your new code, but don't want to guarantee that it will be backward compatible in future releases. In this case, use something like the @Unstable annotation to

Annotating unstable classes/methods for Javadoc

iceberg When developing new classes/methods for a Java project, sometimes you want people to try out your new code, but don't want to guarantee that it will be backward compatible in future releases. In this case, use something like the @Unstable annotation to

Annotating unstable classes/methods for Javadoc

iceberg When developing new classes/methods for a Java project, sometimes you want people to try out your new code, but you don't want to guarantee that it will be backward compatible in future releases. In this case, use something like the @Unstable annotatio

Annotating unstable classes/methods for Javadoc

iceberg When developing new classes/methods for a Java project, sometimes you want people to try out your new code, but you don't want to guarantee that it will be backward compatible in future releases. In this case, use something like the @Unstable annotatio

Hibernate/JPA - Annotating bean methods vs fields

benstpierre: I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wit

Hibernate/JPA - Annotating bean methods vs fields

benstpierre: I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wit

Hibernate/JPA - Annotating bean methods vs fields

Benstpierre : I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wi

Annotating in the same model produces unexpected results

Naresh I have a User model with the following fields. class User(models.Model): user_id = models.CharField(max_length=40,unique=True) user_name = models.CharField(max_length=40) user_email = models.EmailField() user_city = models.CharField(max_

Annotating in the same model produces unexpected results

Naresh I have a User model with the following fields. class User(models.Model): user_id = models.CharField(max_length=40,unique=True) user_name = models.CharField(max_length=40) user_email = models.EmailField() user_city = models.CharField(max_

Annotating in the same model produces unexpected results

Naresh I have a User model with the following fields. class User(models.Model): user_id = models.CharField(max_length=40,unique=True) user_name = models.CharField(max_length=40) user_email = models.EmailField() user_city = models.CharField(max_

Spring @Transactional on classes and methods

ex3v I am using Spring Bootand Spring Data JPA. There are the following courses: import org.springframework.transaction.annotation.Transactional; @Transactional(propagation = Propagation.REQUIRED) public class Foo{ public void bar(){} } bar()and any ot

Java - Transactional Methods on Objects

bavran I have a simple Java object (not connected to any database) and would like to have transactional semantics on it. Here is a contrived example: class MySession { public User user; public int numLogins; } public void login(MySession session, St

Nesting @Transactional methods with @Async

Michael Ressler: I am using Spring with JPA. me @EnableAsyncand @EnableTransactionManagementturn on. In my user registration service method, I call some other annotated service methods @Async. These methods do things like send welcome emails and register newly

Spring @Transactional on classes and methods

ex3v I am using Spring Bootand Spring Data JPA. There are the following courses: import org.springframework.transaction.annotation.Transactional; @Transactional(propagation = Propagation.REQUIRED) public class Foo{ public void bar(){} } bar()and any ot

Java - Transactional Methods on Objects

bavran I have a simple Java object (not connected to any database) and would like to have transactional semantics on it. Here is a contrived example: class MySession { public User user; public int numLogins; } public void login(MySession session, St

Nesting @Transactional methods with @Async

Michael Ressler I am using Spring with JPA. me @EnableAsyncand @EnableTransactionManagementturn on. In my user registration service method, I call some other annotated service methods @Async. These methods do things like send welcome emails and register newly

Spring @Transactional on classes and methods

ex3v I am using Spring Bootand Spring Data JPA. There are the following courses: import org.springframework.transaction.annotation.Transactional; @Transactional(propagation = Propagation.REQUIRED) public class Foo{ public void bar(){} } bar()and any ot

Java - Transactional Methods on Objects

bavran I have a simple Java object (not connected to any database) and would like to have transactional semantics on it. Here is a contrived example: class MySession { public User user; public int numLogins; } public void login(MySession session, St

@Transactional vs static methods

Karibasappa GC Why can't a static method be used to manage transactions @Transactionalin a spring project ? @TransactionalWorks great for non-static methods, but static methods don't work for any particular reason? Geoand In order to understand why something l

Nesting @Transactional methods with @Async

Michael Ressler I am using Spring with JPA. me @EnableAsyncand @EnableTransactionManagementturn on. In my user registration service method, I call some other annotated service methods @Async. These methods do things like send welcome emails and register newly

Spring @Transactional on classes and methods

ex3v I am using Spring Bootand Spring Data JPA. There are the following courses: import org.springframework.transaction.annotation.Transactional; @Transactional(propagation = Propagation.REQUIRED) public class Foo{ public void bar(){} } bar()and any ot