Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JPA 2.0 non-jta-data-source issue(EntityManager behaviour)
icon8.gif  JPA 2.0 non-jta-data-source issue [message #503226] Fri, 11 December 2009 08:21 Go to next message
No real name is currently offline No real name
Messages: 4
Registered: December 2009
Junior Member
Hi,

I'm building a web shop on the 6th platform of the J2EE and I'm using JPA 2.0 with a non-jta-data-source.
Reading and writing data to one of the entities is done with the help of two singleton beans, as such:



@Singleton
public class ReadInformationBean {
@PersistenceUnit(unitName="BShopPU")
EntityManagerFactory entityManagerFactory;
EntityManager entityManager;

@PostConstruct
void init() {
entityManager = entityManagerFactory.createEntityManager()
}
....
public Shopper getShopper(int shopperId) {...}
....
}

@Singleton
public class WriteInformationBean {
@PersistenceUnit(unitName="BShopPU")
EntityManagerFactory entityManagerFactory;
EntityManager entityManager;

@PostConstruct
void init() {
entityManager = entityManagerFactory.createEntityManager()
}
....
public void setShopperInformation(...) {...}
}



When I use the WriteInformationBean to write information to he database, the information is written in the database, but that information is not available for reading through ReadInformationBean.
The application behaves exactly as if there are two different persistence units even thought the persistence unit is clearly the same in both ejbs.
There is no possibility for the two ejbs to share the same entity manager, so this must an EclipseLink bug, right?
I forgot to mention that for a jta-data-source the code works perfectly fine.
Re: JPA 2.0 non-jta-data-source issue [message #503774 is a reply to message #503226] Tue, 15 December 2009 12:14 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

I'm not familiar with the 6th Platform, but I assume the issue is how you have configured things.

Since each bean has its own non-managed EntityManager, each EntityManager will have its own persistence context and transaction context. They will only be able to see each others changes after they commit (and possible refresh for updated objects as they may be already in the persistence context).

Are you calling commit()? If so, then how have you configure your persistence unit, are you using JTA or RESOURCE_LOCAL. If you are using JTA, you need to set the target-server in EclipseLink to integrate with your server's JTA implementation. You would also need to join a JTA transaction because you are using an application managed EntityManager.

In JEE the EntityManager is normally injected using @PersistenceContext and is JTA managed and accessed from SessionBean methods. You don't seem to be using JEE, so probably need to be managing your EntityManagers and transactions directly. Having singletons each with their own EntityManager seems to be a bad design, you should most likely hold an EntityManagerFactory and create and close an EntityManager per request, or per transaction.


James : Wiki : Book : Blog : Twitter
Re: JPA 2.0 non-jta-data-source issue [message #504096 is a reply to message #503774] Wed, 16 December 2009 23:00 Go to previous message
No real name is currently offline No real name
Messages: 4
Registered: December 2009
Junior Member
Hello James,

Thank you for your reply.

We are discussing the situation where each Singleton has its own EntityManager. You think it seems to be a bad design, I don't agree, but for the sake of the discussion lets stick to the point:

For a JTA data-source, all the entity managers operate over the same Persistence Context.
For a RESOURCE_LOCAL data-source, Eclipse-Link provides each entityManager with each its own persistence unit.

Sun Microsystem's official specification of JPA 2.0 : JSR 317 does not clearly specify that for a RESOURCE_LOCAL data-source, each entity manage must have its own persistence unit, nor does it clearly specify that all the entity managers should use the same persistence unit.

Now lets look at the persistence.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" ...>
<persistence-unit name="BShopPU1" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider >
<non-jta-data-source>...</non-jta-data-source>
</persistence-unit>
</persistence>

Isn't it common sense that all RESOURCE_LOCAL entity managers operate over the same persistence unit?
Why should one create and close an EntityManager per request, or per transaction? Why the extra number of objects and operations?

J2EE version 6 has the official status of final draft, and it is also up to its providers to determine its specifications, just as J2EE 5's JPA 1.0 behaved differently depending on its provider.

Additionally to this discussion I found a bug in EclipseLink's implementation of JPA 2.0 API. Is this forum for bug report or can you point me to the official bug report page?
Previous Topic:How to implement preUpdate from DescriptorEventAdapter?
Next Topic:keeping a history: who done it?
Goto Forum:
  


Current Time: Wed Apr 16 12:11:36 EDT 2014

Powered by FUDForum. Page generated in 0.07008 seconds