O'Reilly logo

Java Enterprise in a Nutshell, Third Edition by William Crawford, Jim Farley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Programmatic Transactions Versus Declarative Transactions

You can approach coding transactions in two basic ways: programmatically or declaratively. When possible, declarative transactions are a better choice since they allow the container to manage the transaction for you, and this saves you from having to put JTA transaction management calls into your application code. But as we’ll see, not every scenario can be adapted to use declarative transactions. We’ll start by examining programmatic transaction management, then discuss how declarative transaction management can supplant some programmatic calls.

Programmatic Transactions

Typically, you implement transactions in an application using one of the resource-specific transaction APIs such as JDBC or JMS. If the application requires distributed transactions, you implement the transaction code using JTA.

The JTA can be used in a number of programming contexts, including web and EJB components, Swing- or SWT-based desktop clients, and many others. The application code begins a transaction and then performs some operations against one or more resource managers (databases accessed via JDBC, message servers accessed through JMS, EJB components accessed via the EJB API, etc.). At some point, the application decides to either commit or roll back the transaction. The container managing the application’s runtime environment, including its transaction manager, propagates the transactional context to the different components and resources that ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required