H
H
HVV7772020-09-04 12:52:35
Java
HVV777, 2020-09-04 12:52:35

How to properly bind an object to the collection from which it was taken?

In general, the situation is as follows: there is a connection pool manager that has methods to open a connection (get it from a specific pool) and close it (return it to the pool from which the connection was taken). Those. a mechanism is needed that would understand which pool a particular connection belongs to.

The idea is to create a map, in which the key will be some identifier of the connection object, and the value will be the identifier of its parent pool. Well, when issuing a connection, add the corresponding entry to the map, and delete it when closing ...
Actually the question is: will this solution be suitable, if so, how can the object be identified? The hash code is not an option since the connection is mutable.
There is another option to create a wrapper over the connection object with the necessary additional properties, but as for me, this will be a crutch.

---
I tried to make the key of the map Object and add the connection object. Everything is working. I hope this is the right decision.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2020-09-04
@zagayevskiy

I don’t know what kind of connections you have, but this map must be used correctly. And practice shows that if something can be used incorrectly, then this will happen.
I would suggest wrappers over pool and connection.
The pool wrapper has the obtain method, which takes a connection from the pool, creates a wrapper over it, in which the pool itself also puts it.
The connection wrapper has a release method that puts the connection into the pool.
Thus, when working with pools purely through these wrappers, you cannot use it incorrectly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question