• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Tyronne Fernando
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!