19

I am currently having a problem with understanding a concept of JPA.

I am currently using/developing recent EclipseLink, Glassfish, Derby database to demonstrate a project.

Before I develop something in much bigger picture, I need to be absolutely sure of how this PersistingUnit work in terms of different scopes.

I have bunch of servlets 3.0 and currently saving user's associated entity classes in the request.session object (everything in the same war file). I am currently using Application-managed EntityManager using EntityManagerFactory and UserTransaction injection. It works smooth when it is tested by myself. The different versions of entities occur when 2 people accessing the same entities at the same time. I want to work with managed beans cross the same WAR, same persistence unit if possible.

I have read http://docs.oracle.com/javaee/6/tutorial/doc/bnbqw.html and bunch of explanations of those scopes which don't make sense at all for me.

Long story short, what are the usage and difference of app and container managed EntityManagers?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Hayati Guvence
  • 688
  • 2
  • 6
  • 22

2 Answers2

21

When you say application managed transaction it means its your code which is supposed to handle the transaction. In a nutshell it means:

You call:

entityManager.getTransaction().begin(); //to start a transaction

then if success you will ensure to call

entityManager.getTranasaction().commit(); //to commit changes to database

or in case of failure you will make sure to call:

entityManager.getTransaction().rollBack();

Now imagine you have a container, which knows when to call begin(), commit() or rollback(), thats container managed transaction. Someone taking care of transaction on your behalf.

You just need to specify that.

Daniel Szalay
  • 3,852
  • 12
  • 54
  • 100
mprabhat
  • 19,229
  • 7
  • 42
  • 62
  • what do you mean by "Now imagine you have a container, who knows when to call being, commit or rollback, thats container managed transaction. Someone taking care of transaction on your behalf."? – Hayati Guvence Dec 08 '11 at 14:06
  • Container is like Spring, where you configure your bean you specify what is your tansaction entity like you configure an entity bean, you specify that your bean as @Transactional and your transaction is taken care by Spring – mprabhat Dec 08 '11 at 14:16
  • 3
    I have found the question which reflects what I was searching for - "usage and difference of app and container managed EntityManagers", only to find out that the accepted answer talks about something different - container vs application handled _transactions_. On top of that, the answer starts with "When you say application managed transaction", which OP did not say. – alterfox Oct 25 '13 at 13:32
12

Container managed transaction(CMT) could be regarded as a kind of declarative transaction, in which case, transaction management is delegated to container (normally EJB container), and much development work could be simplified.

If we are in a Java EE environment with an EJB container, we could use CMT directly.

If we are in a Java SE environment, or a Java EE environment without an EJB container, we could still take advantage of CMT, one way is to use Spring, which uses AOP to implement declarative transaction management; Another way is to use Guice, which uses a PersistFilter to implement declarative transaction.

In CMT, a container (whatever an EJB container, Spring or Guice) will take care of the transaction propagation and commit/rollback stuff;

Application managed transaction (AMT) differs from CMT in that we need to handle transactions programmatically in our code.

Arjan Tijms
  • 36,666
  • 12
  • 105
  • 134
aqingsao
  • 2,013
  • 1
  • 17
  • 17
  • 4
    Good answer, but don't forget that every official Java EE environment always has an EJB container. Otherwise it can't be called Java EE (Tomcat is thus not an official Java EE environment). Also, in Java EE the opposite of CMT is called BMT; Bean Managed Transactions. – Arjan Tijms Feb 17 '13 at 10:57
  • Thanks a lot for your complement to this answer. – aqingsao Aug 08 '13 at 04:00