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 ...

Get Java Enterprise in a Nutshell, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.