Skip to content

Commit

Permalink
Improve fake function for SQL dead lock servlet
Browse files Browse the repository at this point in the history
  • Loading branch information
k-tamura committed Apr 22, 2017
1 parent 6badb7c commit 74aeaff
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 55 deletions.
18 changes: 18 additions & 0 deletions src/main/java/org/t246osslab/easybuggy/core/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class User {
private String name = null;
private String password = null;
private String secret = null;
private String phone = null;
private String mail = null;
private int loginFailedCount = 0;
private Date lastLoginFailedTime = null;

Expand Down Expand Up @@ -43,6 +45,22 @@ public void setSecret(String secret) {
this.secret = secret;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getMail() {
return mail;
}

public void setMail(String mail) {
this.mail = mail;
}

public int getLoginFailedCount() {
return loginFailedCount;
}
Expand Down
169 changes: 126 additions & 43 deletions src/main/java/org/t246osslab/easybuggy/troubles/DeadlockServlet2.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLTransactionRollbackException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Locale;

import javax.servlet.ServletException;
Expand All @@ -13,10 +16,10 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.t246osslab.easybuggy.core.dao.DBClient;
import org.t246osslab.easybuggy.core.model.User;
import org.t246osslab.easybuggy.core.utils.Closer;
import org.t246osslab.easybuggy.core.utils.HTTPResponseCreator;
import org.t246osslab.easybuggy.core.utils.MessageUtils;
Expand All @@ -31,67 +34,145 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S

Locale locale = req.getLocale();
StringBuilder bodyHtml = new StringBuilder();
String updateResult = "";
ArrayList<User> users = null;
try {
String order = req.getParameter("order");
bodyHtml.append("<form action=\"deadlock2\" method=\"post\">");
bodyHtml.append(MessageUtils.getMsg("msg.select.asc.or.desc", locale));
bodyHtml.append("<br><br>");
bodyHtml.append(MessageUtils.getMsg("label.order", locale) + ": ");
bodyHtml.append("<input type=\"radio\" name=\"order\" value=\"asc\" checked>");
bodyHtml.append(MessageUtils.getMsg("label.asc", locale));
bodyHtml.append("&nbsp; ");
bodyHtml.append("<input type=\"radio\" name=\"order\" value=\"desc\">");
bodyHtml.append(MessageUtils.getMsg("label.desc", locale));
bodyHtml.append("<br><br>");
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.update", locale) + "\">");
bodyHtml.append("<br><br>");

if ("asc".equals(order)) {
String message = updateUsersTable(new String[] {"1", "2"}, locale);
bodyHtml.append(message);
} else if ("desc".equals(order)) {
String message = updateUsersTable(new String[] { "2", "1" }, locale);
bodyHtml.append(message);
}else{
bodyHtml.append(MessageUtils.getMsg("msg.warn.select.asc.or.desc", locale));
bodyHtml.append("<br><br>");
String order = getOrder(req);
if ("POST".equals(req.getMethod())) {
users = new ArrayList<User>();
for (int j = 0;; j++) {
String uid = req.getParameter("uid_" + (j + 1));
if (uid == null) {
break;
}
User user = new User();
user.setUserId(uid);
user.setName(req.getParameter(uid + "_name"));
user.setPhone(req.getParameter(uid + "_phone"));
user.setMail(req.getParameter(uid + "_mail"));
users.add(user);
}
updateResult = updateUsers(users, locale);
} else {
users = selectUsers(order, locale);
}
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.sql.deadlock", locale));
bodyHtml.append("</form>");
createHTMLUserTable(locale, bodyHtml, users, order, updateResult);

} catch (Exception e) {
log.error("Exception occurs: ", e);
bodyHtml.append(MessageUtils.getErrMsg("msg.unknown.exception.occur", new String[]{e.getMessage()}, locale));
bodyHtml.append(
MessageUtils.getErrMsg("msg.unknown.exception.occur", new String[] { e.getMessage() }, locale));
bodyHtml.append(e.getLocalizedMessage());
} finally {
HTTPResponseCreator.createSimpleResponse(req, res, MessageUtils.getMsg("title.update.ordered.recordes", locale), bodyHtml.toString());
HTTPResponseCreator.createSimpleResponse(req, res,
MessageUtils.getMsg("title.xxe", locale), bodyHtml.toString());
}
}

private String getOrder(HttpServletRequest req) {
String order = req.getParameter("order");
if ("asc".equals(order)) {
order = "desc";
} else {
order = "asc";
}
return order;
}

private void createHTMLUserTable(Locale locale, StringBuilder bodyHtml, ArrayList<User> users, String order,
String updateResult) {

bodyHtml.append("<form action=\"deadlock2\" method=\"post\">");
bodyHtml.append(MessageUtils.getMsg("msg.update.users", locale));
bodyHtml.append("<br><br>");
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.update", locale) + "\">");
bodyHtml.append("<br><br>");
bodyHtml.append(
"<table class=\"table table-striped table-bordered table-hover\" style=\"font-size:small;\"><th>");
bodyHtml.append("<a href=\"/deadlock2?order=" + order + "\">" + MessageUtils.getMsg("label.user.id", locale)
+ "</a></th><th>");
bodyHtml.append(MessageUtils.getMsg("label.name", locale) + "</th><th>");
bodyHtml.append(MessageUtils.getMsg("label.phone", locale) + "</th><th>");
bodyHtml.append(MessageUtils.getMsg("label.mail", locale) + "</th>");
int rownum = 1;
for (User user : users) {
bodyHtml.append("<tr><td><input type=\"hidden\" name=\"uid_" + rownum + "\" value=\"" + user.getUserId()
+ "\"></input>" + user.getUserId() + "</td>");
bodyHtml.append("<td><input type=\"text\" name=\"" + user.getUserId() + "_name\" value=\"" + user.getName()
+ "\"></input></td>");
bodyHtml.append("<td><input type=\"text\" name=\"" + user.getUserId() + "_phone\" value=\""
+ user.getPhone() + "\"></input></td>");
bodyHtml.append("<td><input type=\"text\" name=\"" + user.getUserId() + "_mail\" value=\"" + user.getMail()
+ "\"></input></td></tr>");
rownum++;
}
bodyHtml.append("</table>");
bodyHtml.append(updateResult);
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.sql.deadlock", locale));
bodyHtml.append("</form>");
}

private ArrayList<User> selectUsers(String order, Locale locale) {

Statement stmt = null;
Connection conn = null;
ResultSet rs = null;
ArrayList<User> users = new ArrayList<User>();
try {
if (!"asc".equals(order) && !"desc".equals(order)) {
order = "asc";
}

conn = DBClient.getConnection();
conn.setAutoCommit(true);
// conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

stmt = conn.createStatement();
rs = stmt.executeQuery("select * from users where ispublic = 'true' order by id " + order);
while (rs.next()) {
User user = new User();
user.setUserId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setPhone(rs.getString("phone"));
user.setMail(rs.getString("mail"));
users.add(user);
}
} catch (SQLException e) {
log.error("SQLException occurs: ", e);
} catch (Exception e) {
log.error("Exception occurs: ", e);
} finally {
Closer.close(rs);
Closer.close(stmt);
Closer.close(conn);
}
return users;
}

public String updateUsersTable(String[] ids, Locale locale) {
private String updateUsers(ArrayList<User> users, Locale locale) {

PreparedStatement stmt = null;
Connection conn = null;
int executeUpdate = 0;
String resultMessage = "";
try {

conn = DBClient.getConnection();
conn.setAutoCommit(false);
// conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

stmt = conn.prepareStatement("Update users set secret = ? where id = ?");
stmt.setString(1, RandomStringUtils.randomNumeric(10));
stmt.setString(2, ids[0]);
executeUpdate = stmt.executeUpdate();

Thread.sleep(5000);

stmt.setString(1, RandomStringUtils.randomNumeric(10));
stmt.setString(2, ids[1]);
executeUpdate = executeUpdate + stmt.executeUpdate();
stmt = conn.prepareStatement("Update users set name = ?, phone = ?, mail = ? where id = ?");
for (User user : users) {
stmt.setString(1, user.getName());
stmt.setString(2, user.getPhone());
stmt.setString(3, user.getMail());
stmt.setString(4, user.getUserId());
executeUpdate = executeUpdate + stmt.executeUpdate();
Thread.sleep(500);
}
conn.commit();
resultMessage = MessageUtils.getMsg("msg.update.records", new Object[] { executeUpdate }, locale) + "<br><br>";
resultMessage = MessageUtils.getMsg("msg.update.records", new Object[] { executeUpdate }, locale)
+ "<br><br>";

} catch (SQLTransactionRollbackException e) {
resultMessage = MessageUtils.getErrMsg("msg.deadlock.occurs", locale);
Expand All @@ -101,12 +182,14 @@ public String updateUsersTable(String[] ids, Locale locale) {
if ("41000".equals(e.getSQLState())) {
resultMessage = MessageUtils.getErrMsg("msg.deadlock.occurs", locale);
} else {
resultMessage = MessageUtils.getErrMsg("msg.unknown.exception.occur", new String[]{e.getMessage()}, locale);
resultMessage = MessageUtils.getErrMsg("msg.unknown.exception.occur", new String[] { e.getMessage() },
locale);
}
log.error("SQLException occurs: ", e);
rollbak(conn);
} catch (Exception e) {
resultMessage = MessageUtils.getErrMsg("msg.unknown.exception.occur", new String[]{e.getMessage()}, locale);
resultMessage = MessageUtils.getErrMsg("msg.unknown.exception.occur", new String[] { e.getMessage() },
locale);
log.error("Exception occurs: ", e);
rollbak(conn);
} finally {
Expand Down
7 changes: 1 addition & 6 deletions src/main/resources/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ description.test.regular.expression=Please test if an input string matches the r
description.send.mail=You can send a mail to the site administrator.
label.access.time=Access Time
label.available.characters=Available Characters
label.asc=asc
label.attach.file=Attach File
label.calculate=Calculate
label.capitalized.string=Capitalized String
Expand All @@ -21,7 +20,6 @@ label.content=Content
label.current.date=Current Date
label.current.thread.count=Current Thread Count
label.current.time=Current Time
label.desc=desc
label.execution.result=Execution Result:
label.goto.admin.page=Go to admin main page
label.go.to.main=Go to main page
Expand All @@ -43,7 +41,6 @@ label.memory.collection.usage=Collection Usage
label.name=Name
label.numbers=Numbers
label.obelus=/
label.order=Order
label.password=Password
label.phone=Phone
label.ping.url=Ping URL
Expand Down Expand Up @@ -176,13 +173,12 @@ msg.socket.leak.occur=Network socket leak occurs every time you load this page.
msg.unknown.exception.occur=Unknown exception occurs : {0}
msg.update.records=Updated {0} records.
msg.update.users.by.xml=If you upload an XML file of the following format, users can be updated all at once.
msg.select.asc.or.desc=You can update recordes, inserted in a table of RDBMS, in ascending or descending order of ID.
msg.update.users=You can update users information.
msg.select.upload.file=Select file to upload.
msg.thread.leak.occur=Thread leak occurs every time you load this page.
msg.user.not.exist=The user does not exist.
msg.user.already.exist=The user already exists.
msg.valid.json=Valid JSON!
msg.warn.select.asc.or.desc=Please select "asc" or "desc" and click the Update button.
msg.warn.enter.name.and.passwd=Please enter your name and password.
section.change.mail=Change Your Mail
section.change.password=Change Your Password
Expand Down Expand Up @@ -230,7 +226,6 @@ title.timezone.list=Lists of Time Zones
title.truncation.error.page=Decimal Division
title.unrestricted.extension.upload=Convert Gray Scale of Image File
title.unrestricted.size.upload=Reverse Color of Image File
title.update.ordered.recordes=Update Ascending/Descending Records
title.user.list=User List
title.xss.page=Reverse String
title.xee=Batch Registration of Users
Expand Down
7 changes: 1 addition & 6 deletions src/main/resources/messages_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ description.test.regular.expression=\u6b63\u898f\u8868\u73fe <code>^([a-z0-9]+[-
description.send.mail=\u30b5\u30a4\u30c8\u306e\u7ba1\u7406\u8005\u306b\u30e1\u30fc\u30eb\u3092\u9001\u4fe1\u3067\u304d\u307e\u3059\u3002
label.access.time=\u30a2\u30af\u30bb\u30b9\u6642\u523b
label.available.characters=\u5229\u7528\u53ef\u80fd\u306a\u6587\u5b57
label.asc=\u6607\u9806
label.attach.file=\u6dfb\u4ed8\u30d5\u30a1\u30a4\u30eb
label.calculate=\u8a08\u7b97\u3059\u308b
label.capitalized.string=\u5148\u982d\u3092\u5927\u6587\u5b57\u306b\u3057\u305f\u6587\u5b57\u5217
Expand All @@ -21,7 +20,6 @@ label.content=\u672c\u6587
label.current.date=\u73fe\u5728\u65e5\u4ed8
label.current.thread.count=\u73fe\u5728\u306e\u30b9\u30ec\u30c3\u30c9\u6570
label.current.time=\u73fe\u5728\u6642\u523b
label.desc=\u964d\u9806
label.execution.result=\u5b9f\u884c\u7d50\u679c:
label.goto.admin.page=\u7ba1\u7406\u8005\u30e1\u30a4\u30f3\u30da\u30fc\u30b8\u3078
label.go.to.main=\u30e1\u30a4\u30f3\u30da\u30fc\u30b8\u3078
Expand All @@ -43,7 +41,6 @@ label.memory.collection.usage=\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u4f7f\u7528\u
label.name=\u540d\u524d
label.numbers=\u6570\u5b57
label.obelus=\u00f7
label.order=\u9806\u5e8f
label.password=\u30d1\u30b9\u30ef\u30fc\u30c9
label.phone=\u96fb\u8a71\u756a\u53f7
label.ping.url=Ping\u3059\u308bURL
Expand Down Expand Up @@ -176,13 +173,12 @@ msg.socket.leak.occur=\u3053\u306e\u30da\u30fc\u30b8\u3092\u8aad\u307f\u8fbc\u30
msg.unknown.exception.occur=\u4f55\u3089\u304b\u306e\u4f8b\u5916\u304c\u767a\u751f\u3057\u307e\u3057\u305f : {0}
msg.update.records={0}\u4ef6\u66f4\u65b0\u3057\u307e\u3057\u305f\u3002
msg.update.users.by.xml=\u6b21\u306e\u5f62\u5f0f\u306eXML\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u3068\u3001\u30e6\u30fc\u30b6\u30fc\u304c\u4e00\u62ec\u3067\u66f4\u65b0\u3067\u304d\u307e\u3059\u3002
msg.select.asc.or.desc=RDBMS\u306e\u30c6\u30fc\u30d6\u30eb\u3067\u7ba1\u7406\u3055\u308c\u305f\u30ec\u30b3\u30fc\u30c9\u3092\u3001ID\u306e\u300c\u6607\u9806\u300d\u307e\u305f\u306f\u300c\u964d\u9806\u300d\u3067\u9806\u6b21\u66f4\u65b0\u3057\u307e\u3059\u3002
msg.update.users=\u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u3092\u4e00\u62ec\u3067\u66f4\u65b0\u3057\u307e\u3059\u3002
msg.select.upload.file=\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u30d5\u30a1\u30a4\u30eb\u3092\u9078\u629e\u3057\u3066\u4e0b\u3055\u3044\u3002
msg.thread.leak.occur=\u3053\u306e\u30da\u30fc\u30b8\u3092\u8aad\u307f\u8fbc\u3080\u305f\u3073\u306b\u3001\u30b9\u30ec\u30c3\u30c9\u30ea\u30fc\u30af\u304c\u767a\u751f\u3057\u307e\u3059\u3002
msg.user.not.exist=\u30e6\u30fc\u30b6\u30fc\u304c\u5b58\u5728\u3057\u307e\u305b\u3093\u3002
msg.user.already.exist=\u65e2\u306b\u30e6\u30fc\u30b6\u30fc\u304c\u5b58\u5728\u3057\u307e\u3059\u3002
msg.valid.json=\u6b63\u3057\u3044JSON\u6587\u5b57\u5217\u3067\u3059\u3002
msg.warn.select.asc.or.desc=\u300c\u6607\u9806\u300d\u307e\u305f\u306f\u300c\u964d\u9806\u300d\u3092\u9078\u629e\u3057\u3066\u3001\u66f4\u65b0\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u4e0b\u3055\u3044\u3002
msg.warn.enter.name.and.passwd=\u540d\u524d\u3068\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u5165\u529b\u3057\u3066\u4e0b\u3055\u3044\u3002
section.change.mail=\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u5909\u66f4
section.change.password=\u30d1\u30b9\u30ef\u30fc\u30c9\u5909\u66f4
Expand Down Expand Up @@ -230,7 +226,6 @@ title.timezone.list=\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306e\u4e00\u89a7
title.truncation.error.page=\u5c0f\u6570\u306e\u5272\u308a\u7b97
title.unrestricted.extension.upload=\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u306e\u30b0\u30ec\u30fc\u30b9\u30b1\u30fc\u30eb\u5909\u63db
title.unrestricted.size.upload=\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u306e\u8272\u53cd\u8ee2
title.update.ordered.recordes=\u30ec\u30b3\u30fc\u30c9\u306e\u6607\u9806/\u964d\u9806\u66f4\u65b0
title.user.list=\u30e6\u30fc\u30b6\u30fc\u4e00\u89a7
title.xss.page=\u6587\u5b57\u5217\u306e\u9006\u8ee2
title.xee=\u30e6\u30fc\u30b6\u30fc\u306e\u4e00\u62ec\u767b\u9332
Expand Down

0 comments on commit 74aeaff

Please sign in to comment.