UpdateServlet.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package hello;
  2. import java.io.*;
  3. import java.sql.*;
  4. import java.util.*;
  5. import java.util.concurrent.*;
  6. import javax.annotation.*;
  7. import javax.servlet.*;
  8. import javax.servlet.http.*;
  9. import javax.sql.*;
  10. /**
  11. * Database connectivity (with a Servlet-container managed pool) test.
  12. */
  13. @SuppressWarnings("serial")
  14. public class UpdateServlet extends HttpServlet
  15. {
  16. // Database details.
  17. private static final String DB_QUERY = "SELECT * FROM World WHERE id = ?";
  18. private static final String UPDATE_QUERY = "UPDATE World SET randomNumber = ? WHERE id = ?";
  19. private static final int DB_ROWS = 10000;
  20. // Database connection pool.
  21. @Resource(name="jdbc/hello_world")
  22. private DataSource mysqlDataSource;
  23. @Override
  24. protected void doGet(HttpServletRequest req, HttpServletResponse res)
  25. throws ServletException, IOException
  26. {
  27. // Set content type to JSON
  28. res.setHeader(Common.HEADER_CONTENT_TYPE, Common.CONTENT_TYPE_JSON);
  29. // Reference the data source.
  30. final DataSource source = mysqlDataSource;
  31. // Get the count of queries to run.
  32. int count = 1;
  33. try
  34. {
  35. count = Integer.parseInt(req.getParameter("queries"));
  36. // Bounds check.
  37. if (count > 500)
  38. {
  39. count = 500;
  40. }
  41. if (count < 1)
  42. {
  43. count = 1;
  44. }
  45. }
  46. catch (NumberFormatException nfexc)
  47. {
  48. // Do nothing.
  49. }
  50. // Fetch some rows from the database.
  51. final World[] worlds = new World[count];
  52. final Random random = ThreadLocalRandom.current();
  53. try (Connection conn = source.getConnection())
  54. {
  55. try (PreparedStatement statement = conn.prepareStatement(DB_QUERY,
  56. ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY))
  57. {
  58. // Run the query the number of times requested.
  59. for (int i = 0; i < count; i++)
  60. {
  61. final int id = random.nextInt(DB_ROWS) + 1;
  62. statement.setInt(1, id);
  63. try (ResultSet results = statement.executeQuery())
  64. {
  65. if (results.next())
  66. {
  67. worlds[i] = new World(id, results.getInt("randomNumber"));
  68. // Update row
  69. try (PreparedStatement statement2 = conn.prepareStatement(UPDATE_QUERY))
  70. {
  71. statement2.setInt(1, random.nextInt(DB_ROWS) + 1);
  72. statement2.setInt(2, id);
  73. statement2.execute();
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. catch (SQLException sqlex)
  81. {
  82. System.err.println("SQL Exception: " + sqlex);
  83. }
  84. // Write JSON encoded message to the response.
  85. try
  86. {
  87. Common.MAPPER.writeValue(res.getOutputStream(), worlds);
  88. }
  89. catch (IOException ioe)
  90. {
  91. // do nothing
  92. }
  93. }
  94. }