Skip to content

Commit

Permalink
Add a servlet which can cause DB connection leaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
k-tamura committed Dec 27, 2016
1 parent 4882058 commit f6e9f01
Showing 1 changed file with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.t246osslab.easybuggy.troubles;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.pmw.tinylog.Logger;
import org.t246osslab.easybuggy.utils.ApplicationUtils;
import org.t246osslab.easybuggy.utils.Closer;
import org.t246osslab.easybuggy.utils.MessageUtils;

@SuppressWarnings("serial")
@WebServlet(urlPatterns = { "/dbconnectionleak" })
public class DBConnectionLeakServlet extends HttpServlet {

static final String dbUrl = ApplicationUtils.getDatabaseURL();
static final String dbDriver = ApplicationUtils.getDatabaseDriver();
boolean isLoad = false;

protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

PrintWriter writer = null;
Connection conn = null;
Statement stmt = null;
Locale locale = req.getLocale();
try {
res.setCharacterEncoding("UTF-8");
res.setContentType("text/plain");
writer = res.getWriter();

if (dbDriver != null && !dbDriver.equals("")) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
Logger.error(e);
}
}
conn = DriverManager.getConnection(dbUrl);
stmt = conn.createStatement();
if (!isLoad) {
isLoad = true;
try {
stmt.executeUpdate("drop table users3");
} catch (SQLException e) {
// ignore exception if exist the table
}
// create and insert users table
stmt.executeUpdate("create table users3 (id int primary key, name varchar(30), password varchar(100))");
}
stmt.executeUpdate("insert into users3 select count(*)+1, 'name', 'password' from users3");
writer.println(MessageUtils.getMsg("msg.db.connection.leak.occur", locale));

} catch (SQLException e) {
Logger.error(e);
writer.println(MessageUtils.getMsg("msg.unknown.exception.occur", locale));
writer.println(e.getLocalizedMessage());
} finally {
Closer.close(writer);
/* A DB connection leaks because the following lines are commented out.
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
Logger.error(e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
Logger.error(e);
}
}
*/
}
}
}

0 comments on commit f6e9f01

Please sign in to comment.