Skip to content

Commit

Permalink
Summary
Browse files Browse the repository at this point in the history
Add ModifyingTransactionAspect for conditional transactional support.

Description
Introduced an aspect to handle methods annotated with @Modifying in Spring Data JPA. The aspect checks the transactional parameter in the annotation to decide whether to open a new transaction explicitly.

Added logic to handle transaction management using PlatformTransactionManager.
Includes support for both transactional and non-transactional method executions.
Updated the class-level documentation to explain its purpose, usage, and dependencies.
This improvement allows developers to gain fine-grained control over transactional behavior for @Modifying queries.

References
Related tickets: spring-projects#3733.
  • Loading branch information
petro-zhylenko committed Jan 9, 2025
1 parent edce922 commit fc5c7ff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @author Christoph Strobl
* @author Nicolas Cirigliano
* @author Jens Schauder
* @author Petro Zhylenko
* @see Query
*/
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
* Aspect for handling @Modifying annotations with conditional transactional support.
* <p>
* This aspect intercepts methods annotated with @Modifying and determines whether a transaction
* should be opened based on the `transactional` parameter of the annotation.
* <p>
* - If `transactional` is set to `true`, a new transaction is explicitly opened using
* {@link PlatformTransactionManager}, and the method execution is wrapped within this transaction.
* - If `transactional` is set to `false`, the method executes without opening a transaction.
* <p>
* Usage:
* This aspect works with Spring Data JPA's @Modifying annotation, allowing for fine-grained control
* over transaction management for specific methods.
* <p>
* Example:
* <pre>
* {@code
* @Modifying(transactional = true)
* @Query("UPDATE YourEntity y SET y.field = :value WHERE y.id = :id")
* void updateEntity(@Param("value") String value, @Param("id") Long id);
* }
* </pre>
* <p>
* Dependencies:
* - Requires a {@link PlatformTransactionManager} bean to be configured in the application context.
* - Should be used in conjunction with Spring AOP or AspectJ for proper weaving.
*
* @author Petro Zhylenko
*/
@Aspect
@Component
public class ModifyingTransactionAspect {
Expand Down

0 comments on commit fc5c7ff

Please sign in to comment.