users@jpa-spec.java.net

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

From: Christian von Kutzleben <cvkutzleben_at_versant.com>
Date: Fri, 5 Apr 2013 16:05:13 +0200

>
> 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.
>


I disagree with this conclusion, it is best practice to call close() and a
portable application should do so,
however, it is not _sufficient_, the transaction must be rolled
back/committed also.
(Other implementations might rely on a close invocation for clean-up or
other actions;
it's just that typically the database connections are only needed during a
transaction and not for the entire lifetime
of an EntityManager)

So making close optional is wrong, and if it implements AutoCloseable, then
this IMO promises more
behavior then it actually does, which I think is also wrong. So leaving it
as it is might be the best alternative.

Regards,

Christian