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é