Spring Book – Chapter 9 – Transaction Management

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: 8819

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

1 thought on “Spring Book – Chapter 9 – Transaction Management

Leave a Reply

Your email address will not be published. Required fields are marked *