Browse Source

Merge pull request #1687 from yunspace/postgres

Add Dropwizard Postgres, Upgrade to DW 0.8.1
Mike Smith 10 years ago
parent
commit
af38c20c46
28 changed files with 450 additions and 287 deletions
  1. 9 0
      frameworks/Java/dropwizard/README.md
  2. 31 10
      frameworks/Java/dropwizard/benchmark_config.json
  3. 18 0
      frameworks/Java/dropwizard/hello-world-mongo.yml
  4. 1 8
      frameworks/Java/dropwizard/hello-world-mysql.yml
  5. 43 0
      frameworks/Java/dropwizard/hello-world-postgres.yml
  6. 52 9
      frameworks/Java/dropwizard/pom.xml
  7. 10 0
      frameworks/Java/dropwizard/setup_mongo.sh
  8. 3 3
      frameworks/Java/dropwizard/setup_mysql.sh
  9. 10 0
      frameworks/Java/dropwizard/setup_postgres.sh
  10. 10 16
      frameworks/Java/dropwizard/source_code
  11. 52 0
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/HelloMongoService.java
  12. 12 39
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/HelloWorldService.java
  13. 18 0
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/config/HelloMongoConfiguration.java
  14. 0 9
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/config/HelloWorldConfiguration.java
  15. 11 1
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/config/MongoClientFactory.java
  16. 2 11
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/FortuneDAO.java
  17. 4 16
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/WorldDAO.java
  18. 19 0
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/hibernate/FortuneHibernateImpl.java
  19. 41 0
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/hibernate/WorldHibernateImpl.java
  20. 2 2
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/model/Fortune.java
  21. 5 5
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/model/World.java
  22. 22 0
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/mongo/FortuneMongoImpl.java
  23. 1 1
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/mongo/MongoManaged.java
  24. 45 0
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/mongo/WorldMongoImpl.java
  25. 19 33
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/Helper.java
  26. 0 37
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/MongoFortuneResource.java
  27. 0 65
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/MongoWorldResource.java
  28. 10 22
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/WorldResource.java

+ 9 - 0
frameworks/Java/dropwizard/README.md

@@ -0,0 +1,9 @@
+# Dropwizard Benchmarking Test
+
+This project consists of 3 tests:
+
+* MySql and Postgres - uses: `HelloWorldService.java` and `HelloWorldConfiguration.java`
+* MongoDB - uses: `HelloMongoService.java` and `HelloMongoConfiguration.java`
+
+All other `resources` and `model` code are shared. The main `*Service.java` is set based on maven profiles.
+Respective `.yml` configuration files are specified in each `setup_*.sh` script.

+ 31 - 10
frameworks/Java/dropwizard/benchmark_config.json

@@ -2,7 +2,7 @@
   "framework": "dropwizard",
   "tests": [{
     "default": {
-      "setup_file": "setup",
+      "setup_file": "setup_mysql",
       "json_url": "/json",
       "db_url": "/db",
       "query_url": "/db?queries=",
@@ -21,15 +21,15 @@
       "os": "Linux",
       "database_os": "Linux",
       "display_name": "dropwizard",
-      "notes": "dropwizard with mysql using hibernate",
-      "versus": ""
+      "notes": "dropwizard mysql using hibernate",
+      "versus": "servlet"
     },
     "mongodb": {
-      "setup_file": "setup",
-      "db_url": "/mongo/db",
-      "query_url": "/mongo/db?queries=",
-      "fortune_url": "/mongo/fortunes",
-      "update_url": "/mongo/db/update?queries=",
+      "setup_file": "setup_mongo",
+      "db_url": "/db",
+      "query_url": "/db?queries=",
+      "fortune_url": "/fortunes",
+      "update_url": "/db/update?queries=",
       "port": 9090,
       "approach": "Realistic",
       "classification": "Fullstack",
@@ -41,9 +41,30 @@
       "webserver": "Jetty",
       "os": "Linux",
       "database_os": "Linux",
-      "display_name": "dropwizard-mongodb",
-      "notes": "dropwizard with mongodb using mongojack",
+      "display_name": "dropwizard",
+      "notes": "dropwizard mongodb using mongojack",
       "versus": ""
+    },
+    "postgres": {
+      "setup_file": "setup_postgres",
+      "db_url": "/db",
+      "query_url": "/db?queries=",
+      "fortune_url": "/fortunes",
+      "update_url": "/db/update?queries=",
+      "port": 9090,
+      "approach": "Realistic",
+      "classification": "Fullstack",
+      "database": "Postgres",
+      "framework": "dropwizard",
+      "language": "Java",
+      "orm": "Full",
+      "platform": "Jetty",
+      "webserver": "Jetty",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "dropwizard",
+      "notes": "dropwizard postgres using hibernate",
+      "versus": "servlet"
     }
   }]
 }

+ 18 - 0
frameworks/Java/dropwizard/hello-world-mongo.yml

@@ -0,0 +1,18 @@
+server:
+  type: simple
+  applicationContextPath: /
+  connector:
+    type: http
+    port: 9090
+    useServerHeader: true
+
+  requestLog:
+    appenders: []
+
+logging:
+  appenders: []
+
+mongo:
+  host: 127.0.0.1
+  port: 27017
+  db: hello_world

+ 1 - 8
frameworks/Java/dropwizard/hello-world.yml → frameworks/Java/dropwizard/hello-world-mysql.yml

@@ -28,6 +28,7 @@ database:
   # any properties specific to your JDBC driver:
   properties:
     charSet: UTF-8
+    hibernate.dialect: org.hibernate.dialect.MySQLDialect
 
   # the maximum amount of time to wait on an empty pool before throwing an exception
   maxWaitForConnection: 1s
@@ -40,11 +41,3 @@ database:
 
   # whether or not idle connections should be validated
   checkConnectionWhileIdle: false
-
-  properties:
-    hibernate.dialect: org.hibernate.dialect.MySQLDialect
-
-mongo:
-  host: 127.0.0.1
-  port: 27017
-  db: hello_world

+ 43 - 0
frameworks/Java/dropwizard/hello-world-postgres.yml

@@ -0,0 +1,43 @@
+server:
+  type: simple
+  applicationContextPath: /
+  connector:
+    type: http
+    port: 9090
+    useServerHeader: true
+
+  requestLog:
+    appenders: []
+
+logging:
+  appenders: []
+
+database:
+  # the name of your JDBC driver
+  driverClass: org.postgresql.Driver
+
+  # the username
+  user: benchmarkdbuser
+
+  # the password
+  password: benchmarkdbpass
+
+  # the JDBC URL
+  url: jdbc:postgresql://127.0.0.1:5432/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts&cacheRSMetadata=true
+
+  # any properties specific to your JDBC driver:
+  properties:
+    charSet: UTF-8
+    hibernate.dialect: org.hibernate.dialect.PostgreSQL9Dialect
+
+  # the maximum amount of time to wait on an empty pool before throwing an exception
+  maxWaitForConnection: 1s
+
+  # the minimum number of connections to keep open
+  minSize: 8
+
+  # the maximum number of connections to keep open
+  maxSize: 256
+
+  # whether or not idle connections should be validated
+  checkConnectionWhileIdle: false

+ 52 - 9
frameworks/Java/dropwizard/pom.xml

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
 
     <groupId>com.xekm</groupId>
@@ -9,11 +10,45 @@
     <properties>
         <jdk.version>1.7</jdk.version>
 
-        <dropwizard.version>0.7.1</dropwizard.version>
+        <dropwizard.version>0.8.1</dropwizard.version>
         <mysql-connector-java.version>5.1.35</mysql-connector-java.version>
-        <mongojack.version>2.1.0</mongojack.version>
+        <mongojack.version>2.3.0</mongojack.version>
+        <postgres-jdbc.version>9.4-1201-jdbc41</postgres-jdbc.version>
+        <dropwizard.java8.version>0.8.0-1</dropwizard.java8.version>
+
+        <main.class>com.example.helloworld.HelloWorldService</main.class>
     </properties>
 
+
+    <profiles>
+        <profile>
+            <id>mysql</id>
+            <dependencies>
+                <dependency>
+                    <groupId>mysql</groupId>
+                    <artifactId>mysql-connector-java</artifactId>
+                    <version>${mysql-connector-java.version}</version>
+                </dependency>
+            </dependencies>
+        </profile>
+        <profile>
+            <id>mongo</id>
+            <properties>
+                <main.class>com.example.helloworld.HelloMongoService</main.class>
+            </properties>
+        </profile>
+        <profile>
+            <id>postgres</id>
+            <dependencies>
+                <dependency>
+                    <groupId>org.postgresql</groupId>
+                    <artifactId>postgresql</artifactId>
+                    <version>${postgres-jdbc.version}</version>
+                </dependency>
+            </dependencies>
+        </profile>
+    </profiles>
+
     <dependencies>
         <dependency>
             <groupId>io.dropwizard</groupId>
@@ -31,14 +66,20 @@
             <version>${dropwizard.version}</version>
         </dependency>
         <dependency>
-            <groupId>mysql</groupId>
-            <artifactId>mysql-connector-java</artifactId>
-            <version>${mysql-connector-java.version}</version>
+            <groupId>io.dropwizard.modules</groupId>
+            <artifactId>dropwizard-java8</artifactId>
+            <version>${dropwizard.java8.version}</version>
         </dependency>
         <dependency>
             <groupId>org.mongojack</groupId>
             <artifactId>mongojack</artifactId>
             <version>${mongojack.version}</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>com.fasterxml.jackson.core</groupId>
+                    <artifactId>jackson-databind</artifactId>
+                </exclusion>
+            </exclusions>
         </dependency>
     </dependencies>
 
@@ -90,9 +131,11 @@
                         </goals>
                         <configuration>
                             <transformers>
-                                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
-                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
-                                    <mainClass>com.example.helloworld.HelloWorldService</mainClass>
+                                <transformer
+                                        implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
+                                <transformer
+                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+                                    <mainClass>${main.class}</mainClass>
                                 </transformer>
                             </transformers>
                         </configuration>

+ 10 - 0
frameworks/Java/dropwizard/setup_mongo.sh

@@ -0,0 +1,10 @@
+#!/bin/bash
+
+# load java environment variables
+source $IROOT/java7.installed
+
+sed -i 's|host: 127.0.0.1|host: '"${DBHOST}"'|g' hello-world-mongo.yml
+
+mvn -P mongo clean package
+
+java -jar target/hello-world-0.0.1-SNAPSHOT.jar server hello-world-mongo.yml &

+ 3 - 3
frameworks/Java/dropwizard/setup.sh → frameworks/Java/dropwizard/setup_mysql.sh

@@ -3,8 +3,8 @@
 # load java environment variables
 source $IROOT/java7.installed
 
-sed -i 's|url: jdbc:mysql://.*/hello_world|url: jdbc:mysql://'"${DBHOST}"':3306/hello_world|g' hello-world.yml
+sed -i 's|url: jdbc:mysql://.*/hello_world|url: jdbc:mysql://'"${DBHOST}"':3306/hello_world|g' hello-world-mysql.yml
 
-mvn clean package
+mvn -P mysql clean package
 
-java -jar target/hello-world-0.0.1-SNAPSHOT.jar server hello-world.yml &
+java -jar target/hello-world-0.0.1-SNAPSHOT.jar server hello-world-mysql.yml &

+ 10 - 0
frameworks/Java/dropwizard/setup_postgres.sh

@@ -0,0 +1,10 @@
+#!/bin/bash
+
+# load java environment variables
+source $IROOT/java7.installed
+
+sed -i 's|url: jdbc:postgresql://.*/hello_world|url: jdbc:postgresql://'"${DBHOST}"':5432/hello_world|g' hello-world-postgres.yml
+
+mvn -P postgres clean package
+
+java -jar target/hello-world-0.0.1-SNAPSHOT.jar server hello-world-postgres.yml &

+ 10 - 16
frameworks/Java/dropwizard/source_code

@@ -1,28 +1,22 @@
-./dropwizard/src/main/java/com/example/helloworld/
-./dropwizard/src/main/java/com/example/helloworld/config
 ./dropwizard/src/main/java/com/example/helloworld/config/HelloWorldConfiguration.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/config/MongoConfiguration.java
-./dropwizard/src/main/java/com/example/helloworld/db
-./dropwizard/src/main/java/com/example/helloworld/db/model
+./dropwizard/src/main/java/com/example/helloworld/config/HelloMongoConfiguration.java
+./dropwizard/src/main/java/com/example/helloworld/config/MongoClientFactory.java
 ./dropwizard/src/main/java/com/example/helloworld/db/model/Fortune.java
 ./dropwizard/src/main/java/com/example/helloworld/db/model/World.java
 ./dropwizard/src/main/java/com/example/helloworld/db/FortuneDAO.java
 ./dropwizard/src/main/java/com/example/helloworld/db/WorldDAO.java
-./dropwizard/src/main/java/com/example/helloworld/db/mongo
+./dropwizard/src/main/java/com/example/helloworld/db/hibernate/FortuneHibernateImpl.java
+./dropwizard/src/main/java/com/example/helloworld/db/hibernate/WorldHibernateImpl.java
+./dropwizard/src/main/java/com/example/helloworld/db/mongo/FortuneMongoImpl.java
+./dropwizard/src/main/java/com/example/helloworld/db/mongo/WorldMongoImpl.java
 ./dropwizard/src/main/java/com/example/helloworld/db/mongo/MongoManaged.java
-./dropwizard/src/main/java/com/example/helloworld/db/mongo/MongoWorldDAO.java
-./dropwizard/src/main/java/com/example/helloworld/db/mongo/MongoFortuneDAO.java
-./dropwizard/src/main/java/com/example/helloworld/resources
-./dropwizard/src/main/java/com/example/helloworld/resources/api
-./dropwizard/src/main/java/com/example/helloworld/resources/api/HelloMessage
-./dropwizard/src/main/java/com/example/helloworld/resources/views
-./dropwizard/src/main/java/com/example/helloworld/resources/views/FortuneView
+./dropwizard/src/main/java/com/example/helloworld/resources/api/HelloMessage.java
+./dropwizard/src/main/java/com/example/helloworld/resources/views/FortuneView.java
 ./dropwizard/src/main/java/com/example/helloworld/resources/FortuneResource.java
 ./dropwizard/src/main/java/com/example/helloworld/resources/JsonResource.java
 ./dropwizard/src/main/java/com/example/helloworld/resources/TextResource.java
 ./dropwizard/src/main/java/com/example/helloworld/resources/WorldResource.java
-./dropwizard/src/main/java/com/example/helloworld/resources/MongoFortuneResource.java
-./dropwizard/src/main/java/com/example/helloworld/resources/MongoWorldResource.java
 ./dropwizard/src/main/java/com/example/helloworld/resources/Helper.java
 ./dropwizard/src/main/java/com/example/helloworld/HelloWorldService.java
-./dropwizard/src/main/resources/fortunes.mustache
+./dropwizard/src/main/java/com/example/helloworld/HelloMongoService.java
+./dropwizard/src/main/resources/fortunes.mustache

+ 52 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/HelloMongoService.java

@@ -0,0 +1,52 @@
+package com.example.helloworld;
+
+import com.example.helloworld.config.HelloMongoConfiguration;
+import com.example.helloworld.db.model.Fortune;
+import com.example.helloworld.db.model.World;
+import com.example.helloworld.db.mongo.FortuneMongoImpl;
+import com.example.helloworld.db.mongo.MongoManaged;
+import com.example.helloworld.db.mongo.WorldMongoImpl;
+import com.example.helloworld.resources.FortuneResource;
+import com.example.helloworld.resources.WorldResource;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.mongodb.DB;
+import com.mongodb.MongoClient;
+import io.dropwizard.Application;
+import io.dropwizard.jackson.Jackson;
+import io.dropwizard.setup.Bootstrap;
+import io.dropwizard.setup.Environment;
+import io.dropwizard.views.ViewBundle;
+import org.mongojack.JacksonDBCollection;
+import org.mongojack.internal.MongoJackModule;
+
+import java.net.UnknownHostException;
+
+public class HelloMongoService extends Application<HelloMongoConfiguration> {
+
+    public static void main(String[] args) throws Exception {
+        new HelloMongoService().run(args);
+    }
+
+    @Override
+    public void initialize(Bootstrap<HelloMongoConfiguration> bootstrap) {
+        bootstrap.addBundle(new ViewBundle());
+    }
+
+    @Override
+    public void run(HelloMongoConfiguration config, Environment environment) throws UnknownHostException {
+        final MongoClient mongoClient = config.getMongo().build();
+        environment.lifecycle().manage(new MongoManaged(mongoClient));
+
+        final DB db = mongoClient.getDB(config.getMongo().getDb());
+        final ObjectMapper mongoJackMapper = MongoJackModule.configure(Jackson.newObjectMapper());
+        final JacksonDBCollection<World, Integer> worlds =
+                JacksonDBCollection.wrap(db.getCollection("World"), World.class, Integer.class, mongoJackMapper);
+
+        final JacksonDBCollection<Fortune, Integer> fortunes =
+                JacksonDBCollection.wrap(db.getCollection("Fortune"), Fortune.class, Integer.class, mongoJackMapper);
+
+        environment.jersey().register(new WorldResource(new WorldMongoImpl(worlds))); // Test types 2, 3 & 5: Single database query, Multiple database queries & Database updates
+        environment.jersey().register(new FortuneResource(new FortuneMongoImpl(fortunes))); // Test type 4: Fortunes
+    }
+
+}

+ 12 - 39
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/HelloWorldService.java

@@ -1,25 +1,20 @@
 package com.example.helloworld;
 
 import com.example.helloworld.config.HelloWorldConfiguration;
-import com.example.helloworld.config.MongoConfiguration;
-import com.example.helloworld.db.FortuneDAO;
-import com.example.helloworld.db.MongoManaged;
-import com.example.helloworld.db.WorldDAO;
+import com.example.helloworld.db.hibernate.FortuneHibernateImpl;
+import com.example.helloworld.db.hibernate.WorldHibernateImpl;
 import com.example.helloworld.db.model.Fortune;
 import com.example.helloworld.db.model.World;
-import com.example.helloworld.resources.*;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.mongodb.DB;
-import com.mongodb.MongoClient;
+import com.example.helloworld.resources.FortuneResource;
+import com.example.helloworld.resources.JsonResource;
+import com.example.helloworld.resources.TextResource;
+import com.example.helloworld.resources.WorldResource;
 import io.dropwizard.Application;
 import io.dropwizard.db.DataSourceFactory;
 import io.dropwizard.hibernate.HibernateBundle;
-import io.dropwizard.jackson.Jackson;
 import io.dropwizard.setup.Bootstrap;
 import io.dropwizard.setup.Environment;
 import io.dropwizard.views.ViewBundle;
-import org.mongojack.JacksonDBCollection;
-import org.mongojack.internal.MongoJackModule;
 
 import java.net.UnknownHostException;
 
@@ -44,33 +39,11 @@ public class HelloWorldService extends Application<HelloWorldConfiguration> {
 
     @Override
     public void run(HelloWorldConfiguration config, Environment environment) throws UnknownHostException {
-        environment.jersey().register(new JsonResource()); // Test type 1: JSON serialization
-        environment.jersey().register(new WorldResource(new WorldDAO(hibernate.getSessionFactory()))); // Test types 2, 3 & 5: Single database query, Multiple database queries & Database updates
-        environment.jersey().register(new FortuneResource(new FortuneDAO(hibernate.getSessionFactory()))); // Test type 4: Fortunes
-        environment.jersey().register(new TextResource()); // Test type 6: Plaintext
-
-        setUpMongo(config.getMongo(), environment);
-    }
-
-    private void setUpMongo(MongoConfiguration config, Environment environment) throws UnknownHostException {
-        final MongoClient mongoClient = new MongoClient(config.getHost(), config.getPort());
-        environment.lifecycle().manage(new MongoManaged(mongoClient));
-
-        final DB db = mongoClient.getDB(config.getDb());
-        final ObjectMapper mongoJackMapper = MongoJackModule.configure(Jackson.newObjectMapper());
-        final JacksonDBCollection<World, Long> worlds = JacksonDBCollection.wrap(
-                db.getCollection("World"),
-                World.class,
-                Long.class,
-                mongoJackMapper);
-
-        final JacksonDBCollection<Fortune, Long> fortunes = JacksonDBCollection.wrap(
-                db.getCollection("Fortune"),
-                Fortune.class,
-                Long.class,
-                mongoJackMapper);
-
-        environment.jersey().register(new MongoWorldResource(worlds));
-        environment.jersey().register(new MongoFortuneResource(fortunes));
+        if ("com.mysql.jdbc.Driver".equals(config.getDatabaseConfiguration().getDriverClass())) { // register below for default dropwizard test only
+            environment.jersey().register(new JsonResource()); // Test type 1: JSON serialization
+            environment.jersey().register(new TextResource()); // Test type 6: Plaintext
+        }
+        environment.jersey().register(new WorldResource(new WorldHibernateImpl(hibernate.getSessionFactory()))); // Test types 2, 3 & 5: Single database query, Multiple database queries & Database updates
+        environment.jersey().register(new FortuneResource(new FortuneHibernateImpl(hibernate.getSessionFactory()))); // Test type 4: Fortunes
     }
 }

+ 18 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/config/HelloMongoConfiguration.java

@@ -0,0 +1,18 @@
+package com.example.helloworld.config;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import io.dropwizard.Configuration;
+
+import javax.validation.Valid;
+import javax.validation.constraints.NotNull;
+
+public class HelloMongoConfiguration extends Configuration {
+    @Valid
+    @NotNull
+    @JsonProperty
+    private MongoClientFactory mongo = new MongoClientFactory();
+
+    public MongoClientFactory getMongo() {
+        return mongo;
+    }
+}

+ 0 - 9
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/config/HelloWorldConfiguration.java

@@ -14,16 +14,7 @@ public class HelloWorldConfiguration extends Configuration {
     @JsonProperty
     private DataSourceFactory database = new DataSourceFactory();
 
-    @Valid
-    @NotNull
-    @JsonProperty
-    private MongoConfiguration mongo = new MongoConfiguration();
-
     public DataSourceFactory getDatabaseConfiguration() {
         return database;
     }
-
-    public MongoConfiguration getMongo() {
-        return mongo;
-    }
 }

+ 11 - 1
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/config/MongoConfiguration.java → frameworks/Java/dropwizard/src/main/java/com/example/helloworld/config/MongoClientFactory.java

@@ -1,12 +1,14 @@
 package com.example.helloworld.config;
 
 import com.fasterxml.jackson.annotation.JsonProperty;
+import com.mongodb.MongoClient;
 import org.hibernate.validator.constraints.NotEmpty;
 
 import javax.validation.constraints.Max;
 import javax.validation.constraints.Min;
+import java.net.UnknownHostException;
 
-public class MongoConfiguration {
+public class MongoClientFactory {
     @JsonProperty
     @NotEmpty
     private String host;
@@ -31,4 +33,12 @@ public class MongoConfiguration {
     public String getDb() {
         return db;
     }
+
+    public MongoClient build() {
+        try {
+            return new MongoClient(host, port);
+        } catch (UnknownHostException e) {
+            throw new RuntimeException(e);
+        }
+    }
 }

+ 2 - 11
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/FortuneDAO.java

@@ -1,18 +1,9 @@
 package com.example.helloworld.db;
 
 import com.example.helloworld.db.model.Fortune;
-import io.dropwizard.hibernate.AbstractDAO;
-import org.hibernate.SessionFactory;
 
 import java.util.List;
 
-public class FortuneDAO extends AbstractDAO<Fortune> {
-
-    public FortuneDAO(SessionFactory factory) {
-        super(factory);
-    }
-
-    public List<Fortune> list() {
-        return list(criteria());
-    }
+public interface FortuneDAO  {
+    List<Fortune> list();
 }

+ 4 - 16
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/WorldDAO.java

@@ -1,21 +1,9 @@
 package com.example.helloworld.db;
 
 import com.example.helloworld.db.model.World;
-import com.google.common.base.Optional;
-import io.dropwizard.hibernate.AbstractDAO;
-import org.hibernate.SessionFactory;
 
-public class WorldDAO extends AbstractDAO<World> {
-
-    public WorldDAO(SessionFactory factory) {
-        super(factory);
-    }
-
-    public Optional<World> findById(Long id) {
-        return Optional.fromNullable(get(id));
-    }
-
-    public World update(World world) {
-        return persist(world);
-    }
+public interface WorldDAO {
+    World findById(int id);
+    World findAndModify(int id, int newRandomNumber);
+    World[] updatesQueries(int queries);
 }

+ 19 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/hibernate/FortuneHibernateImpl.java

@@ -0,0 +1,19 @@
+package com.example.helloworld.db.hibernate;
+
+import com.example.helloworld.db.FortuneDAO;
+import com.example.helloworld.db.model.Fortune;
+import io.dropwizard.hibernate.AbstractDAO;
+import org.hibernate.SessionFactory;
+
+import java.util.List;
+
+public class FortuneHibernateImpl extends AbstractDAO<Fortune> implements FortuneDAO {
+
+    public FortuneHibernateImpl(SessionFactory factory) {
+        super(factory);
+    }
+
+    public List<Fortune> list() {
+        return list(criteria());
+    }
+}

+ 41 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/hibernate/WorldHibernateImpl.java

@@ -0,0 +1,41 @@
+package com.example.helloworld.db.hibernate;
+
+import com.example.helloworld.db.WorldDAO;
+import com.example.helloworld.db.model.World;
+import com.example.helloworld.resources.Helper;
+import io.dropwizard.hibernate.AbstractDAO;
+import org.hibernate.SessionFactory;
+import org.hibernate.Transaction;
+
+public class WorldHibernateImpl extends AbstractDAO<World> implements WorldDAO {
+    public WorldHibernateImpl(SessionFactory factory) {
+        super(factory);
+    }
+
+    public World findById(int id) {
+        return get(id);
+    }
+
+    public World findAndModify(int id, int newRandomNumber) {
+        final World world = get(id);
+        world.setRandomNumber(newRandomNumber);
+        return persist(world);
+    }
+
+    @Override
+    public World[] updatesQueries(int totalQueries) {
+        final World[] worlds = new World[totalQueries];
+        for (int i = 0; i < totalQueries; i++) {
+            Transaction transaction = currentSession().beginTransaction();
+            try {
+                worlds[i] = findAndModify(Helper.randomWorld(), Helper.randomWorld());
+                transaction.commit();
+            } catch (Exception e) {
+                transaction.rollback();
+                throw new RuntimeException(e);
+            }
+        }
+        return worlds;
+    }
+
+}

+ 2 - 2
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/model/Fortune.java

@@ -10,7 +10,7 @@ public class Fortune implements Comparable<Fortune> {
 
     @Id
     @JsonProperty
-    private long id;
+    private int id;
 
     @JsonProperty
     @Column(name = "message", nullable = false)
@@ -23,7 +23,7 @@ public class Fortune implements Comparable<Fortune> {
         this.message = message;
     }
 
-    public long getId() {
+    public int getId() {
         return id;
     }
 

+ 5 - 5
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/model/World.java

@@ -13,24 +13,24 @@ public class World {
 
     @Id
     @JsonProperty
-    private long id;
+    private int id;
 
     @JsonProperty
     @Column(name = "randomNumber", nullable = false)
-    private long randomNumber;
+    private int randomNumber;
 
     public World() {
     }
 
-    public long getId() {
+    public int getId() {
         return id;
     }
 
-    public long getRandomNumber() {
+    public int getRandomNumber() {
         return randomNumber;
     }
 
-    public void setRandomNumber(long randomNumber) {
+    public void setRandomNumber(int randomNumber) {
         this.randomNumber = randomNumber;
     }
 }

+ 22 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/mongo/FortuneMongoImpl.java

@@ -0,0 +1,22 @@
+package com.example.helloworld.db.mongo;
+
+import com.example.helloworld.db.FortuneDAO;
+import com.example.helloworld.db.model.Fortune;
+import org.mongojack.DBProjection;
+import org.mongojack.DBQuery;
+import org.mongojack.JacksonDBCollection;
+
+import java.util.List;
+
+public class FortuneMongoImpl implements FortuneDAO {
+
+    private final JacksonDBCollection<Fortune, Integer> fortuneCollection;
+
+    public FortuneMongoImpl(JacksonDBCollection<Fortune, Integer> fortuneCollection) {
+        this.fortuneCollection = fortuneCollection;
+    }
+    @Override
+    public List<Fortune> list() {
+        return fortuneCollection.find(DBQuery.empty(), DBProjection.include("_id", "message")).toArray();
+    }
+}

+ 1 - 1
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/MongoManaged.java → frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/mongo/MongoManaged.java

@@ -1,4 +1,4 @@
-package com.example.helloworld.db;
+package com.example.helloworld.db.mongo;
 
 import com.mongodb.MongoClient;
 import io.dropwizard.lifecycle.Managed;

+ 45 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/mongo/WorldMongoImpl.java

@@ -0,0 +1,45 @@
+package com.example.helloworld.db.mongo;
+
+import com.example.helloworld.db.WorldDAO;
+import com.example.helloworld.db.model.World;
+import com.example.helloworld.resources.Helper;
+import org.mongojack.DBProjection;
+import org.mongojack.DBQuery;
+import org.mongojack.DBUpdate;
+import org.mongojack.JacksonDBCollection;
+
+public class WorldMongoImpl implements WorldDAO {
+
+    private final JacksonDBCollection<World, Integer> worldCollection;
+
+    public WorldMongoImpl(JacksonDBCollection<World, Integer> worlds) {
+        worldCollection = worlds;
+    }
+
+    @Override
+    public World findById(int id) {
+        return worldCollection.findOneById(id, DBProjection.include("_id", "randomNumber"));
+    }
+
+    @Override
+    public World findAndModify(int worldId, int newRandomNumber) {
+        return worldCollection.findAndModify(
+                DBQuery.is("_id", worldId),
+                DBProjection.include("_id", "randomNumber"),
+                null,
+                false,
+                DBUpdate.set("randomNumber", newRandomNumber),
+                true,
+                false);
+
+    }
+
+    @Override
+    public World[] updatesQueries(int totalQueries) {
+        final World[] worlds = new World[totalQueries];
+        for (int i = 0; i < totalQueries; i++) {
+            worlds[i] = findAndModify(Helper.randomWorld(), Helper.randomWorld());
+        }
+        return worlds;
+    }
+}

+ 19 - 33
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/Helper.java

@@ -1,45 +1,31 @@
 package com.example.helloworld.resources;
 
-import java.util.concurrent.ThreadLocalRandom;
-
 import com.google.common.base.Optional;
 
+import java.util.concurrent.ThreadLocalRandom;
+
 /**
  * Provides utility methods for the benchmark tests.
  * Taken from undertow-edge project.
  */
-final class Helper {
-  private Helper() {
-    throw new AssertionError();
-  }
-
-  /**
-   * Returns the value of the "queries" request parameter, which is an integer
-   * bound between 1 and 500 with a default value of 1.
-   *
-   * @param exchange the current HTTP exchange
-   * @return the value of the "queries" request parameter
-   */
-  static int getQueries(Optional<String> queries) {
-    String value = queries.orNull();
-    if (value == null) {
-      return 1;
+public final class Helper {
+    private Helper() {
+        throw new AssertionError();
     }
-    try {
-      int parsedValue = Integer.parseInt(value);
-      return Math.min(500, Math.max(1, parsedValue));
-    } catch (NumberFormatException e) {
-      return 1;
+
+    static int getQueries(Optional<String> queries) {
+        if (!queries.isPresent()) {
+            return 1;
+        }
+        try {
+            int parsedValue = Integer.parseInt(queries.get());
+            return Math.min(500, Math.max(1, parsedValue));
+        } catch (NumberFormatException e) {
+            return 1;
+        }
     }
-  }
 
-  /**
-   * Returns a random integer that is a suitable value for both the {@code id}
-   * and {@code randomNumber} properties of a world object.
-   *
-   * @return a random world number
-   */
-  static int randomWorld() {
-    return 1 + ThreadLocalRandom.current().nextInt(10000);
-  }
+    public static int randomWorld() {
+        return 1 + ThreadLocalRandom.current().nextInt(10000);
+    }
 }

+ 0 - 37
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/MongoFortuneResource.java

@@ -1,37 +0,0 @@
-package com.example.helloworld.resources;
-
-import com.example.helloworld.db.model.Fortune;
-import com.example.helloworld.resources.views.FortuneView;
-import com.google.common.collect.Lists;
-import org.mongojack.DBProjection;
-import org.mongojack.DBQuery;
-import org.mongojack.JacksonDBCollection;
-
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.MediaType;
-import java.util.Collections;
-import java.util.List;
-
-@Path("/mongo/fortunes")
-@Produces(MediaType.TEXT_HTML + ";charset=UTF-8")
-public class MongoFortuneResource {
-
-    private final JacksonDBCollection<Fortune, Long> fortuneCollection;
-
-    public MongoFortuneResource(JacksonDBCollection<Fortune, Long> fortuneCollection) {
-        this.fortuneCollection = fortuneCollection;
-    }
-
-    @GET
-    public FortuneView dbTest() {
-        final List<Fortune> fortunes = Lists.newArrayListWithExpectedSize(32);
-
-        fortunes.addAll(fortuneCollection.find(DBQuery.empty(), DBProjection.include("_id", "message")).toArray());
-        fortunes.add(new Fortune("Additional fortune added at request time."));
-
-        Collections.sort(fortunes);
-        return new FortuneView(fortunes);
-    }
-}

+ 0 - 65
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/MongoWorldResource.java

@@ -1,65 +0,0 @@
-package com.example.helloworld.resources;
-
-import com.example.helloworld.db.model.World;
-import com.google.common.base.Optional;
-import org.mongojack.DBProjection;
-import org.mongojack.DBQuery;
-import org.mongojack.DBUpdate;
-import org.mongojack.JacksonDBCollection;
-
-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.MediaType;
-
-import static com.example.helloworld.resources.Helper.getQueries;
-
-@Path("/mongo/db")
-@Produces(MediaType.APPLICATION_JSON)
-public class MongoWorldResource {
-    private final JacksonDBCollection<World, Long> worldsCollection;
-
-    public MongoWorldResource(JacksonDBCollection<World, Long> worldsCollection) {
-        this.worldsCollection = worldsCollection;
-    }
-
-    @GET
-    public Object dbTest(@QueryParam("queries") Optional<String> queries) {
-        int totalQueries = getQueries(queries);
-        final World[] worlds = new World[totalQueries];
-
-        for (int i = 0; i < totalQueries; i++) {
-            worlds[i] = worldsCollection.findOneById(
-                    (long) Helper.randomWorld(),
-                    DBProjection.include("_id", "randomNumber")
-            );
-        }
-        if (!queries.isPresent()) {
-            return worlds[0];
-        } else {
-            return worlds;
-        }
-    }
-
-    @GET
-    @Path("/update")
-    public World[] updateTest(@QueryParam("queries") Optional<String> queries) {
-        int totalQueries = getQueries(queries);
-        final World[] worlds = new World[totalQueries];
-
-        for (int i = 0; i < totalQueries; i++) {
-            // use findAndModify() similar to nodejs-mongodb-raw
-            worlds[i] = worldsCollection.findAndModify(
-                    DBQuery.is("_id", (long) Helper.randomWorld()),
-                    DBProjection.include("_id", "randomNumber"),
-                    null,
-                    false,
-                    DBUpdate.set("randomNumber", (long) Helper.randomWorld()),
-                    true,
-                    false
-            );
-        }
-        return worlds;
-    }
-}

+ 10 - 22
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/WorldResource.java

@@ -23,34 +23,22 @@ public class WorldResource {
     @GET
     @UnitOfWork
     public Object dbTest(@QueryParam("queries") Optional<String> queries) {
-        int totalQueries = Helper.getQueries(queries);
-        final World[] worlds = new World[totalQueries];
-
-        for (int i = 0; i < totalQueries; i++) {
-            final long worldId = Helper.randomWorld();
-            worlds[i] = worldDAO.findById(worldId).orNull();
-        }
-        if (!queries.isPresent()) {
-        	return worlds[0];
+        if (queries.isPresent()) {
+            int totalQueries = Helper.getQueries(queries);
+            final World[] worlds = new World[totalQueries];
+            for (int i = 0; i < totalQueries; i++) {
+                worlds[i] = worldDAO.findById(Helper.randomWorld());
+            }
+            return worlds;
         } else {
-        	return worlds;
+            return worldDAO.findById(Helper.randomWorld());
         }
     }
 
     @GET
     @Path("/update")
-    @UnitOfWork
+    @UnitOfWork(transactional = false)
     public World[] updateTest(@QueryParam("queries") Optional<String> queries) {
-        int totalQueries = Helper.getQueries(queries);
-        final World[] worlds = new World[totalQueries];
-
-        for (int i = 0; i < totalQueries; i++) {
-            final long worldId = Helper.randomWorld();
-
-            final World world = worldDAO.findById(worldId).orNull();
-            world.setRandomNumber(Helper.randomWorld());
-            worlds[i] = worldDAO.update(world);
-        }
-        return worlds;
+        return worldDAO.updatesQueries(Helper.getQueries(queries));
     }
 }