Monday 15 March 2010

tomcat - Why does my server crash when I run more than 1 almost identicall java servlets? -



tomcat - Why does my server crash when I run more than 1 almost identicall java servlets? -

i running 2 java (7) servlets on tomcat 7 vps server. 1 servlet returns json response , other servlet returns 4 lines of pure html code.

if run json response servlet, have no problems handling 12+ 1000000 request per day (~140 request per second).

currently run half traffic on json servlet (so ~70 request per second).

if add together servlet returns html server response time explodes , returns errors when number of requests not reaches 20 per sec servlet. (so total amount of requests ~90 per second).

however if requests html servlet drop ~2 per min fine. looks servlet doesn't have errors.

the 2 servlets running are:

// loading required libraries import java.lang.*; import java.io.*; import javax.servlet.*; import javax.servlet.annotation.webservlet; import javax.servlet.http.*; import javax.sql.datasource; import javax.naming.*; import java.util.random; import java.math.*; import java.security.*; import java.sql.*; /** * servlet implementation class test */ @webservlet("/html") public class html extends httpservlet{ private static final long serialversionuid = 12l; public html() { super(); // todo auto-generated constructor stub } private datasource datasource; public void init() throws servletexception { seek { // datasource context initcontext = new initialcontext(); context envcontext = (context)initcontext.lookup("java:/comp/env"); datasource = (datasource)envcontext.lookup("jdbc/testdb"); system.out.println("obtained cached info source "); } grab (namingexception e) { e.printstacktrace(); } } public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // parameters request. string source = null; string ua = null; string id = null; string ip = null; source = request.getparameter("source"); ua = request.getheader("user-agent"); //is client behind something? ip = request.getheader("x-forwarded-for"); if (ip == null) { ip = request.getremoteaddr(); } connection conn = null; statement stmt = null; int exit = 0; // set response content type response.setcontenttype("text/html"); printwriter out = response.getwriter(); try{ // open connection conn = datasource.getconnection(); stmt = conn.createstatement(); // execute sql query stmt = conn.createstatement(); string sql; sql = "select info db"; resultset rs = stmt.executequery(sql); rs.next(); string url = rs.getstring("url"); string url_src = rs.getstring("url_src"); out.println("<html>"); out.println("<body>"); out.println("<a href=\"url\">"); out.println("<img src=\"url_src\"/></a>"); out.println("</body></html>"); long time = system.currenttimemillis()/1000; random randomgenerator = new random(); int randomint = randomgenerator.nextint(250); string toenc = ip + time + ua + randomint; // value hash messagedigest mdenc = messagedigest.getinstance("md5"); mdenc.update(toenc.getbytes(), 0, toenc.length()); id = new biginteger(1, mdenc.digest()).tostring(16);// hashed sql = "insert request"; stmt.execute(sql); // clean-up environment rs.close(); stmt.close(); conn.close(); }catch(sqlexception se){ //handle errors jdbc se.printstacktrace(); }catch(exception e){ //handle errors class.forname e.printstacktrace(); }finally{ //finally block used close resources try{ if(stmt!=null) stmt.close(); }catch(sqlexception se2){ }// nil can try{ if(conn!=null) conn.close(); }catch(sqlexception se){ se.printstacktrace(); }//end seek } //end try*/ } }

and sec servlet is:

// loading required libraries import java.lang.*; import java.io.*; import javax.servlet.*; import javax.servlet.annotation.webservlet; import javax.servlet.http.*; import javax.sql.datasource; import javax.naming.*; import java.util.random; import java.math.*; import java.security.*; import java.sql.*; /** * servlet implementation class test */ @webservlet("/json") public class json extends httpservlet{ private static final long serialversionuid = 11l; public json() { super(); // todo auto-generated constructor stub } private datasource datasource; public void init() throws servletexception { seek { // datasource context initcontext = new initialcontext(); context envcontext = (context)initcontext.lookup("java:/comp/env"); datasource = (datasource)envcontext.lookup("jdbc/testdb"); system.out.println("obtained cached info source "); } grab (namingexception e) { e.printstacktrace(); } } public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // parameters request. string source = null; string ua = null; string id = null; string ip = null; source = request.getparameter("source"); ua = request.getparameter("ua"); ip = request.getparameter("clientip"); connection conn = null; statement stmt = null; int exit = 0; // set response content type response.setcontenttype("application/json"); printwriter out = response.getwriter(); try{ // open connection conn = datasource.getconnection(); stmt = conn.createstatement(); // execute sql query stmt = conn.createstatement(); string sql; sql = "select * db"; resultset rs = stmt.executequery(sql); rs.next(); string url = rs.getstring("url"); string url_src = rs.getstring("url_src"); string jsonresponse = "{\"url\":\"url\","\"media_url\":\"url_src\"}"; out.println(jsonresponse); long time = system.currenttimemillis()/1000; random randomgenerator = new random(); int randomint = randomgenerator.nextint(250); string toenc = ip + time + ua + randomint; // value hash messagedigest mdenc = messagedigest.getinstance("md5"); mdenc.update(toenc.getbytes(), 0, toenc.length()); id = new biginteger(1, mdenc.digest()).tostring(16);// hashed sql = "insert request"; stmt.execute(sql); // clean-up environment rs.close(); stmt.close(); conn.close(); }catch(sqlexception se){ //handle errors jdbc se.printstacktrace(); }catch(exception e){ //handle errors class.forname e.printstacktrace(); }finally{ //finally block used close resources try{ if(stmt!=null) stmt.close(); }catch(sqlexception se2){ }// nil can try{ if(conn!=null) conn.close(); }catch(sqlexception se){ se.printstacktrace(); }//end seek } //end try*/ } }

i have setup connection pool jdbc. , have next config.xml file:

<?xml version='1.0' encoding='utf-8'?> <!-- licensed apache software foundation (asf) under 1 or more contributor license agreements. see notice file distributed work additional info regarding copyright ownership. asf licenses file under apache license, version 2.0 (the "license"); may not utilize file except in compliance license. may obtain re-create of license @ http://www.apache.org/licenses/license-2.0 unless required applicable law or agreed in writing, software distributed under license distributed on "as is" basis, without warranties or conditions of kind, either express or implied. see license specific language governing permissions , limitations under license. --> <!-- contents of file loaded each web application --> <context> <!-- default set of monitored resources --> <watchedresource>web-inf/web.xml</watchedresource> <!-- uncomment disable session persistence across tomcat restarts --> <!-- <manager pathname="" /> --> <!-- uncomment enable comet connection tacking (provides events on session expiration webapp lifecycle) --> <!-- <valve classname="org.apache.catalina.valves.cometconnectionmanagervalve" /> --> <resource name="jdbc/testdb" auth="container" type="javax.sql.datasource" driverclassname="com.mysql.jdbc.driver" url="jdbc:mysql://localhost:3306/database" username="username" password="password" maxactive="250" maxidle="60" minidle="30" maxwait="10000" poolpreparedstatements="true" maxopenpreparedstatements="100" /> </context>

i have set envirenment variables or java_opts in file /tomcat/bin/setenv.sh:

export catalina_opts="-djava.library.path=/usr/local/apr/lib" normal="-d64 -xmx1536m -xms512m -server" max_perm_gen="-xx:maxpermsize=512m" headless="-djava.awt.headless=true" java_opts="$normal $max_perm_gen $headless" export java_opts

my vps has 2gb of ram, 4 cpu cores of 2ghz. have tried changing heap size or maxpermsize not seem matter.

can explain me doing wrong configuration? don't understand why server handle much load 1 servlet , crashes when traffic split on another.

thanks

hagubear info, problem in couple of out.println() statements. in thread [1]: do not utilize system.out.println in server side code

i found using bad practice. placed in 1 out.println() , server works much better.

thanks

java tomcat servlets config

No comments:

Post a Comment