Skip to main content

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

  • From: <jonty_lawson@...>
  • To: users@...
  • Subject: [jpa-spec users] Re: Why no AutoCloseable for EntityManager?
  • Date: Thu, 4 Apr 2013 12:25:06 +0000 (GMT)

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?


[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