Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add check to distinguish between db and multiple queries * Handle non-numeric parameter value * Add separate implementations for db and query tests for MySQL * Disable SSL for MySQL connections * Add separate handlers for db and queries for MongoDB
- Loading branch information
1 parent
42873e9
commit 1d83a3a
Showing
10 changed files
with
261 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
frameworks/Java/restexpress/src/main/java/hello/controller/DbMongodbController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package hello.controller; | ||
|
||
import hello.controller.persistence.WorldsMongodbRepository; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
import com.strategicgains.restexpress.Request; | ||
import com.strategicgains.restexpress.Response; | ||
|
||
public class DbMongodbController { | ||
// Database details. | ||
private static final int DB_ROWS = 10000; | ||
|
||
private WorldsMongodbRepository worldRepo; | ||
|
||
public DbMongodbController(WorldsMongodbRepository worldsRepository) { | ||
super(); | ||
this.worldRepo = worldsRepository; | ||
} | ||
|
||
public Object read(Request request, Response response) { | ||
// Fetch some rows from the database. | ||
final int random = 1 + ThreadLocalRandom.current().nextInt(DB_ROWS); | ||
|
||
return worldRepo.find(random); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
frameworks/Java/restexpress/src/main/java/hello/controller/DbMysqlController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package hello.controller; | ||
|
||
import hello.domain.World; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import com.strategicgains.restexpress.Request; | ||
import com.strategicgains.restexpress.Response; | ||
|
||
public class DbMysqlController { | ||
// Database details. | ||
private static final String DB_QUERY = "SELECT * FROM World WHERE id = ?"; | ||
private static final int DB_ROWS = 10000; | ||
|
||
private DataSource mysqlDataSource; | ||
|
||
public DbMysqlController(DataSource dataSource) { | ||
super(); | ||
this.mysqlDataSource = dataSource; | ||
} | ||
|
||
public Object read(Request request, Response response) throws SQLException { | ||
final int random = 1 + ThreadLocalRandom.current().nextInt(DB_ROWS); | ||
|
||
World world = null; | ||
|
||
// Fetch some rows from the database. | ||
try (Connection conn = mysqlDataSource.getConnection()) { | ||
try (PreparedStatement statement = conn.prepareStatement(DB_QUERY, | ||
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) { | ||
statement.setInt(1, random); | ||
try (ResultSet results = statement.executeQuery()) { | ||
results.next(); // Here the expectation is ONLY one | ||
// result row | ||
world = new World(results.getLong("id"), results.getInt("randomNumber")); | ||
} | ||
} | ||
} | ||
return world; | ||
} | ||
} |
64 changes: 0 additions & 64 deletions
64
frameworks/Java/restexpress/src/main/java/hello/controller/MongodbController.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.