Custom Shortcut Annotation
If you find that you are repeatedly using the same attributes with @Transactional on many different methods in your application, then Spring’s meta-annotation support allows you to define custom shortcut annotations. Listing 9-18 shows creating and using these stereotype annotations to avoid repeated code in your application.
Listing 9-18. Custom annotation to avoid repeated annotations in your classes
[java]Consider your service class as shown below:
@Transactional
public class LoyaltyService {
@SuppressWarnings("unchecked")
@Transactional(“txOne”)
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.METHOD)
public void doOperationOne(String attrib) {
//…
}
@SuppressWarnings("unchecked")
@Transactional(“txTwo”)
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.METHOD)
public void doOperationOne(String attrib) {
//…
}
}[/java]
It seems that your method signature is covered with annotations and it is not so readable. To avoid this you can write a stereotype annotation which wraps these annotations within as shown below:
[java]@SuppressWarnings("unchecked")
@Transactional(“txOne”)
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.METHOD)
public @interface TransactionOne { …. }
@SuppressWarnings("unchecked")
@Transactional(“txTwo”)
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.METHOD)
public @interface TransactionTwo{ …. }[/java]
After creating these stereotype annotations, now your LoyaltyService class will be as shown below:
[java]@Transactional
public class LoyaltyService {
@TransactionOne
public void doOperationOne(String attrib) {
//…
}
@TransactionTwo
public void doOperationOne(String attrib) {
//…
}
}[/java]
Spring Testing Support for Transactions
The TestContext framework provides transaction support Spring enabled tests via the PlatformTransactionManager bean in the application context and the @Transactional annotation.
Spring also support various test specific annotations which, used in conjunction with the TestContext framework allow for full control over the transaction configuration: @TransactionConfiguration, @Rollback and @BeforeTransaction/@AfterTransaction.
Summary
Transactions help ensure data integrity in your application. Spring has two main transaction styles namely; programmatic and declarative. Declarative is the preferred styles due to its various advantages like; simple, non-invasive and capability of handling much of boilerplate code.
After reading this chapter you should have a clear idea of various core transaction concepts like isolation level, transaction propagation, transaction models and so forth. The initial section covered these core concepts and also touched up these concepts with respect to Spring Framework.
After that we shifted our focus to Spring’s way of handling transaction management and linked it with the core concepts of transaction management. We covered all the transaction management styles in detail with code samples. You should now have clear idea on the various transaction styles in Spring Framework along with through thoughts on how to configure these in your application.
We then covered various JTA implementations available in software industry and how Spring allows to plug these into it with ease using sample code snippets.
Page Visitors: 9315

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Thanks, nice chapter.