Skip to main content

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

  • From: Christian von Kutzleben <cvkutzleben@...>
  • To: users@...
  • Subject: [jpa-spec users] Re: Why no AutoCloseable for EntityManager?
  • Date: Thu, 4 Apr 2013 14:52:35 +0200

Hi,

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

- EntityManager.close() has a special semantics, if a transaction is active, it does not free resources then
- our implementation keeps resources while a transaction is active, so having a "transaction-scope" with
a try could make sense*, but for "close" it does not bring us any advantage (might be different for other vendors)

* also see the .NET System.Transactions API with using and the TransactionScope

Regards,

Christian

On Thu, Apr 4, 2013 at 2:25 PM, <jonty_lawson@...> wrote:
Sorry to insist about this, and I know it's very late in the day for
this version, but unless I've failed to see something this does look
like a stupid oversight that is very easily fixed.

EntityManager contains a close() method which has to be called on
anything returned by emf.createEntityManager().
If the words "implements AutoCloseable" were added to the declaration
of interface EntityManager then, without making any other changes, the
try-with-resources statement could be used to ensure closure in all
circumstances, thus:

try (EntityManager em = emf.createEntityManager()) {
  // use em
}

This is now the preferred idiom for anything that requires a close
call.

The only down side I can see to this change is that it would tie JPA to
java 1.7 or later, since java.lang.AutoCloseable was only introduced in
this version, but that doesn't seem much of a problem.

Is there a good reason this hasn't been done?



--
Christian von Kutzleben
Chief Engineer | Versant GmbH
(T) +49 40 60990-0
(F) +49 40 60990-113
(E) cvkutzleben@...
www.versant.com | www.db4o.com

--
Versant
GmbH is incorporated in Germany. Company registration number: HRB
54723, Amtsgericht Hamburg. Registered Office: Halenreie 42, 22359
Hamburg, Germany. Geschäftsführer: Bernhard Wöbker, Volker John

CONFIDENTIALITY
NOTICE: This e-mail message, including any attachments, is for the sole
use of the intended recipient(s) and may contain confidential or
proprietary information. Any unauthorized review, use, disclosure or
distribution is prohibited. If you are not the intended recipient,
immediately contact the sender by reply e-mail and destroy all copies of
the original message.


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

jonty_lawson 04/04/2013

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

Christian von Kutzleben 04/04/2013

<Possible follow-up(s)>

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

jonty_lawson 04/05/2013

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

Christian von Kutzleben 04/05/2013
 
 
Close
loading
Please Confirm
Close