VishalDevgire
VishalDevgire

Reputation: 4268

How to enforce release of resources?

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.

Upvotes: 0

Views: 186

Answers (1)

Onur
Onur

Reputation: 5625

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

Upvotes: 1

Related Questions