getTransaction() on transaction-scoped persistence context.

Assuming that "em" is a reference to the EntityManager with transaction-scoped persistence context, what is the state of the entity referenced by "cust" after the execution of statement 05?

[A] new
[B] managed
[C] detached
[D] remove

Explanation: The "cust" entity becomes managed after the execution of statement 04. But at the end of the transaction in statement 05, all the entities associated with the persistence context becomes detached. Hence after the execution of statement 05, the "cust" variable references a detached entity.

According to WhizLabs, the correct answer is [C].

But I feel it is wrong. Well, the question says that the Entity Manager is with transaction-scoped persistence context. If it is transaction-scoped, then the entity manager must be a container-managed entity manager. As container-managed entity managers always uses the JTA transactions, then this EntityManager's persistence context must be associated with a JTA transaction. In that case, em.getTransaction() method here, must throw IllegalStateException, because getTransaction() method cannot be invoked on a JTA EntityManager. Now, the entity is still in NEW state. Do you agree?

Can you prove it? Then show me a sample code about how to create an application-managed entity manager with transaction-scoped persistence context. (Because the question said it is a transaction-scoped persistence context). As far as I know, application managed entity managers are always in with extended persistence context.

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277

posted Mar 27, 2009 11:17:30

0

[quote=Treimin Clark][quote=Niranjan Deshpande]the code above is using application managed entity manager and not a container managed entity manager.[/quote]
Can you prove it? Then show me a sample code about how to create an application-managed entity manager with transaction-scoped persistence context. (Because the question said it is a transaction-scoped persistence context). As far as I know, [b]application managed[/b] entity managers are [b]always [/b]in with [b]extended [/b]persistence context.

[/quote]

1.Since your code is using em.getTransaction().xxxx(); methods, we can say that your appliction is manging the transactions! This proves that this is a Application Managed Entity Manager! Your are manually starting and comitting the transactions, which usually is absent in the code that uses container managed transactions!

[u][b]Creating application scoped EM in Java EE:[/b][/u]
[i]Following code demonstrates injection of an EntityManagerFactory into a servlet and the use of it to create a short-lived (transaction-scoped) entity manager in order to verify a user id.[/i]
[code]
public class LoginServlet extends HttpServlet {
@PersistenceUnit(unitName="EmployeeService")
EntityManagerFactory emf;

If you need to make a extended persistence context, all you need to use is:e.g code [i]DepartmentManagerBean to audit when an employee is added to a department.[/i]
[code]
@Stateful
public class DepartmentManagerBean implements DepartmentManager {
@PersistenceContext(unitName="EmployeeService",
type=PersistenceContextType.EXTENDED)
EntityManager em;
Department dept;
@EJB AuditService audit;

3. From 2. above it's clear that 'Application Managed Entity Managers can be of either type: extended/transaction scoped!

Hope I cleared all your points!

I would recommend you to read chapter 5 of Pro EJB3.0 book.

Guys, please add on this explanation if you find I missed something!

Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757

posted Mar 27, 2009 14:19:40

0

Niranjan Deshpande wrote:3. From 2. above it's clear that 'Application Managed Entity Managers can be of either type: extended/transaction scoped!

Sorry but I couldn't see a transaction-scoped entity manager from your example. Your first program shows an application-managed extended scoped Entity Manager with resource-local transaction, and your second example shows a container-managed extended scoped Entity Manager with JTA transaction.

Finally, this is from the JPA specification (JPA 3.3 - page 53):

JPA Specs wrote:The scope of the persistence context of an application-managed entity manager is extended. It is the responsibility of the application to manage the lifecycle of the persistence context.

Treimin Clark wrote: But I feel it is wrong. Well, the question says that the Entity Manager is with transaction-scoped persistence context. If it is transaction-scoped, then the entity manager must be a container-managed entity manager. As container-managed entity managers always uses the JTA transactions, then this EntityManager's persistence context must be associated with a JTA transaction. In that case, em.getTransaction() method here, must throw IllegalStateException, because getTransaction() method cannot be invoked on a JTA EntityManager. Now, the entity is still in NEW state. Do you agree?

I agree.

But the Whizzlab question is nevertheless interesting: If you replace "em.getTransaction" through "userTransaction" in lines 03, 05 the question is really good.

[color=white]"Ranch Hand wrote
3. From 2. above it's clear that 'Application Managed Entity Managers can be of either type: extended/transaction scoped!"[/color]

It's wrong!
All application-managed persistence contexts are extended in scope, and may span multiple transactions.
you can find the detail by this :http://www.ucertify.com/article/what-is-an-application-managed-persistence-context.html

I think you are very much confuse with JTA transaction. if EntityManager with transaction-scoped persistence context, then transaction will be Stateless mode , and after commit it will be in detached mode. if you want to know how it works with JTA read following line carefully.

the transaction-scoped entity manager as stateless. If that is the case, how can it
work with a persistence context? The answer lies with the JTA transaction. All container-managed
entity managers depend on JTA transactions because they can use the transaction as a way to track
persistence contexts. Every time an operation is invoked on the entity manager, the container proxy
for that entity manager checks to see whether a persistence context is associated with the container
JTA transaction. If it finds one, the entity manager will use this persistence context. If it doesn’t find
one, it creates a new persistence context and associates it with the transaction. When the transaction
ends, the persistence context goes away.