users@jpa-spec.java.net

[jpa-spec users] Re: Why no AutoCloseable for EntityManager?

From: <jonty_lawson_at_yahoo.co.uk>
Date: Fri, 5 Apr 2013 13:37:32 +0000 (GMT)

> Just my 2 cents regarding this, why I don't see this as useful:

Wow. So are you saying that calling close() on the EM is not important
(at least for your implementation)?

If this is the case in general then you are right and adding
AutoCloseable to EntityManager would be a mistake. The javadoc for
AutoCloseable states: "A resource that must be closed when it is no
longer needed", and if this doesn't apply to EM instances then it's
appropriate that it not be implemented.

However, good citizen that I am, I had assumed that it was important
that the close method be called on instances of EM, and that use of a
try/finally was a good way of achieving this.

This belief was fostered by:

1) The general usage of close methods within the java libraries.
     Usually these require that close be called to free system
resources
     and it is effectively a programming error to fail to do so (eg in
java.io,
     java.net, java.sql, java.nio, javax.management and elsewhere).

2) The examples in the JPA spec that show close being called on an EM
     in a number of places.

Admittedly the JPA spec is silent on the *necessity* of calling close
on application-managed EMs.

I summary, I would say that either the JPA spec should be updated to
state that calling close on an EM is optional (and explain when it
should be called as is done for, eg, javax.naming.NamingEnumeration),
or it should be changed to indicate that it is necessary and EM should
implement AutoCloseable.