How to use @Transactional with Spring Data?

I just started working on a Spring-data, Hibernate, MySQL, JPA project. I switched to spring-data so that I wouldn't have to worry about creating queries by hand.

I noticed that the use of @Transactional isn't required when you're using spring-data since I also tried my queries without the annotation.

Is there a specific reason why I should/shouldn't be using the @Transactional annotation?


public List listStudentsBySchool(long id) {
    return repository.findByClasses_School_Id(id);

Also works:

public List listStudentsBySchool(long id) {
    return repository.findByClasses_School_Id(id);

Thanks in advance!


What is your question actually about? The usage of the @Repository annotation or @Transactional.

@Repository is not needed at all as the interface you declare will be backed by a proxy the Spring Data infrastructure creates and activates exception translation for anyway. So using this annotation on a Spring Data repository interface does not have any effect at all.

@Transactional - for the JPA module we have this annotation on the implementation class backing the proxy (SimpleJpaRepository). This is for two reasons: first, persisting and deleting objects requires a transaction in JPA. Thus we need to make sure a transaction is running, which we do by having the method annotated with @Transactional.

Reading methods like findAll() and findOne(…) are using @Transactional(readOnly = true) which is not strictly necessary but triggers a few optimizations in the transaction infrastructure (setting the FlushMode to MANUAL to let persistence providers potentially skip dirty checks when closing the EntityManager). Beyond that the flag is set on the JDBC Connection as well which causes further optimizations on that level.

Depending on what database you use it can omit table locks or even reject write operations you might trigger accidentally. Thus we recommend using @Transactional(readOnly = true) for query methods as well which you can easily achieve adding that annotation to you repository interface. Make sure you add a plain @Transactional to the manipulating methods you might have declared or re-decorated in that interface.

I think that the question is a little bit wider and cannot be reduced on the annotations on the data access layer. We need to consider the entire stack of the application, the transaction strategies we want to apply and so on. There is a very comprehensive set of articles on this topic by Mark Richards on IBM developerworks site. You can find the first one here:

Best Regards

You should use @Repository annotation

This is because @Repository is used for translating your unchecked SQL exception to Spring Excpetion and the only exception you should deal is DataAccessException

Need Your Help

dismissViewControllerAnimated is called but ViewController is not dismissed

iphone objective-c ios ios4 ios5

I am having a problems with the dismissViewControllerAnimated method not closing down the view.

How do you shrink a UIPickerView on the iPhone?

ios interface-builder uipickerview picker

I would like to reduce the height of a UIPickerView in my iPhone app, so that it shows only one row and one column. The height of the picker view should be equal to the height of a row.