سؤال

I'm building a JCA adapter with a custom 'connection' class. In all of the examples I've seen, you're supposed to put a "void close()" in your connection class. Then while using your custom connection class, you have to put a call to close() in a finally block.

I don't really trust the developers to remember to put close in a finally block. Also, I don't have to call "close()" on an EntityManager. The container manages opening and close of the EntityManager automatically.

Is there a way to have the container auto close any connections to my JCA adapter? I've tried overriding finalize(), but it could be a very long time before the JVM decides to cleanup my connections.

Or, if someone knew how to write a plugin for findbugs to identity everywhere the custom connection class was used without close() in a finally block... I'd probably be ok with that.

هل كانت مفيدة؟

المحلول

If you know your workflow, you could have a background thread watching activity on your connections, and close anything after a certain amount time (10s, 30s, 2 hrs, whatever).

You could also override the finalize method, as you mentioned. It's usable, just not guaranteed. But on any reasonably busy server, you will likely be getting enough GC activity to have it close most of your connections in time. The caveat of no guarantee with finalize is simply to accept that if someone kills the process (or it faults or whatever), your finalize won't be called.

Finally, it's straightforward capture where a connection is created by simply creating a new Exception and filling in its stack trace. Keep that exception around with the open connection, and when you detect that you are "forced" to close it (via time, finalize, or whatever), you can simply dump the exception to your log to find the offending "open" line for a connection.

You should tie in to the JCA lifecycle for when the connector is destroyed, or undeployed (i.e. when the module is removed from the server, or the server shuts down). This can tell you what connections were left open the module was being removed.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top