miyukiutada
Photo credit: miyukiutada
del.icio.us Digg DZone Reddit StumbleUpon
1 | 2 | 3 | Next »
Software Development

Annotation-Based Transactions in Spring

Use Spring annotations to vastly simplify transaction management.

In this article, I'm going to show you how to use transaction management annotations in Spring. I'm using Spring 2.5 though these features have been available at least since Spring 2.0.

Spring provides several different approaches to transaction management:

  • Programmatic: You can write your transactions using Java source code directly. Normally you wouldn't do this unless you needed fine-grained control over some particular transaction.
  • Declarative: You can declare transaction definitions in either a centralized way using XML or in a distributed way using annotations:
    • XML-based: You can configure transactions in Spring's centralized application context file. Once again you have options:
      • Proxy-based: In this approach you wrap your service beans with transactional proxies. The proxy and the service bean both implement the same Java interface so the application can't tell them apart. This is a fairly verbose approach with respect to the actual context configuration though there are techniques you can use to streamline the configuration.
      • AOP-based: Here you define AOP aspects to endow your service beans with transactional semantics. Not quite as verbose as the proxy-based approach.
    • Annotation-based: You can use Java 5 annotations to distribute transaction configuration across Java classes.

As the title suggests, this article deals with annotation-based configuration. In my opinion this is the cleanest and most sensible approach: context file configuration is almost nil and the transaction definitions are conveniently located with the service interfaces and/or beans themselves.

Let's start with a basic overview of transactions, and then we'll get to the good stuff.

Social bookmarks: del.icio.us Digg DZone Reddit StumbleUpon
1 | 2 | 3 | Next »
Show comments (9)

Post a comment

Your name:
Your e-mail address (won't be displayed):
Your web site (optional):
example: www.xyz.com
Your comment:
Please help us prevent comment spam:

What's New?

2008-10-20 - I've added a new mailing list feature to the site. Sign up to receive e-mail updates about new articles.
2008-09-30 - We've released chapter 4 (User registration) and chapter 5 (Authentication) of Spring in Practice.
2008-09-11 - By popular demand, I've added an RSS feed to the site.
Home | Consulting | Tech Articles | Mailing List | About | Contact | Spring Blog
Copyright © 2008 Wheeler Software, LLC.