Skip to content

Instantly share code, notes, and snippets.

@chilloutman
Last active August 29, 2015 14:22
Show Gist options
  • Save chilloutman/ddb74f51be9239c776d9 to your computer and use it in GitHub Desktop.
Save chilloutman/ddb74f51be9239c776d9 to your computer and use it in GitHub Desktop.
Ensuring Closeable is closed in Java/Groovy
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