-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Using EntityManager the right way in a Tomcat Servlet
PostPosted: Fri Nov 10, 2006 9:42 am 
Newbie

Joined: Wed Aug 18, 2004 10:44 am
Posts: 14
Hibernate version:
3.2

As far as I know, when using the entitymanager in Tomcat you must look up how to do it in an J2SE environment.

Im trying to use the persistence API, entity manager and annotations. My servlet code looks like this:

Code:
public class Admin extends HttpServlet {

private static final long serialVersionUID = 1L;


@SuppressWarnings("unchecked")
protected void doGet( HttpServletRequest request,
      HttpServletResponse response )
                                throws ServletException, IOException {
  // The output is a html-page.
  response.setContentType( "text/html" );
  PrintWriter out = response.getWriter(); 
  out.print("...");
  EntityManagerFactory emf =  Persistence.createEntityManagerFactory("manager1");
  EntityManager em = emf.createEntityManager();
  try {
         EntityTransaction tx = em.getTransaction();
         tx.begin();
         // create new entity
         Institution inst = new Institution();
         inst.setNavn(nyInstNavn);
         em.persist(inst);
         tx.commit();
         out.print("<p>Added new:"+nyInstNavn+"</p>");
   } catch (Exception e) {
      out.print("<p><b>WOW</b> exception</p>");
   } finally {
     em.close();
   }
}


The servlet runs fine - until the servlet is ran a 100 times, then an exception is thrown:

Code:
javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Cannot open connection
   at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:647)
   at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:40)
   at com.hypergenic.weboverblik.servlets.Admin.doGet(Admin.java:76)
...
Caused by: org.hibernate.exception.JDBCConnectionException: Cannot open connection
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
   at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420)
   at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)


It seems like all the connections to the database are in use. But the entitymanager and the entitytransaction are both created in the doGet() method. This should ensure cleanup of the connections, right?

Regards,
René


Top
 Profile  
 
 Post subject: Found what seems to be the problem
PostPosted: Mon Nov 13, 2006 6:30 am 
Newbie

Joined: Wed Aug 18, 2004 10:44 am
Posts: 14
Moved EntityManagerFactory into a static @PersistenceUnit

Code:
@PersistenceUnit EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1");
   


According to:

https://blueprints.dev.java.net/bpcatal ... lyapp.html

I'm running a "Application-Managed Entity Managers"

Regards,
Rene


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 13, 2006 8:04 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Please check the configuration of your datasource / database in the persistence.xml. Somehow you can't get to the database

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 13, 2006 9:07 am 
Newbie

Joined: Wed Aug 18, 2004 10:44 am
Posts: 14
Sorry, I forgot to mention in my previous post, that moving Moved EntityManagerFactory into a static @PersistenceUnit fixed the problem for me.

Just curius: It seems like not many people are using the Java Persistence API in a Servlet-environment. Or am I wrong here?

Regards,
René


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 13, 2006 9:25 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
It is absolutely fine for you to use JPA in a servlet environment.

_________________
Please don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


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

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.