PostgresUpdateServlet.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package hello;
  2. import java.io.IOException;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.Random;
  8. import java.util.concurrent.ThreadLocalRandom;
  9. import javax.annotation.Resource;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import javax.sql.DataSource;
  15. /**
  16. * Database connectivity (with a Servlet-container managed pool) test.
  17. */
  18. @SuppressWarnings("serial")
  19. public class PostgresUpdateServlet extends HttpServlet {
  20. private static final String PARAMETER_QUERIES = "queries";
  21. // Database details.
  22. private static final String DB_QUERY = "SELECT * FROM World WHERE id = ?";
  23. private static final String UPDATE_QUERY = "UPDATE World SET randomNumber = ? WHERE id = ?";
  24. // Database connection pool.
  25. @Resource(name = "jdbc/hello_world")
  26. private DataSource dataSource;
  27. @Override
  28. protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
  29. IOException {
  30. // Reference the data source.
  31. final DataSource source = dataSource;
  32. final int count = Common.normalise(req.getParameter(PARAMETER_QUERIES));
  33. final World[] worlds = new World[count];
  34. final Random random = ThreadLocalRandom.current();
  35. try (Connection conn = source.getConnection();
  36. PreparedStatement statement = conn.prepareStatement(DB_QUERY,
  37. ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
  38. PreparedStatement statement2 = conn.prepareStatement(UPDATE_QUERY)) {
  39. Common.modifySQLConnectionSettings(conn);
  40. // Run the query the number of times requested.
  41. for (int i = 0; i < count; i++) {
  42. final int id = Common.getRandom();
  43. statement.setInt(1, id);
  44. try (ResultSet results = statement.executeQuery()) {
  45. if (results.next()) {
  46. worlds[i] = new World(id, results.getInt("randomNumber"));
  47. // Update row
  48. worlds[i].setRandomNumber(Common.getRandom());
  49. statement2.setInt(1, worlds[i].getRandomNumber());
  50. statement2.setInt(2, id);
  51. // Execute the update statement
  52. statement2.execute();
  53. /*
  54. * Applying batch updates will lead to transaction
  55. * deadlocks. This could not be apparent on local
  56. * testing but will be visible on higher concurrencies
  57. * in the TFB test environment.
  58. */
  59. }
  60. }
  61. }
  62. } catch (SQLException sqlex) {
  63. throw new ServletException(sqlex);
  64. }
  65. // Set content type to JSON
  66. res.setHeader(Common.HEADER_CONTENT_TYPE, Common.CONTENT_TYPE_JSON);
  67. // Write JSON encoded message to the response.
  68. Common.MAPPER.writeValue(res.getOutputStream(), worlds);
  69. }
  70. }