aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Transaction Scoped Persistence Contexts 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 "Transaction Scoped Persistence Contexts" Watch "Transaction Scoped Persistence Contexts" New topic
Author

Transaction Scoped Persistence Contexts

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
I read this in Oreilly book -

Transaction scoped Persistence Contexts are limited to the scope of a JTA transaction.

My question is, what is the thing in the code, that will tell me that this code is JTA scoped?



Here Customer is a entity bean. The change made by calling the setName( ) method will be synchronized with the database when the JTA transaction completes and commits.

The annotation marks the start
of a JTA transaction. My question is, what marks the end of the above JTA method transaction. Is there anything in code that marks the end of the JTA
transaction method? How does the container know ?


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.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9499
    
    2

I guess the end of your method tells you that it's the end of this transaction. Am I right guys?


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

The annotation

code:

@TransactionAttribute(REQUIRED)

marks the start
of a JTA transaction. My question is, what marks the end of the above JTA method transaction. Is there anything in code that marks the end of the JTA
transaction method? How does the container know ?


As the PersistenceContextType.EXTENDED is not used (For stateful bean only) when defining the persistence context the persistence context is transaction scope.
@TransactionAttribute(REQUIRED) or no attribute does not confirm that the transaction will be start before the method invocation. The JTA transaction may be started from the caller code and the Required attribute propagate the caller transaction.

If there is no caller transaction, the transaction start before the method is invoked and ends when the method completes.

If the transaction is propagated from the caller, the transaction completes in the caller method only. The called method does not commit the transaction.

Hope this help .

Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Marimuthu Madasamy
Ranch Hand

Joined: Jun 07, 2007
Posts: 72

Transaction Scoped Persistence context starts and ends within the scope of a business method. Extended persistence context (only for SFSB) manages a transaction across method invocations. Here the REQUIRED transaction attribute specfies that the method has to be executed in a transaction. It doesnt mark the start of a transaction.


- Marimuthu Madasamy
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi

Originally posted by Marimuthu Madasamy:
Transaction Scoped Persistence context starts and ends within the scope of a business method.


I do not agree with above statement.

All container managed transaction scoped entity managers depend on JTA transactions. The reason for this is because they can use the transaction as a way to track persistence contexts. Every time an operation is invoked on the entity manager, it checks to see if a persistence context is associated with the transaction.

If it finds one, the entity manager will use this persistence context. If it doesn�t find one, then it creates a new persistence context and associates it with the transaction. When the transaction ends, the persistence context goes away.

Thanks
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
Thank a lot for the inputs, the lessons that I learnt are -

Muthu's post -

Here the REQUIRED transaction attribute specfies that the method has to be executed in a transaction.


Narendra'a post -

All container managed transaction scoped entity managers depend on JTA transactions. The reason for this is because they can use the transaction as a way to track persistence contexts. Every time an operation is invoked on the entity manager, it checks to see if a persistence context is associated with the transaction.

If it finds one, the entity manager will use this persistence context. If it doesn�t find one, then it creates a new persistence context and associates it with the transaction. When the transaction ends, the persistence context goes away.


Ok now ?
[ February 01, 2008: Message edited by: Niranjan Deshpande ]
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
One more thing to confirm -



The above refers a Transaction scoped PersistenceContext/entityManager serice. All entities operated using the above entityManager object, will be transaction scoped.



The above refers a Extended PersistenceContext/entityManager service. All entities operated using the above entityManager object, will be Extended in scope.

So the start/end of the java methods is insignificant marking the start/end of the JTA.Then what for @TransactionAttribute(REQUIRED) is needed?
Marimuthu Madasamy
Ranch Hand

Joined: Jun 07, 2007
Posts: 72

Thanks Narendra. More to add,

From EJB 3 Oreilly, P-no - 373
When a transaction-scoped entity manager is invoked outside the scope of a transaction, it creates a persistence context for the duration of that method call. After the method call completes, any managed objects produced by the call are immediately detached.

so duration of the method is significant.

REQUIRED attribute ensures that your business method is part of an existing transaction or executing within a new transaction. In case of new transaction, Once the method completes, the scope of the new transaction ends.
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
thanks a lot guys...

this group rocks...

this discussion was really good...like a class room !!
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

Niranjan,


is not way to declare persistence context. One way is,



This type of persistence context is applicable only for stateful session bean

Marimuthu,

These are the quotes from EJB specifications

The application may obtain a container-managed entity manager with transaction-scoped persistence
context bound to the JTA transaction by injection or direct lookup in the JNDI namespace. The persistence
context type for the entity manager is defaulted or defined as PersistenceContext-
Type.TRANSACTION.
A new persistence context begins when the container-managed entity manager is invoked[36] in the
scope of an active JTA transaction, and there is no current persistence context already associated with
the JTA transaction. The persistence context is created and then associated with the JTA transaction.
The persistence context ends when the associated JTA transaction commits or rolls back, and all entities
that were managed by the EntityManager become detached.
If the entity manager is invoked outside the scope of a transaction, any entities loaded from the database
will immediately become detached at the end of the method call.


This clears that the transaction scoped persistence context is bound to JTA transaction. I case of REQUIRED attribute, and the transaction is propagated from the caller code, the scope of transaction will not complete at the end of method call. In this case the duration of caller's method is significant, as when the persistence context is bound to transaction either is caller method or called method, the persistence context is valid in both the methods after the point when the persistence context is created.

Hope that I am clear about it. Please comment.

Thanks
eric khoa
Greenhorn

Joined: Sep 22, 2005
Posts: 10
I think JTA or Local resource for Persistence Context is determine at the time we create PU in persistence.xml.

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="manager1" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/DefaultDS</jta-data-source>
</persistence-unit>
</persistence>

If we use @PersistenceContext successfully then it mean we declared Container Manage Entity Manager with JTA transaction.

extract from EJB 3 Persistence API (Apress)

When the transaction-scoped entity manager in stateless bean. 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. The reason for this is because they can use the transaction as a way to track persistence contexts. Every time an operation is invoked on the entity manager, it checks to see if a persistence context is associated with the transaction. If it finds one, the entity manager will use this persistence context. If it doesn�t find one, then it creates a new persistence context and associates it with the transaction. When the transaction ends, the persistence context goes away.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

That's right. If do not specify the transaction-type, it defaulted to JTA transaction. The Resource Local trasaction management used only with EntityTransaction API.

Thanks
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
thanks a lot guys !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transaction Scoped Persistence Contexts