| By Christopher Richardson | Article Rating: |
|
| February 27, 2006 02:15 PM EST | Reads: |
103,201 |
EJB 3 Transaction Management
In EJB 3, declarative transaction management is, as you might expect, one of the built-in features of session and message-driven beans. You configure transaction management using either Java 5 annotations or a more traditional EJB deployment descriptor. This means, for example, that the MoneyTransferService EJB shown in Listing 2 will automatically be transactional.
Now that we've looked at how to make POJOs transactional and persistent let's examine the dangers of using annotated POJOs.
Beware of Annotated POJOs
The examples in this article use both XML and annotations to configure POJOs. However, the trend in POJO programming is away from XML configuration files and towards annotations. Annotations are certainly convenient because they're physically next to the program element (class, field, method) that they configure. The metadata is more concise and easier to maintain. There's certainly nothing wrong with using application-specific annotations. But, the problem with framework-specific annotations is that they couple your application to the framework in the same way that an API call does (www.aspectprogrammer.org/blogs/adrian/2004/08/
when_is_a_pojo.html).
For example, if you use Spring's @Transactional attribute then your classes won't compile without Spring. Similarly, if you use EJB 3 annotations then your classes will be coupled to the EJB 3 framework. These annotations won't stop you from testing your code with simple JUnit tests, but are classes that use them still POJOs?
Moreover, other annotations such as EJB 3's @EJB annotation, which injects an EJB, let you write code that's difficult to test without running it in the EJB container.
class SomeServiceImpl {
@EJB private AccountDAO accountDAO;...
}
The @ EJB annotation tells the EJB container to "magically" initialize the private field on creation. Not a very POJO-like operation. Similarly, other frameworks' annotations couple your code to the database schema.
These kinds of annotations are definitely handy but using them will couple your application to the framework. That makes it difficult to support multiple frameworks simultaneously. Migrating to a later version or to a new framework can be difficult because you might have to change all of the annotations. This isn't quite as bad as rewriting the code but you still have to change the Java source and most importantly, you have to recompile. If you want to decouple your application from evolving infrastructure frameworks then you might want to consider using pure POJOs and XML metadata.
Summary
Developing with POJOs and non-invasive frameworks such as Spring, Hibernate, JDO, and EJB 3 provide many benefits. POJOs simplify and accelerate development. They make it easier to test classes in isolation without deploying them in an application server, a process that often slows down the edit-compile-debug cycle. And because POJOs are decoupled from the infrastructure frameworks that provide services such as transaction management and persistence you can focus on developing the business logic and address those concerns later.
POJOs also make it easier to upgrade your application to newer version of a framework or switch to a different framework entirely. They give your application increased immunity from the problems caused by rapidly evolving infrastructure frameworks. POJOs enable us to build Enterprise Java applications that last.
References
- Acegi Security System, http://acegisecurity.sourceforge.net/
- Download the sample code from www.pojosinaction.com
- Colyer, A. www.aspectprogrammer.org/blogs/adrian/2004/08/when_is_a_pojo.html
- Fowler M. www.martinfowler.com/articles/injection.html
- Fowler M. www.martinfowler.com/bliki/POJO.html
- Richardson C. (2005). POJOs in Action. Manning
- Richardson C. 2002, www.theserverside.com/articles/article.tss?l=TwoLevelDomainModel
- Vinge, V. (2000). A Deepness in the Sky. Tor Books.
I would like to thank Chris Smith, Michael Yuan, Jennifer Shi, David Vydra, and Robert Benson for commenting on this article.
Published February 27, 2006 Reads 103,201
Copyright © 2006 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Christopher Richardson
Chris Richardson is the author of the recently published POJOs in Action. He's a developer, architect, and mentor with over 20 years of experience. Chris runs a consulting company that jumpstarts new development projects and helps teams that are frustrated with Enterprise Java to become more productive and successful. He lives in Oakland, CA with his wife and three children.
![]() |
Preet 06/24/08 01:42:08 PM EDT | |||
interesting read... answers the what, but, and what ifs.. |
||||
![]() |
Mathan 02/26/08 10:39:21 AM EST | |||
The main advantage of the POJOs are felt in distributed applications. Assume that your application is consuming services from 10 different applications. If one of them is down, then your application is effectively down. Instead, if you have the POJOs for all those apps bundled in your app, then your application can stand alone without any dependency |
||||
![]() |
rogerv 03/28/06 05:20:31 PM EST | |||
What Is POJO Programming? Oh - that's where you write 30% of a Java application in a declarative, XML-based domain specific language. For all practical purposes (and by very definition) the domain specific language will be unique to a given POJO container and hence will render the Java application essentially unportable. |
||||
![]() |
Patrick Ellul 03/14/06 10:25:04 PM EST | |||
I found this article very helpful with introducing me to the concepts behind POJO's and ORM's and frameworks such as Spring and Hibernate. |
||||
![]() |
Justin Peck 03/13/06 12:28:09 AM EST | |||
I liked this article so much, I wrote a Web application after having read it. Spring really does make programming Java-base Web apps fun again. Thanks for the article! [You can see the app now at http://javajuster.com] |
||||
![]() |
Alain Ah Ming 02/26/06 05:21:30 AM EST | |||
Is this simply replacing the underlying entity EJB with the value object? would that would make it as good as what POJO prog is trying to achieve? From our test class, we can easily invoke our business logic helper class (which is a pure POJO) by injecting a test DAO impl class. |
||||
![]() |
harris reynolds 02/23/06 05:39:06 PM EST | |||
Trackback Added: JDJ RIP; I receive several trade rags about technolgy. Most of them get stacked up in a pile by my desk that I eventually go through after they start cluttering the office. When going through the latest round of magazines recently I... |
||||
![]() |
SYS-CON Italy News Desk 02/21/06 01:40:28 PM EST | |||
The novel A Deepness in the Sky by Vernor Vinge is set in the distant future. The character Pham Nuwen is responsible for maintaining software whose components are thousands of years old. Today, however, it's difficult to imagine maintaining an Enterprise Java application for more than a few years. More often than not, the application is tightly coupled to infrastructure frameworks that evolve rapidly in ways that don't preserve backwards compatibility. Consequently, upgrading to a new and improved framework can be challenging and risky. |
||||
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- Industry Experts Discuss the State of Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Performance Tuning Essentials for Java
- Confessions of a Ulitzer Addict
- It's the Java vs. C++ Shootout Revisited!
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer Aid Campaign for the Typhoon Ondoy Victims
- Cloud Computing Can Revitalize Your Career as Software Developer
- A Brief History of Cloud Computing
- Oracle & Cloud Computing: Exclusive Q&A with SVP Richard Sarwal
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Why IBM’s Server Chief Got Busted
- Industry Experts Discuss the State of Cloud Computing
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Performance Tuning Essentials for Java
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- It's the Java vs. C++ Shootout Revisited!
- The End of IT 1.0 As We Know It Has Begun
- My Thoughts on Ulitzer
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- i-Technology Predictions for 2007: Where's It All Headed?
































