소스 검색

Merge pull request #1674 from denkab/master

Fix parameter parsing and Hibernate Session use
Mike Smith 10 년 전
부모
커밋
4e8c428f92
2개의 변경된 파일16개의 추가작업 그리고 14개의 파일을 삭제
  1. 1 1
      frameworks/Java/grizzly-jersey/benchmark_config.json
  2. 15 13
      frameworks/Java/grizzly-jersey/src/main/java/hello/DbResource.java

+ 1 - 1
frameworks/Java/grizzly-jersey/benchmark_config.json

@@ -4,7 +4,7 @@
     "default": {
       "setup_file": "setup",
       "json_url": "/json",
-      "db_url": "/db",
+      "db_url": "/db?single=true",
       "query_url": "/db?queries=",
       "fortune_url": "/fortunes",
       "port": 8080,

+ 15 - 13
frameworks/Java/grizzly-jersey/src/main/java/hello/DbResource.java

@@ -11,14 +11,12 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.ThreadLocalRandom;
 
-import javax.ws.rs.DefaultValue;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.Context;
 
-import org.hibernate.IdentifierLoadAccess;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 
@@ -35,15 +33,12 @@ public class DbResource {
   
   @GET
   @Produces(APPLICATION_JSON + "; charset=utf-8")
-  public Object db(@QueryParam("queries") String queriesParam)
+  public Object db(@QueryParam("queries") String queryParam, @QueryParam("single") boolean isSingle)
       throws ExecutionException, InterruptedException {
 
-    final int queries = getQueries(queriesParam);
+    final int queries = getQueries(queryParam);
     final World[] worlds = new World[queries];
     final Random random = ThreadLocalRandom.current();
-    final Session session = sessionFactory.openSession();
-    session.setDefaultReadOnly(true);
-    final IdentifierLoadAccess accessor = session.byId(World.class);
 
     Map<Integer, Future<World>> futureWorlds = new ConcurrentHashMap<>();
     for (int i = 0; i < queries; i++) {
@@ -51,7 +46,14 @@ public class DbResource {
         new Callable<World>() {
           @Override
           public World call() throws Exception {
-            return (World) accessor.load(random.nextInt(DB_ROWS) + 1);
+            Session session = sessionFactory.openSession();
+            session.setDefaultReadOnly(true);
+
+            try {
+              return (World) session.byId(World.class).load(random.nextInt(DB_ROWS) + 1);
+            } finally {
+              session.close();
+            }
           }
         }
       ));
@@ -61,16 +63,16 @@ public class DbResource {
       worlds[i] = futureWorlds.get(i).get();
     }
 
-    return queries == 1 ? worlds[0] : worlds;
+    return isSingle ? worlds[0] : worlds;
   }
 
   private int getQueries(String proto) {
     int result = 1;
     try {
-      result = Integer.parseInt(proto);
-    } catch (NumberFormatException e) {
-      e.printStackTrace();
-    }
+      if (proto != null && !proto.trim().isEmpty()) {
+        result = Integer.parseInt(proto);
+      }
+    } catch (NumberFormatException e) {/* by test contract */}
 
     return Math.min(500, Math.max(1, result));
   }