Last active
August 29, 2015 14:22
-
-
Save chilloutman/ddb74f51be9239c776d9 to your computer and use it in GitHub Desktop.
Ensuring Closeable is closed in Java/Groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Database { | |
public void useConnection (Consumer consumer) { | |
try (def connection : newConnection()) { | |
consumer.accept(connection); | |
} | |
} | |
private Connection newConnection () { /*...*/ }; | |
} | |
// Use: | |
database.useConnection { c -> | |
c.executeQuery(...); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment