Questions tagged [autocloseable]

java.lang.AutoCloseable is an interface which, when implemented by a class, can be used in the try-with-resources Statement. It is ensured that resource/class object is closed at the end of the statement.

The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

Resources:

127 questions
9
votes
1 answer

Is using a lambda a safe, correct, and equivalent workaround for classes that do not implement AutoCloseable?

Background: I use the Java class InitialDirContext to access LDAP directories. Unfortunately, it does not implement interface AutoCloseable, so it cannot be used in try-with-resources blocks. Here is the original code I wrote: (inspired by this…
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
9
votes
2 answers

Try-with-resources close order

I am looking at examples of try-with-resources in Java and I understand the following one: try (Connection conn = DriverManager.getConnection(url, user, pwd); Statement stmt = conn.createStatement(); ResultSet rs =…
Aliuk
  • 1,249
  • 2
  • 17
  • 32
9
votes
1 answer

Do I risk a JDBC connection leak when streaming JOOQ results outside a try-with-resources block?

I have a JOOQ query where I want to avoid materializing all Records at the same time. (However, I am fine with jointly materializing all bean objects created from them.) I have the following simple method to load the data: public List
blubb
  • 9,510
  • 3
  • 40
  • 82
9
votes
1 answer

How to handle exceptions that occur in let bindings or body

How does one deal with exceptions that could occur in the bindings or body of a let statement using the same finally block? Ex: (let [connections (create-connections)] (dostuff) (close connections)) If (create-connections) or (dostuff) fails,…
Josh
  • 875
  • 1
  • 13
  • 25
8
votes
5 answers

Using "try with resources" for resources created without any reference

Consider the following situation : try (ResultSet resultSet = DriverManager.getConnection("jdbc:...", "user", "pass") .createStatement().executeQuery(sql)) { . . …
Mohsen R. Agdam
  • 364
  • 3
  • 12
8
votes
3 answers

automatically closing a resource passed as an argument

If I want to automatically close a resource passed as an argument, is there a more elegant solution than this? void doSomething(OutputStream out) { try (OutputStream closeable = out) { // do something with the OutputStream } } Ideally, I'd…
Dónal
  • 185,044
  • 174
  • 569
  • 824
7
votes
3 answers

How should a closed resource behave in java?

I have a java class that holds a Closeable resource internally. This means my class also implements the Closeable interface and closes the internal resource in its own close() method. What is the expected behavior of my class after it has been…
Gyorgy Szekely
  • 2,654
  • 3
  • 23
  • 32
6
votes
3 answers

Close a dynamic number of AutoCloseable objects in try-with-resources

I am creating a variable amount of AutoCloseable objects in a try-with-resources block. At any point of exit, I want all of the allocated resources closed. I can imagine writing something myself to do this, but is there an existing utility similar…
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
6
votes
3 answers

Is disposing SXSSFWorkbook necessary when used in try with resource

Below is the sample code snippet to create SXSSFWorkbook: try(SXSSFWorkbook wb = new SXSSFWorkbook()) { //... } finally { wb.dispose(); //wb not accessible over here, so can't use try with resource } Here problem is that if I use try with…
eatSleepCode
  • 4,427
  • 7
  • 44
  • 93
6
votes
1 answer

Why does Apache CloseableHttpResponse not consume the entity on close?

Looking at the quick start guide it gives the following code example: CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://targethost/homepage"); CloseableHttpResponse response1 =…
james
  • 1,035
  • 2
  • 14
  • 33
6
votes
1 answer

How can I find all instances and objects in my code that are not closed?

My team has quite a huge amount of code. Recently I have found some objects that weren't closed properly. How can i find all the instances that are not closed or not inside a try-with-resources block? Some objects like Statement, ResultSet are not…
Hard Worker
  • 995
  • 11
  • 33
6
votes
3 answers

My own solution for Kotlin's try-with-resources absence

Kotlin provides the use function for Closeable objects, but it seems they forgot to consider AutoCloseable (e.g. DB prepared statements) for the try-with-resources full Java equivalent. I've implemented the next "home-made" solution: inline fun…
Mario
  • 1,661
  • 13
  • 22
5
votes
1 answer

How to replace the deprecated finalize() method from a Java 11 project with inter - dependencies among classes

I have a Java 11 project involving multiple classes. In the present scenario, 2 of my classes - A and B - implement the java finalize() method, which is now deprecated for good. I understand the method may not be removed in the near future but I…
5
votes
2 answers

Is there a correct way to close resources opened in java stream api (for each element)?

Is there a correct way to open a resource for each element in collection, than use stream api, do some map(), filter(), peek() etc. using the resource and than close the resource? I have something like this: List names = …
user3913960
5
votes
1 answer

Eclipse inconsistencies: Resource leak: '' is never closed

If I have the following code: public OutputStream test(boolean condition) throws FileNotFoundException { return condition ? null : new FileOutputStream("test.txt"); } Eclipse puts yellow squiggles under new FileOutputStream("test.txt") and…
Markus A.
  • 12,349
  • 8
  • 52
  • 116
1
2
3
8 9