• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Persistence Context scope and type

 
Jun Liu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepika Joshi
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic