tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Smith <d...@cornell.edu>
Subject Re: jsp optimization for db driver load and connection
Date Mon, 04 Dec 2006 19:23:12 GMT
You'll have to do something because the below code is very inefficient.  
Creating and closing connections is an extremely expensive operation and 
the way this is written you're getting one on every request.

At minimum, I would recommend a ServletContextListener that creates a 
pooled DataSource, stored in the application scope.  Then convert your 
code below to get the DataSource from there whenever it needs it.  The 
difference will be day and night.  Oh, yes ... and make sure you always 
close the connections as soon as you can after you're finished using 
them -- preferably in a finally block.

--David

IT Desk wrote:

> This isn't Tomcat specific but general to any jsp container and its jvm.
>
> I am working on a site where almost everything is done through the jsp 
> page plus one main java class to store state data.
>
> The site's jsp page may do up to 7 queries on the database. On each 
> query, the statements are these:
>
> Driver DriverDB = (Driver)Class.forName(db_DRIVER).newInstance();
> Connection ConnDB = 
> DriverManager.getConnection(db_STRING,db_USERNAME,db_PASSWORD);
> PreparedStatement StatementDB = ConnDB.prepareStatement("SELECT * from 
> table");
> ResultSet resultDB = StatementDB.executeQuery();
>
> My 2 questions are:
> Does the forName call to load the driver get optimized out? Clearly 
> the driver need only load once.
> Does the getConnection reuse the same connection that was done in the 
> previous call on the same jsp page?
>
> There are some performance problems and I'm wondering if I should try 
> to clean the code up or if the jvm
> does it for me through optimization. It's running on Tomcat 5.5.20 and 
> JVM 5.x.
>
> The client won't pay for any major redesign so I'm looking for 
> something small that could make a big impact.
>
> Thanks for any insight.
> Coral
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message