-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a servlet which can cause DB connection leaks.
- Loading branch information
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/main/java/org/t246osslab/easybuggy/troubles/DBConnectionLeakServlet.java
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
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); | ||
} | ||
} | ||
*/ | ||
} | ||
} | ||
} |