aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes getTransaction() on transaction-scoped persistence context. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "getTransaction() on transaction-scoped persistence context." Watch "getTransaction() on transaction-scoped persistence context." New topic
Author

getTransaction() on transaction-scoped persistence context.

Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
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?
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
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.


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
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

Joined: Oct 16, 2005
Posts: 1277
[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

Joined: Nov 12, 2008
Posts: 757
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.

Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
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.

SCJP 5 (98%) - SCBCD 5 (98%)
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Thanks Ralph
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
Now I am confised
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
What's your confusion about ?
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Treimin,

I agree that the question is clever but formulated badly.

Hope it helps,
Reza


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Thanks Reza.
Shenbo Lu
Greenhorn

Joined: May 12, 2010
Posts: 1
[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
Prateek Kumar Singh
Greenhorn

Joined: Jan 21, 2011
Posts: 29

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getTransaction() on transaction-scoped persistence context.