Transaction Best Practices

We recommend the following transaction management best practices :

  • Use declarative transaction management rather than programmatic transactions whenever possible.

  • When using declarative transactions with EJBs, consider using Required as the first choice for a transaction attribute.

  • When using distributed transactions, use XA-enabled resources and XA-enabled drivers whenever possible. Be aware of the limitations of emulating XA for a non-XA resource.

  • Avoid mixing direct transaction management using JTA with implicit transaction management done through a resource-specific API such as JDBC or JMS since this complicates the transaction management picture.

  • Consider leveraging the session façade design pattern for creating coarse-grained transactions.

  • Minimize the use of client-managed transactions wherever possible. The session façade can be useful here.

  • Carefully evaluate the need and implementation of an optimistic concurrency strategy.

Transactions help you to manage very complex, and even unpredictable, system behavior when dealing with shared networked resources. But as this chapter has demonstrated, you must learn to leverage transactions in the most efficient and straightforward manner possible. To that end, having a solid understanding of transaction terms and concepts such as ACID properties, local versus distributed transactions, the flat transaction model, two-phase commit, isolation problems and levels, and optimistic concurrency is essential.

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.