openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ognjen Blagojevic <ognjen.d.blagoje...@gmail.com>
Subject Re: OpenJPA is a J2EE Container - Architecture
Date Tue, 03 Aug 2010 08:47:47 GMT
On 3.8.2010 0:27, jsloyer wrote:
> I am using WebSphere Application Server.  When a request comes to a servlet,
> the servlet calls my Broker Class.  Inside the broker class I have the
> entity manager and some transaction (below).  I am running into issues where
> multiple transactions are going on at once.  How can I fix this?

You are using Singleton pattern to access entity manager, which means 
that in a multi-threaded environment multiple threads might access the 
same EM at the same time. JPA specification says this is wrong:

"An entity manager may not be shared among multiple concurrently 
executing threads. Entity managers may only be accessed in a 
single-threaded manner."

Since you are using JavaEE AS, look at the resource injection 
capabilities (annotation @PersistenceContext), and use stateless session 
beans instead of singleton (annotation @Stateless).

-Ognjen


>
> Call from the servlet.
> Broker.getInstance().getAllSegments();  //this returns a list of POJO
> objects
>
> Broker
> public class Broker {
> 	 private EntityManagerFactory factory;
> 	 private EntityManager em;
> 	 private static Broker instance = new Broker();
> 	
> 	 public Broker() {
> 	     factory = Persistence.createEntityManagerFactory("myFactory",
> System.getProperties());
> 	     em = factory.createEntityManager();
> 	 }
> 	 public static Broker getInstance() {
> 	     return instance;
> 	 }
> .....
>
> public List<Segment>  getAllSegments() {
> 		 Query q = em.createQuery("SELECT segment FROM Segment segment ORDER BY
> segment.name");
> 		 return (List<Segment>) q.getResultList();
> 	 }


Mime
View raw message