• 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

Transaction Scoped Persistence Contexts

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the end of your method tells you that it's the end of this transaction. Am I right guys?
 
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 72
Scala Monad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 72
Scala Monad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot guys...

this group rocks...

this discussion was really good...like a class room !!
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot guys !!!
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic