Browse Source

[ci fw-only Java/servlet] Use autocommit=true for the SQL DB interractions (#4559)

* Add setting of read-only and autocommit for the JDBC connection

* Set readonly for JDBC connection
Radoslav Petrov 6 years ago
parent
commit
f7e2a927cb

+ 15 - 2
frameworks/Java/servlet/src/main/java/hello/Common.java

@@ -7,6 +7,7 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ThreadLocalRandom;
 
 import com.fasterxml.jackson.core.JsonGenerator;
 import com.fasterxml.jackson.databind.*;
@@ -17,12 +18,13 @@ import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
  * Some common functionality and constants used by the Servlet tests.
  */
 public class Common {
+	private static final int DB_ROWS = 10000;
 	// Constants for setting the content type.
 	protected static final String HEADER_CONTENT_TYPE = "Content-Type";
 	protected static final String CONTENT_TYPE_JSON = "application/json";
 	protected static final String CONTENT_TYPE_TEXT = "text/plain";
 	protected static final String CONTENT_TYPE_HTML = "text/html";
-
+	
 	// Jackson encoder, reused for each response.
 	protected static final ObjectMapper MAPPER = new ObjectMapper();
 	// Jackson encoder with AfterBurner module
@@ -53,6 +55,7 @@ public class Common {
 		}
 	}
 	
+	// Request parameter checking and normalisation
 	public static int normalise(String param) {
 		int count = 1;
 		try {
@@ -68,6 +71,7 @@ public class Common {
 		return count;
 	}
 	
+	// Preload all the data for the cache query test
 	public static Map<Integer, CachedWorld> loadAll(Connection connection) throws SQLException{
 		// Fetch all rows from the database.
 		final Map<Integer, CachedWorld> worlds = new HashMap<Integer, CachedWorld>();
@@ -82,4 +86,13 @@ public class Common {
 		}
 		return worlds;
 	}
-}
+	
+	// Modify the SQL connection settings
+	public static void modifySQLConnectionSettings(Connection connection) throws SQLException {
+		connection.setAutoCommit(true);
+	}
+	
+	public static int getRandom() {
+		return 1 + ThreadLocalRandom.current().nextInt(DB_ROWS);
+	}
+}

+ 2 - 5
frameworks/Java/servlet/src/main/java/hello/DbSQLServlet.java

@@ -23,8 +23,6 @@ import javax.sql.DataSource;
 public class DbSQLServlet extends HttpServlet {
 	// Database details.
 	private static final String DB_QUERY = "SELECT * FROM World WHERE id = ?";
-	private static final int DB_ROWS = 10000;
-
 	// Database connection pool.
 	@Resource(name = "jdbc/hello_world")
 	private DataSource dataSource;
@@ -32,15 +30,14 @@ public class DbSQLServlet extends HttpServlet {
 	@Override
 	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
 			IOException {
-		// Reference the data source.
-		final Random random = ThreadLocalRandom.current();
 		World world = null;
 
 		// Fetch some rows from the database.
 		try (Connection conn = dataSource.getConnection()) {
+			Common.modifySQLConnectionSettings(conn);
 			try (PreparedStatement statement = conn.prepareStatement(DB_QUERY,
 					ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
-				statement.setInt(1, random.nextInt(DB_ROWS) + 1);
+				statement.setInt(1, Common.getRandom());
 				try (ResultSet results = statement.executeQuery()) {
 					results.next(); // Here the expectation is ONLY only one
 									// result row

+ 8 - 6
frameworks/Java/servlet/src/main/java/hello/FortunesServlet.java

@@ -42,12 +42,14 @@ public class FortunesServlet extends HttpServlet {
 
 		final List<Fortune> fortunes = new ArrayList<>(32);
 
-		try (Connection conn = dataSource.getConnection();
-				PreparedStatement statement = conn.prepareStatement(DB_QUERY,
-						ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
-				ResultSet results = statement.executeQuery()) {
-			while (results.next()) {
-				fortunes.add(new Fortune(results.getInt("id"), results.getString("message")));
+		try (Connection conn = dataSource.getConnection()) {
+			Common.modifySQLConnectionSettings(conn);
+			try (PreparedStatement statement = conn.prepareStatement(DB_QUERY,
+					ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
+					ResultSet results = statement.executeQuery()) {
+				while (results.next()) {
+					fortunes.add(new Fortune(results.getInt("id"), results.getString("message")));
+				}
 			}
 		} catch (SQLException sqlex) {
 			throw new ServletException(sqlex);

+ 3 - 3
frameworks/Java/servlet/src/main/java/hello/PostgresUpdateServlet.java

@@ -24,7 +24,6 @@ public class PostgresUpdateServlet extends HttpServlet {
 	// Database details.
 	private static final String DB_QUERY = "SELECT * FROM World WHERE id = ?";
 	private static final String UPDATE_QUERY = "UPDATE World SET randomNumber = ? WHERE id = ?";
-	private static final int DB_ROWS = 10000;
 
 	// Database connection pool.
 	@Resource(name = "jdbc/hello_world")
@@ -43,9 +42,10 @@ public class PostgresUpdateServlet extends HttpServlet {
 				PreparedStatement statement = conn.prepareStatement(DB_QUERY,
 						ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
 				PreparedStatement statement2 = conn.prepareStatement(UPDATE_QUERY)) {
+			Common.modifySQLConnectionSettings(conn);
 			// Run the query the number of times requested.
 			for (int i = 0; i < count; i++) {
-				final int id = random.nextInt(DB_ROWS) + 1;
+				final int id = Common.getRandom();
 				statement.setInt(1, id);
 
 				try (ResultSet results = statement.executeQuery()) {
@@ -53,7 +53,7 @@ public class PostgresUpdateServlet extends HttpServlet {
 						worlds[i] = new World(id, results.getInt("randomNumber"));
 
 						// Update row
-						worlds[i].setRandomNumber(random.nextInt(DB_ROWS) + 1);
+						worlds[i].setRandomNumber(Common.getRandom());
 						statement2.setInt(1, worlds[i].getRandomNumber());
 						statement2.setInt(2, id);
 

+ 2 - 3
frameworks/Java/servlet/src/main/java/hello/QueriesSQLServlet.java

@@ -22,7 +22,6 @@ import javax.sql.DataSource;
 public class QueriesSQLServlet extends HttpServlet {
 	// Database details.
 	private static final String DB_QUERY = "SELECT * FROM World WHERE id = ?";
-	private static final int DB_ROWS = 10000;
 
 	// Database connection pool.
 	@Resource(name = "jdbc/hello_world")
@@ -33,15 +32,15 @@ public class QueriesSQLServlet extends HttpServlet {
 			IOException {
 		final int count = Common.normalise(req.getParameter("queries"));
 		final World[] worlds = new World[count];
-		final Random random = ThreadLocalRandom.current();
 
 		// Fetch some rows from the database.
 		try (Connection conn = dataSource.getConnection()) {
+			Common.modifySQLConnectionSettings(conn);
 			try (PreparedStatement statement = conn.prepareStatement(DB_QUERY,
 					ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
 				// Run the query the number of times requested.
 				for (int i = 0; i < count; i++) {
-					final int id = random.nextInt(DB_ROWS) + 1;
+					final int id = Common.getRandom();
 					statement.setInt(1, id);
 
 					try (ResultSet results = statement.executeQuery()) {

+ 3 - 4
frameworks/Java/servlet/src/main/java/hello/UpdateServlet.java

@@ -24,7 +24,6 @@ public class UpdateServlet extends HttpServlet {
 	// Database details.
 	private static final String DB_QUERY = "SELECT * FROM World WHERE id = ?";
 	private static final String UPDATE_QUERY = "UPDATE World SET randomNumber = ? WHERE id = ?";
-	private static final int DB_ROWS = 10000;
 
 	// Database connection pool.
 	@Resource(name = "jdbc/hello_world")
@@ -37,16 +36,16 @@ public class UpdateServlet extends HttpServlet {
 		final DataSource source = mysqlDataSource;
 		final int count = Common.normalise(req.getParameter(PARAMETER_QUERIES));
 		final World[] worlds = new World[count];
-		final Random random = ThreadLocalRandom.current();
 
 		try (Connection conn = source.getConnection();
 				PreparedStatement statement = conn.prepareStatement(DB_QUERY,
 						ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
 				PreparedStatement statement2 = conn.prepareStatement(UPDATE_QUERY,
 						ResultSet.TYPE_FORWARD_ONLY)) {
+			Common.modifySQLConnectionSettings(conn);
 			// Run the query the number of times requested.
 			for (int i = 0; i < count; i++) {
-				final int id = random.nextInt(DB_ROWS) + 1;
+				final int id = Common.getRandom();
 				statement.setInt(1, id);
 
 				try (ResultSet results = statement.executeQuery()) {
@@ -54,7 +53,7 @@ public class UpdateServlet extends HttpServlet {
 						worlds[i] = new World(id, results.getInt("randomNumber"));
 
 						// Update row
-						worlds[i].setRandomNumber(random.nextInt(DB_ROWS) + 1);
+						worlds[i].setRandomNumber(Common.getRandom());
 						statement2.setInt(1, worlds[i].getRandomNumber());
 						statement2.setInt(2, id);