We are using Spring+JPA for our RESTful web services application. This is a high transactional application and performs plenty of CRUD operations.
I am using @Transaction annotation on methods to perform the transaction and all is working fine.
I was just wondering if the Transaction can be managed outside the class in the configuration file based on pattern matching, i.e. all methods starting with add/update/delete can automatically be Transaction enabled (without the use of @Transaction annotation)?
Could someone please confirm if that’s possible?
If ‘Yes’ please provide me with some web link or example.
Thanks.
Of course! In fact this was the only possible solution before Java 5 and
@Transactionalannotation. Look at 10.5.2 Example of declarative transaction implementation in Spring documentation. There you will find examples of transaction demarcation configuration via XML and AspectJ pointcuts.This is a simple configuration excerpt from the documentation pointed above:
As you can see, all methods with name prefixed with
getare marked as read-only, while all other methods ofFooService(x.y.service.FooService.*(..))pointcut) are transactional and not read-only.As you can see using more verbose XML syntax gives you some great benefits like declarative and more flexible transaction demarcation, mainly thanks to AspectJ syntax (make sure to learn it first).