All times are UTC - 5 hours




Post new topic Reply to topic  [ 14 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: About Question enthuware.oce-jpad.v6.2.512 :
PostPosted: Tue Sep 02, 2014 2:55 pm 
Offline

Joined: Tue Sep 02, 2014 2:45 pm
Posts: 1
One of the answers is: Entity s does not exist in the database.
and note below: It is possible that the database row corresponding to entity s is removed by some other process, while s is still being managed by the EntityManager. In this case, there won't be any exception.

However when I write code like this
Code:
public void someMethod() {
   Employee emp = em.find(Employee.class, 1);
   try {
      Thread.sleep(5000);  // during this I remove row from database manually
   } catch (InterruptedException e) {
      e.printStackTrace();
}

em.remove(emp);
}


I get OptimisticLockException on remove. Unless my setup is wrong this note contains the error.

Regards
Jerzy


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.oce-jpad.v6.2.512 :
PostPosted: Tue Sep 02, 2014 7:49 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6625
The implementation is not compliant. As per JPA 2.0 specification section 3.1.1:
Quote:
/**
* Remove the entity instance.
* @param entity
* @throws IllegalArgumentException if the instance is not an
* entity or is a detached entity
* @throws TransactionRequiredException if invoked on a
* container-managed entity manager of type
* PersistenceContextType.TRANSACTION and there is
* no transaction
*/
public void remove(Object entity);


HTH,
Paul.


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.oce-jpad.v6.2.512 :
PostPosted: Sun Mar 19, 2017 4:33 am 
Offline

Joined: Sun Jun 21, 2015 2:56 am
Posts: 18
Hello,

Let's suppose that another session bean with transaction-scoped container-managed entity manager creates the Student s entity while running one of its methods in JTA transaction. Then it calls UniversityBean.removeStudent(s).

Is there no conflict when trying to remove entity already managed by other entityManager (let's say, container-managed entity manager)?

I know that creating new application-managed EntityManager during active JTA transaction synchronizes it with this transaction, but does it mean that persistence context from this transaction is being shared with the new EntityManager in removeStudent() ?


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.oce-jpad.v6.2.512 :
PostPosted: Sun Mar 19, 2017 12:14 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6625
Of course there will be a conflict if you try to manipulate same entity from two different transactions. The result will depend on how you do the locking. One of them will fail while committing or changes of one of them will be lost.

Basically, a persistence Context will be shared across all beans that are participating in the same transaction. It is never shared by beans executing in two different concurrent transactions.

To get more clarity, you should go through Chapter 7 of JPA specification.

HTH,
Paul.


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.oce-jpad.v6.2.512 :
PostPosted: Tue Mar 21, 2017 9:39 am 
Offline

Joined: Sun Jun 21, 2015 2:56 am
Posts: 18
Thank you for your answer.

I assumed the same transaction for both container-managed Entity Manager and application-managed Entity Manager. I will try to formulate the question the other way:

While creating entity manager of any type during transaction it is "connected" (synchronized) with this transaction - does it true that persisted context being existed in current transaction so far is "reused" by application-managed Entity Manager (which, as I read in spec. usually operates on persistence context with extended scope)?


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.oce-jpad.v6.2.512 :
PostPosted: Tue Mar 21, 2017 11:17 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6625
As per Section 7.1 and 7.6.3 - A single persistence context may correspond to one or more JTA entity manager instances (all associated with the same entity manager factory). The persistence context is propagated across the entity manager instances as the JTA transaction is propagated.

Thus, if the transaction is same, then the persistence context will be the same for all those entity managers, irrespective of whether it is an application managed or container managed) that are participating in the same transaction.

As I said before, you will need to go through this chapter of the specification thoroughly to get complete understanding.

HTH,
Paul.


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.oce-jpad.v6.2.512 :
PostPosted: Wed Mar 22, 2017 11:21 am 
Offline

Joined: Sun Jun 21, 2015 2:56 am
Posts: 18
I read the spec. - but some questions still stay in my head.

So when we create application-managed entity manager during JTA transaction, the transaction-scoped persistence context is created? Because as we see in spec. entities are managed until transaction ends (via commit or rollback).


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.oce-jpad.v6.2.512 :
PostPosted: Wed Mar 22, 2017 12:02 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6625
Why do you think so?
Check out section 7.7. It clearly says:
Quote:
When an application-managed entity manager is used, the application interacts directly with the persistence provider's entity manager factory to manage the entity manager lifecycle and to obtain and destroy persistence contexts.
All such application-managed persistence contexts are extended in scope, and can span multiple transactions.

The EntityManagerFactory.createEntityManager method and the EntityManagerclose and isOpen methods are used to manage the lifecycle of an application-managed entity manager and its associated persistence context.

The extended persistence context exists from the point at which the entity manager has been created
using EntityManagerFactory.createEntityManager until the entity manager is closed by means of EntityManager.close.



HTH,
Paul.


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.oce-jpad.v6.2.512 :
PostPosted: Thu Mar 23, 2017 1:35 am 
Offline

Joined: Sun Jun 21, 2015 2:56 am
Posts: 18
In chapter 7.7 we can read:

Quote:
The EntityManager.close method closes an entity manager to release its persistence context and other resources. After calling close, the application must not invoke any further methods on the EntityManager instance except for getTransaction and isOpen, or the IllegalStateException will be thrown. If the close method is invoked when a transaction is active, the persistence context remains managed until the transaction completes.

Last sentence is suggestion for me, that although application-managed entity manager was closed, persistence context still exists until transaction ends. Transaction can end much later, and other beans with transaction-scoped entity managers can be called. So my question is: can we say that application-managed persistent context (for which entity manager ended) is connected to transaction, but is not accessible to other entity managers?

Also I tried the following code:

Code:

    public void beanOperation() {
        Address address = new Address();
        em.persist(address);

        EntityManager em2 = emf.createEntityManager();
        Address addr2 = em.find(Address.class, address.getId());
    }


Assuming stateless session bean with transaction-scoped entity manager, after call to em.persist() transaction-scoped persistent context is created. Then I created app-scoped entity manager (which, by spec. is automatically connected to transaction), but trying to find anything in this entity manager results in null. So in this case persistent context is not shared


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.oce-jpad.v6.2.512 :
PostPosted: Thu Mar 23, 2017 2:35 am 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6625
You have a point. I was wrong in saying that if the transaction is same, then the persistence context will be the same for all those entity managers, irrespective of whether it is an application managed or container managed) that are participating in the same transaction.

It is true only for container managed persistence context. In Section 7.8.2, it says:
Quote:
Exactly how the container maintains the association between persistence context and JTA transaction is not defined.
If a persistence context is already associated with a JTA transaction, the container uses that persistence
context for subsequent invocations within the scope of that transaction, according to the semantics for
persistence context propagation defined in section 7.6.3.


For application managed persistence context, in section 7.7, it says:
Quote:
An extended persistence context obtained from the application-managed entity manager is a stand-alone
persistence context — it is not propagated with the transaction.


So yes, you are right in saying that an application-managed persistent context (for which entity manager ended) is connected to transaction is not accessible to other entity managers.

thanks for the feedback!
Paul.


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Template made by DEVPPL/ThatBigForum