Browse Source

Attempt to fix deadlock in JDBI test (#4639)

As mentioned here : https://github.com/TechEmpower/FrameworkBenchmarks/pull/4546#issuecomment-481678409
Fred Deschenes 6 years ago
parent
commit
18ef8a0a65

+ 13 - 4
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/jdbi/WorldRepository.java

@@ -6,6 +6,10 @@ import com.example.helloworld.db.WorldDAO;
 import com.example.helloworld.db.model.World;
 import com.example.helloworld.db.model.World;
 import com.example.helloworld.resources.Helper;
 import com.example.helloworld.resources.Helper;
 
 
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+
 public class WorldRepository implements WorldDAO {
 public class WorldRepository implements WorldDAO {
 	private final Jdbi jdbi;
 	private final Jdbi jdbi;
 
 
@@ -26,17 +30,22 @@ public class WorldRepository implements WorldDAO {
 	@Override
 	@Override
 	public World[] updatesQueries(int totalQueries) {
 	public World[] updatesQueries(int totalQueries) {
 		return jdbi.withExtension(WorldJDBIImpl.class, dao -> {
 		return jdbi.withExtension(WorldJDBIImpl.class, dao -> {
-			final World[] updates = new World[totalQueries];
+			final List<World> updates = new ArrayList<>(totalQueries);
 
 
 			for (int i = 0; i < totalQueries; i++) {
 			for (int i = 0; i < totalQueries; i++) {
 				final World world = dao.findById(Helper.randomWorld());
 				final World world = dao.findById(Helper.randomWorld());
 				world.setRandomNumber(Helper.randomWorld());
 				world.setRandomNumber(Helper.randomWorld());
-				updates[i] = world;
+				updates.add(i, world);
 			}
 			}
 
 
-			dao.update(updates);
+			// Reason for sorting : https://github.com/TechEmpower/FrameworkBenchmarks/pull/2684
+			updates.sort(Comparator.comparingInt(World::getId));
+
+			final World[] updatesArray = updates.toArray(new World[totalQueries]);
+
+			dao.update(updatesArray);
 
 
-			return updates;
+			return updatesArray;
 		});
 		});
 	}
 	}