Skip to Content
Java EE 7 Essentials
book

Java EE 7 Essentials

by Arun Gupta
August 2013
Intermediate to advanced
362 pages
8h 40m
English
O'Reilly Media, Inc.
Content preview from Java EE 7 Essentials

Chapter 12. Java Transaction

The Java Transaction API (JTA) is defined as JSR 907, and the complete specification can be downloaded.

The JTA specifies local interfaces between a transaction manager and the parties involved in a distributed transaction system: the application, the resource manager, and the application server.

The API defines a high-level interface, annotation, and scope to demarcate transaction boundaries in a transactional application.

User-Managed Transactions

The UserTransaction interface enables the application to control transaction boundaries programmatically. This interface is typically used in EJBs with bean-managed transactions (BMT).

You can obtain UserTransaction using injection:

@Inject UserTransaction ut;

or through a JNDI lookup using the name java:comp/UserTransaction:

Context context = new InitialContext();
UserTransaction ut = (UserTransaction)context.lookup("java:comp/UserTransaction");

The begin method starts a global transaction and associates the transaction with the calling thread. The commit method completes the transaction associated with the current thread. All statements within begin and commit execute in the transaction scope:

ut.begin();
//. . .
ut.commit();

When the commit method completes, the thread is no longer associated with a transaction.

A transaction can be rolled back via the rollback method:

ut.begin();
//. . .
ut.rollback();

When the rollback method completes, the thread is no longer associated with a transaction.

You can change the timeout ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Beginning Java EE 7

Beginning Java EE 7

Antonio Goncalves
Java EE 8 High Performance

Java EE 8 High Performance

Romain Manni-Bucau

Publisher Resources

ISBN: 9781449370589Errata Page