Skip to content

Instantly share code, notes, and snippets.

@vhmolinar
Created December 23, 2020 18:30
Show Gist options
  • Save vhmolinar/b4184c69f561104c9574b1f0c8f17042 to your computer and use it in GitHub Desktop.
Save vhmolinar/b4184c69f561104c9574b1f0c8f17042 to your computer and use it in GitHub Desktop.
Example of a repository configured to have its connections auto closeable
@TransactionScoped
public class Repository {
@Resource(mappedName = "java:jboss/datasources/MyDS")
private DataSource dataSource;
private Connection connection;
@PostConstruct
public void open() {
try {
this.connection = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
throw new WebApplicationException(e.getMessage());
}
}
@PreDestroy
public void close() {
if (this.connection == null) return;
try {
if (!this.connection.isClosed()) {
this.connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
throw new WebApplicationException(e.getMessage());
}
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment