Skip to content

Commit

Permalink
Improve error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
k-tamura committed Dec 27, 2016
1 parent a2d4dd5 commit f644d66
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S

EmbeddedJavaDb2 app = new EmbeddedJavaDb2();
if ("asc".equals(order)) {
String message = app.update(new String[] { "Mark", "James" }, locale);
String message = app.update(new int[] { 1, EmbeddedJavaDb2.MAX_USER_COUNT }, locale);
writer.write(message);
} else if ("desc".equals(order)) {
String message = app.update(new String[] { "James", "Mark" }, locale);
String message = app.update(new int[] { EmbeddedJavaDb2.MAX_USER_COUNT, 1 }, locale);
writer.write(message);
} else {
writer.write(MessageUtils.getMsg("msg.warn.enter.asc.or.desc", locale));
Expand All @@ -79,6 +79,7 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S

class EmbeddedJavaDb2 {

static final int MAX_USER_COUNT = 1000;
static final String dbUrl = ApplicationUtils.getDatabaseURL();
static final String dbDriver = ApplicationUtils.getDatabaseDriver();

Expand All @@ -105,8 +106,9 @@ class EmbeddedJavaDb2 {
stmt.executeUpdate("create table users2 (id int primary key, name varchar(30), password varchar(100))");

// insert rows
stmt.executeUpdate("insert into users2 values (0,'Mark','password')");
stmt.executeUpdate("insert into users2 values (1,'James','pathwood')");
for (int i = 1; i <= MAX_USER_COUNT; i++) {
stmt.executeUpdate("insert into users2 values (" + i + ",'user" + i + "','password')");
}

} catch (SQLException e) {
Logger.error(e);
Expand All @@ -128,7 +130,7 @@ class EmbeddedJavaDb2 {
}
}

public String update(String[] names, Locale locale) {
public String update(int[] ids, Locale locale) {

PreparedStatement stmt = null;
Connection conn = null;
Expand All @@ -144,34 +146,54 @@ public String update(String[] names, Locale locale) {
}
conn = DriverManager.getConnection(dbUrl);
conn.setAutoCommit(false);
// conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

stmt = conn.prepareStatement("Update users2 set password = ? where name = ?");
stmt = conn.prepareStatement("Update users2 set password = ? where id = ?");
stmt.setString(1, UUID.randomUUID().toString());
stmt.setString(2, names[0]);
stmt.setInt(2, ids[0]);
executeUpdate = stmt.executeUpdate();

Thread.sleep(5000);

stmt.setString(1, UUID.randomUUID().toString());
stmt.setString(2, names[1]);
stmt.setInt(2, ids[1]);
executeUpdate = executeUpdate + stmt.executeUpdate();
conn.commit();
message = MessageUtils.getMsg("msg.update.records", new Object[] { executeUpdate }, locale);

} catch (SQLTransactionRollbackException e) {
message = MessageUtils.getMsg("msg.deadlock.occurs", locale);
Logger.error(e);
try {
conn.rollback();
} catch (SQLException e1) {
Logger.error(e1);
if (conn != null) {
try {
conn.rollback();
} catch (SQLException e1) {
Logger.error(e1);
}
}
} catch (SQLException e) {
if ("41000".equals(e.getSQLState())) {
message = MessageUtils.getMsg("msg.deadlock.occurs", locale);
} else {
message = MessageUtils.getMsg("msg.unknown.exception.occur", locale);
}
Logger.error(e);
if (conn != null) {
try {
conn.rollback();
} catch (SQLException e1) {
Logger.error(e1);
}
}
} catch (Exception e) {
message = MessageUtils.getMsg("easybuggy", locale);
Logger.error(e);
try {
conn.rollback();
} catch (SQLException e1) {
Logger.error(e1);
if (conn != null) {
try {
conn.rollback();
} catch (SQLException e1) {
Logger.error(e1);
}
}
} finally {
if (stmt != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws Ser
Constructor<?> cunstructor = cl.getConstructor(new Class[] { cl2 });
cunstructor.newInstance(new Object[] { null });
} catch (Exception e) {
Logger.error(e);
message = MessageUtils.getMsg("msg.info.jvm.not.crash", req.getLocale());
} finally {
res.setCharacterEncoding("UTF-8");
Expand Down

0 comments on commit f644d66

Please sign in to comment.