我写了一个servlet并在tomcat6上运行它。当有一个查询发送到指定的DNS时,servlet将获取信息并使用它们在数据库中搜索(mysql),然后写回一些东西。我在tomcat6中使用连接池。但是,当我尝试对我的程序进行负载测试时,我发现吞吐量非常低,并且很多例外情况如
Response code: Non HTTP response code:
java.net.SocketTimeoutException Non HTTP response message: Read timed
out
Your result:
Team and Account info\n at java.net.SocketInputStream.socketRead0(Native Method); at
java.net.SocketInputStream.read(SocketInputStream.java:152); at
java.net.SocketInputStream.read(SocketInputStream.java:122); at
org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:166); at
org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:90); at
org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:281); at
org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:92); at
org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:61); at
org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254); at
org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289); at
org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252); at
org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:191); at
org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300); at
org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127); at
org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:715); at
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:520); at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906); at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805); at
org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.executeRequest(HTTPHC4Impl.java:475); at
org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:295); at
org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:74); at
org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1105); at
org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1094); at
org.apache.jmeter.threads.JMeterThread.process_sampler(JMeterThread.java:429); at
org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257); at
java.lang.Thread.run(Thread.java:745)
有人可以给我一些建议吗?这是我的servlet代码,这是我在这个web项目中使用的唯一Java代码,谢谢
@SuppressWarnings("serial")
public class Servlet extends HttpServlet {
@Override
public void doGet(final HttpServletRequest req, final HttpServletResponse res) throws IOException {
Connection conn = null;
DataSource ds = null;
InitialContext ctx;
try {
ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
conn = ds.getConnection();
final Statement stmt = conn.createStatement();
stmt.executeQuery("SET NAMES 'utf8mb4'; ");
} catch (final SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
res.setContentType("text/plain;charset=UTF-8");
final PrintWriter pw = res.getWriter();
try {
final Statement stmt = conn.createStatement();
String teamInfo = "aa\n";
ResultSet rs = null;
final String info = req.getQueryString();
final String[] sp = info.split("&");
final String[] child0 = sp[0].split("=");
final String[] child1 = sp[1].split("=");
final String sqlString = "select * from tweets where timeandid=\"" + child1[1] + child0[1] + "\"";
rs = stmt.executeQuery(sqlString);
rs.next();
final String a = rs.getString(2);
teamInfo = teamInfo + rs.getString(3) + ":" + rs.getString(4).trim() + ":" + a + "\n";
pw.print(teamInfo);
pw.flush();
rs.close();
stmt.close();
} catch (final SQLException ex) {
System.out.println(ex);
} finally {
System.out.println("over");
}
pw.close();
try {
conn.close();
} catch (final SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}