Env, AutoCloseable and abstraction
See original GitHub issueThis is somewhere between feedback and a question. Or I just miss something.
Currently creating and using an Env
requires proper error handling via try-resource (AutoCloseable
).
At the same time I would like to hide the details of “the database” so I have a data access abstraction.
It is my understanding that the Env
is typically opened at application startup and closed on application shutdown. So the resource is used roughly for the lifetime of the application.
I see two ways to have my abstraction:
- I make it
AutoCloseable
too, so it is treated as the “resource” and it in turn will close theEnv
underneath. - I don’t use
Env
for the lifetime of the application but create it when needed. This basically means per request, what feels wrong, but who knows…
So my question is: Is there any way Env
does not need to be a resource that requires AutoCloseable
? Because this cascades upwards trough the application right up to the “main” what feels kind of ugly.
Maybe it is “good enough” to have a “destructor” cleanup what needs to be cleaned for Env
?
Everyone who has a reference to Env
could continue to close it explicitly but if not Env
would take care of itself? Is something like that possible?
Or is it reasonable to start with creating an Env
for each request?
Or is there another way to handle Env
that does not cascade/leaks AutoCloseable
beyond abstractions?
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (7 by maintainers)
Top GitHub Comments
Are we OK to now close this ticket?
The “trick” seams to be to not assign a
AutoCloseable
to a local variable (without a try-with-resource). That would give you a warning. Assigning it to a field is fine.