You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by jsloyer <js...@gmail.com> on 2010/08/02 02:41:31 UTC

OpenJPA is a J2EE Container - Architecture

Hello,

I am using OpenJPA as my persistence layer in my web application and I am
running into issues with multiple transaction hitting my broker.  My broker
class contains The entity manager and the code the uses the entity manager
to access the DB.  Each of my servlets instantiates the Broker class and
then calls the particular methods it needs to in the Broker class.  I know
this isn't the correct design, can anyone help me out with what the correct
design for a web application.


Thanks!
Jeff
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/OpenJPA-is-a-J2EE-Container-Architecture-tp5362389p5362389.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: OpenJPA is a J2EE Container - Architecture

Posted by Ognjen Blagojevic <og...@gmail.com>.
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();
> 	 }


Re: OpenJPA is a J2EE Container - Architecture

Posted by jsloyer <js...@gmail.com>.
Hello,

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?

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();
	 }
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/OpenJPA-is-a-J2EE-Container-Architecture-tp5362389p5366179.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: OpenJPA is a J2EE Container - Architecture

Posted by Ognjen Blagojevic <og...@gmail.com>.
Hi Jeff,

On 2.8.2010 2:41, jsloyer wrote:
> I am using OpenJPA as my persistence layer in my web application and I am
> running into issues with multiple transaction hitting my broker.  My broker
> class contains The entity manager and the code the uses the entity manager
> to access the DB.  Each of my servlets instantiates the Broker class and
> then calls the particular methods it needs to in the Broker class.  I know
> this isn't the correct design, can anyone help me out with what the correct
> design for a web application.

OpenJPA is Apache implementation of Java persistence (JPA) 
specification. It is not a J2EE/JavaEE container.

You may use it stand-alone, together with JSP/Servlet container like 
Apache Tomcat, or with JavaEE application server like Apache Geronimo. 
How do you use it? How do you manage transactions?

Could you also post relevant parts of your code, and provide more 
details about issues you have with transaction handling? Do you use 
resource injection? How about stateless session beans?

-Ognjen