简体   繁体   中英

Spring Data CrudRepository and Transactions

I'm trying to implement transactions on a CrudRepository Interface. I'm a beginner with this and my current problem is that when receiving a lot of requests from different clients, I'm sometimes getting a duplicate. To avoid that I wanted to use SQL Transactions and their implementation with Spring but I'm unable to get it working.

Here is how I've tried to do it :

@Repository
@EnableTransactionManagement
@Transactional
public interface ApplicationPackageDao extends CrudRepository<ApplicationPackage, Long> {

/**
 * Find if a record exists for this package name ,
 * @param packageName
 * @return
 */
@Transactional
ApplicationPackage findByPackageName(String packageName);

}

However it doesn't seem to work. I tried to add the @Transactionnal annotations earlier in the Java methods I'm calling but I can't get it working either.

How am I supposed to work with transactions on CrudRepository ? Or am I using completely the wrong thing?

In addition to crm86's answer some more notes to the @Transactional annotation:

  • It seems to be best practice to annotate the entry points into your application (eg your web controller methods or the main method of a scheduled batch). By using the annotation attribute TxType you can ensure constraints/conditions in methods which are located deeper in your application (eg TxType.MANDATORY would throw if no trx-context is running, etc.).

  • The @Transactional annotation has only an effect if the class is loaded as spring bean (eg @Component annotation at class level).

  • Remember that only RuntimeException's lead to a rollback. If you want a checked Exception leading to a rollback you have to enumerate each such Exception by using the attribute rollbackOn.

  • The annotation at class level is valid for all public methods of this class. Method level annotations override those at the class level. The repeated annotation in your example above (first at class level, then at method level) has no effect.

What I suggest:

Check your context and configuration classes with @Configuration annotation. From the documentation :

The @EnableTransactionManagement annotation provides equivalent support if you are using Java based configuration. Simply add the annotation to a @Configuration class

@EnableTransactionManagement and only looks for @Transactional on beans in the same application context they are defined in

Then you could use @Transactional in your service even in a method

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM