开发者

Spring Data Jpa / Hades Transaction Behaviour

From Spring Data JPA reference (previously Hades),

CRUD methods on repository instances are transactional by default.

If a me开发者_如何学JAVAthod covers more than one repository, example:

@Transactional
public void addRoleToAllUsers(String roleName) {
    Role role = roleRepository.findByName(roleName);
    for (User user : userRepository.readAll()) {
        user.addRole(role);
        userRepository.save(user);
    }
}

The reference states

The transaction configuration at the repositories will be neglected then as the outer transaction configuration determines the actual one used.

The behavior inner transactions will be neglected is a default spring transaction behavior or actually explicitly handled by Spring Data JPA?


It's a default behaviour.

As you can see, methods of JpaRepository are annotated as @Transactional with propagation by default (REQUIRED), and it's a normal behaviour for that propagation type (see 10.5.7 Transaction propagation).


The @Transactional at the JpaRepository/the implementation class will cause these methods simply taking part in the outer transaction by default. So if you think "neglect" is a misleading word feel free to open a JIRA for it (I think it is to some degree ;) ).

If you want to change the configuration of transactions for the CRUD methods you can do so by simply redeclaring the CRUD method inside your concrete repository interface and add an @Transactional to it containing the config you want to have. See the reference documentation for details.


It all depends on the propagation set in the @Transactional annotation. By default, it's set to REQUIRED, which means : if no transaction context, create one and commit at the end of the method; else, include the method call inside the existing transaction context.

There are other ones : REQUIRES_NEW, SUPPORTS, NEVER, etc. See http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/transaction/annotation/Propagation.html for details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜