Created
December 23, 2020 18:30
-
-
Save vhmolinar/b4184c69f561104c9574b1f0c8f17042 to your computer and use it in GitHub Desktop.
Example of a repository configured to have its connections auto closeable
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
@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