How do I close the EnityManager when it has not been entered?

I have a servlet running on an Oracle OCCAS server. I am currently mapping some data in the database to an entity class in my application using @Entity annotaion. However, I did not introduce the EntityManager (@PersistenceContext), and as far as I know, this is because it is running in my servlet context and not as a separate Entity EJB. However, creating a manager via the EntityManagerFactory works, so I am using at the moment.

Now, after several restarts of the application, I am getting a PermGen space error. I suppose it has something to do with persistence. There is a call to EntityManager.close () in my finalize method, but it never appears in the log.

Is this really a bad way of doing things - do I need to "have a separate Entity Bean, or how should I clean up the EntityManager?"


source to share

1 answer

I had a similar problem and solved it using ThreadLocal and a servlet filter.

Here is a post on my blog detailing what you need to do ; basically your servlet filter configures the entity manager and then closes it after the servlet call completes; this makes the entity dispatcher available as a thread local variable (as recommended by hibernation). You also need to catch the exceptions in the filter and rollback.

By the way, finalizing is not guaranteed to be calling the way you think. finalize

should be called before the JVM exits, but outside of that it can be long.



All Articles