Meaningless Drivel is fun!*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Persistence Context scope and type 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 "Persistence Context scope and type" Watch "Persistence Context scope and type" New topic
Author

Persistence Context scope and type

Jun Liu
Greenhorn

Joined: Nov 15, 2005
Posts: 11
I am quite confused by the combination of the bean type and persistence context scope and type. As I know, the following two points are correct:
1. application-managed persistence context is always EXTENDED scope.
2. stateless session bean and message-driven bean can only use TRANSACTION scope persistence context.

The conclusion from these two points is that stateless session bean can not use application-managed persistence context. But I do see such code in EJB3 spec, such as 5.7.1.1 in ejb 3 persistence spec.
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Jun Liu wrote:The conclusion from these two points is that stateless session bean can not use application-managed persistence context. But I do see such code in EJB3 spec, such as 5.7.1.1 in ejb 3 persistence spec.


In that case, you MUST close the persistence context, at the end of a method. (em.close()).
Jun Liu
Greenhorn

Joined: Nov 15, 2005
Posts: 11
So the second item that stateless session bean can only use TRANSACTION scope persistence context is wrong, right? It can uses EXTENDED persistence scope, as long as calling em.close() in every method.
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Jun Liu wrote:So the second item that stateless session bean can only use TRANSACTION scope persistence context is wrong, right? It can uses EXTENDED persistence scope, as long as calling em.close() in every method.


Only application managed entity managers can use the close() method. Container managed entity managers couldn't use that method. Therefore it wont allow you to create container managed entity manager with EXTENDED persistence context.
Karnati Sudhakar
Ranch Hand

Joined: Aug 26, 2008
Posts: 270

Jun Liu wrote: 2. stateless session bean and message-driven bean can only use TRANSACTION scope persistence context.


Please let us know the sources from which you drew this conclusion because even SLSBs can use EXTENDED persistence context.


Regards
Sudhakar
Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
SLSB/ MDB are (method) call specific. It acts on user call & forget who called and user is never sure when calling another method on SLSB, which instance (from pool) will serve.

- Having instance variable is not logical
but still you can have it.

- Same way, with SLSB, EXTENDED is not logical
but may be you can have it by application managed PersistanceUnit (I never tried EXTENDED with SLSB, & I hope there won't be compile / deploy time error)
but you can not have it with Container managed PersistenceContext


Promod kumar
Ranch Hand

Joined: Jun 26, 2006
Posts: 90
Jun Liu wrote
The conclusion from these two points is that stateless session bean can not use application-managed persistence context. But I do see such code in EJB3 spec, such as 5.7.1.1 in ejb 3 persistence spec.


I was under the same impression before reading this thread. It looks like stateless session bean can use application managed persistence context. You can have extended persistence context in these two scenarios
1) Container Manager extended persistence context: This only applies for stateful session bean. Refer to section 5.6.4.2
2) Application managed extended persistence context: A stateless session bean can also use this as shown in examples 5.7.1.1 and 5.7.1.2. Both of them use container-managed transaction demarcation.
5.7.1.1 - In this case em is created in every business method and closed at the end of the method.
5.7.1.2 - In this case em is created in PostConstruct and closed in PreDestroy method.

Stateless session beans can use extended persistence context provided that the persistence context is application managed and not container managed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Persistence Context scope and type
 
Similar Threads
Usage of EntityManagerFactory
container-managed extended persistence context
EJB 3.0 - Container Managed VS Application Managed Persistence
Persitence confusion. Please help.
Passed SCBCD 5.0 :)