*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EJB3 in Action - JTA transaction within a application-managed EntityManager within a container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EJB3 in Action - JTA transaction within a application-managed EntityManager within a container" Watch "EJB3 in Action - JTA transaction within a application-managed EntityManager within a container" New topic
Author

EJB3 in Action - JTA transaction within a application-managed EntityManager within a container

Tyronne Fernando
Ranch Hand

Joined: May 11, 2008
Posts: 50
@Stateless
public class ItemManagerBean implements ItemManager {
@PersistenceUnit private EntityManagerFactory entityManagerFactory;
private EntityManager entityManager;
public ItemManagerBean() {}

@PostConstruct public void initialize() {
entityManager = entityManagerFactory.createEntityManager();
}
...
public Item updateItem(Item item) {
entityManager.joinTransaction();
entityManager.merge(item);
return item;
}
...
@PreDestroy
public void cleanup() {
if (entityManager.isOpen()) {
entityManager.close();
}
}
...
}


The above code is from pages 308 and 309 of "EJB 3 in Action" book. This bean is using an application-managed EntityManager within a container. In the updateItem() method where does the JTA transaction comes from when we call entityManager.joinTransaction()? Is it created by the container?

Thanks in advance.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
It's a container managed JTA-Transaction. The transaction isn't created in the code. So it should be created by the containter.

void joinTransaction()

Indicate to the EntityManager that a JTA transaction is active. This method should be called on a JTA application managed EntityManager that was created outside the scope of the active transaction to associate it with the current JTA transaction.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EJB3 in Action - JTA transaction within a application-managed EntityManager within a container
 
Similar Threads
Persitence confusion. Please help.
Container-Manager EntityManager - Automatically joins JTA ?
Enthuware Persistence Context Question
EntityManagers and transactions
MZs Notes - Pg 159