• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getTransaction() on transaction-scoped persistence context.

 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from WhizLabs Q32, of Practice Exam 2.

Consider the code below where Customer is an entity bean.


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?
 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clark,

pro EJB3 Chapter 5 has crystal clear explanation for all of the EntityManager concepts. Please check out the explanation that revolves around the Listings
5-7 and 5-8 in Pro EJB Chapter 5.

Whizlabs is correct. Even I arrived at option C when I read the question.

Pro EJB3.0 book's chapter 5 is MUST READ!

By the way, the code above is using application managed entity manager and not a container managed entity manager.
So JTA does not come into picture.
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Niranjan Deshpande wrote:the code above is using application managed entity manager and not a container managed entity manager.


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
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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!

2. Sample code that uses application managed entity manager with a transaction scoped persistence context: I'm using excepts from the pro EJB3.0 Book-

JPA when used in Java SE environment, the only available entity manager available is the application managed entity manager!

[u][b]Creating application scoped EM in Java SE:[/b][/u]
[code]
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("EmployeeService"); //EmployeeService is the name of the persistence uint in the persistence.xml file
EntityManager em = emf.createEntityManager();
[/code]

[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;

protected void doPost(HttpServletRequest request, HttpServletResponse response)
{
String userId = request.getParameter("user");

// check valid user
EntityManager em = emf.createEntityManager();
try {
User user = em.find(User.class, userId);
if (user == null) {
// return error page
// ...
}
} finally {
em.close();
}

// ...
}
}

[/code]


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;

public void init(int deptId) {
dept = em.find(Department.class, deptId);
}

public void addEmployee(int empId) {
Employee emp = em.find(Employee.class, empId);
dept.getEmployees().add(emp);
emp.setDepartment(dept);
audit.logTransaction(emp.getId(),
"added to department " + dept.getName());
}

// ...
}

[/code]

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
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ralph
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I am confised
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's your confusion about ?
 
author
Posts: 580
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Treimin,

I agree that the question is clever but formulated badly.

Hope it helps,
Reza
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Reza.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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
 
Ranch Hand
Posts: 30
Hibernate Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Clark,

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.



Hope it will help you.
Prateek Singh
 
reply
    Bookmark Topic Watch Topic
  • New Topic