question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Env, AutoCloseable and abstraction

See original GitHub issue

This 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:

  1. I make it AutoCloseable too, so it is treated as the “resource” and it in turn will close the Env underneath.
  2. 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:closed
  • Created 6 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
benalexaucommented, Jan 2, 2018

Assigning it to a field is fine.

Are we OK to now close this ticket?

0reactions
jbeecommented, Jan 1, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AutoCloseable (Java Platform SE 8 ) - Oracle Help Center
The close() method of an AutoCloseable object is called automatically when exiting a try -with-resources ... All Methods Instance Methods Abstract Methods ...
Read more >
Env - javadoc.io
Env. All Implemented Interfaces: java.lang.AutoCloseable. Direct Known Subclasses: RocksEnv, RocksMemEnv, ... public abstract class Env extends RocksObject.
Read more >
java - Why is AutoCloseable the base interface for Closeable ...
I think it has to be with an enhancement for Closeable superclasses, so that the JVM can auto close them on the new...
Read more >
AutoCloseable, Closeable And Flushable Interfaces In Java
AutoCloseable, Closeable And Flushable Interfaces In Java: There are three interfaces that are quite important to the stream classes. Two are Closeable and ......
Read more >
AutoCloseable - Android Developers
AbstractCursor, This is an abstract cursor class that handles a lot of the common code that all cursors need to deal with and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found