【发布时间】:2015-09-23 21:22:07
【问题描述】:
我正在使用 Java 的 playframework (2.4) 并将其连接到 Postgres。 play 框架被用作一个 RESTful 服务,它所做的只是使用 JDBC 进行插入、更新、读取和删除。在这个播放页面https://www.playframework.com/documentation/2.3.x/JavaAsync 上,它清楚地指出 JDBC 是阻塞的,并且播放的线程很少。对于知道这一点的人来说,这有多大的限制,有什么办法可以解决这个问题吗?我的特定应用每秒可以有几百个数据库调用。我将拥有所有的硬件和额外的服务器,但不知道 play 如何处理这个或扩展以在代码中处理这个。我的代码如下所示:
public static Result myprofile() {
DynamicForm requestData = Form.form().bindFromRequest();
Integer id = Integer.parseInt(requestData.get("id"));
try {
JSONObject jo = null;
Connection conn = DB.getConnection();
ResultSet rs;
JSONArray ja = new JSONArray();
PreparedStatement ps = conn.prepareStatement("SELECT p.fullname as fullname, s.post as post,to_char(s.created_on, 'MON DD,YYYY') as created_on,s.last_reply as last_reply,s.id as id,s.comments as comments,s.state as state,s.city as city,s.id as id FROM profiles as p INNER JOIN streams as s ON (s.profile_id=p.id) WHERE s.profile_id=? order by created_on desc");
ps.setInt(1, id);
rs = ps.executeQuery();
while (rs.next()) {
jo = new JSONObject();
jo.put("fullname", rs.getString("fullname"));
jo.put("post", rs.getString("post"));
jo.put("city", rs.getString("city"));
jo.put("state", rs.getString("state"));
jo.put("comments", rs.getInt("comments"));
jo.put("id", rs.getInt("id"));
jo.put("last_reply", difference(rs.getInt("last_reply"), rs.getString("created_on")));
ja.put(jo);
}
JSONObject mainObj = new JSONObject();
mainObj.put("myprofile", ja);
String total = mainObj.toString();
System.err.println(total);
conn.close();
return ok(total);
} catch (Exception e) {
e.getMessage();
}
return ok();
}
我也知道我可以尝试将其包装在期货承诺中,但仍然会发生阻塞。如前所述,我将处理所有服务器和其他东西,但是播放框架是否能够使用 jdbc 扩展到每秒数百个请求?我现在正在询问和学习,以避免以后出现严重错误。
【问题讨论】:
-
拥有多个阻塞线程应该没问题,每个线程都用于一个 JDBC 连接。
-
哦,好吧,我的印象是 play 有 4 个线程,并且每秒每个请求都绑定一个。
-
您需要在不同的线程轮询上执行阻塞 IO 操作。
标签: java jdbc playframework playframework-2.3