Przykład
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) {
Connection conn = null;
try {
conn = DriverManager.getConnection("url", "użytkownik", "hasło");
// ...
}
catch (SQLException exc) {
LogUtility.log(exc);
}
finally {
if (conn != null) {
try {
conn.close();
}
catch (SQLException exc) {
LogUtility.log(exc);
}
}
}
}
Rozwiązanie
Użyj metod getConnection() dla interfejsu javax.sql.DataSource.
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) {
Connection conn = null;
try {
DataSource ds = getDataSource();
conn = ds.getConnection();
// ...
conn.commit();
}
catch (SQLException exc) {
LogUtility.log(exc);
}
finally {
if (conn != null) {
try {
conn.close();
}
catch (SQLException exc) {
LogUtility.log(exc);
}
}
}
}

private DataSource getDataSource() {
return _source;
}

private transient DataSource _source;