0

I have a class called Parameter that wraps different values including java.sql.Clob

With JDBC4 specification, we have a free() method that can be called to free resources allocated to Clob object. I am thinking of making Parameter class an AutoClosable and call free() method inside close()

This works out great but i also want something to enforce or at least hint others that Parameter is an instance of AutoClosable

Other than documentation is there a way to achieve this? Basically i am looking for something extra that one can do in this situation.

VishalDevgire
  • 4,232
  • 10
  • 33
  • 59
  • 2
    Who shall give this hint to programmer using `Parameter`? The IDE? The compiler? –  Jun 23 '16 at 12:43
  • [AutoCloseable](https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html) is an interface. What more "hint" do you want other than implement it? – Fildor Jun 23 '16 at 12:49
  • @Lutz Horn I was thinking from IDE point of view. I know my question is absurd but i don't have better way to put it. :) – VishalDevgire Jun 23 '16 at 13:00
  • Do you want to hint or to enforce? You can't prevent a programmer using a `Parameter` in a normal `try`/`catch`(/`finally`) way. There is no way to *forcing* the use of TWR. –  Jun 23 '16 at 13:02
  • 2
    The only way to "enforce" it is to not give up control of the process. That is, your code will open and close the `Parameter` and will accept a callback it can invoke to do everything in between. – biziclop Jun 23 '16 at 13:11
  • @biziclop Great idea. Though i am not sure how i will achieve that with my existing structure. – VishalDevgire Jun 23 '16 at 13:15
  • I remember that at least some compilers give warning when AutoClosable is not closed, at least in somem situations. –  Jun 23 '16 at 13:16

1 Answers1

1

Usual way to do this is implement an ObjectPool with a configurable timeout. Start counting when object taken from ObjectPool and check if it's returned to the pool at the end of timeout. If the Object is not returned to the pool throw an exception or forcibly release the resources that Object is keeping. This way you will force programmer to call close() method or detect leaks.

Most of Database Connection Pooling libraries has this feature. Check JDBCConnectionPool - InactiveConnectionTimeoutSeconds and Apache Commons Pool - AbandonedConfig

Onur
  • 5,617
  • 3
  • 26
  • 35