tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chris Berthold" <CBerth...@terrafirma-productions.com>
Subject RE: jsp optimization for db driver load and connection
Date Mon, 04 Dec 2006 19:49:39 GMT
My suggestions are as follows:

1. Create a ServletContextListener and load the Class.forName in the
contextInitialized method.  I'm not 100% sure of the performance penalty
on your creating the driver object over and over.  I'm going to assume
it's a static object so it only initializes once but don't hold my word
for it.  

2. If you do not setup a connection pool of any kind you are going to
experience the expense of creating new connections over and over. If you
were to optimize what you have without adding functionality you can
reuse the connection for the length of the request.  Do not reuse the
connection across a session or you will undoubtedly end up with memory
leaks.  You will generally see something along the lines of the
following example (minus the try/catch/finally's and a bit of shorthand)

Connection conn = null;
PrepStmt stmt = null;
ResultSet rs = null;

Conn = DriverManager.getConnection();

stmt = conn.prepstmt("SELECT * FROM data1");
rs = stmt.executeQuery();
rs.close();
stmt.close();

stmt = conn.prepstmt("SELECT * FROM data2");
rs = stmt.executeQuery();
rs.close();
stmt.close();

conn.close();

rs = null;
stmt = null;
conn = null;

The close statements end up in the finally block and make sure they are
not null.  There are plenty of examples of this on the web.  Just by
cutting down on the number of times the connection is created
(getConnection()) will improve the app performance tremendously.  I
would focus on that first.  That quick change will be your first
increase in performance.

Hope this helps some.

Chris Berthold


-----Original Message-----
From: IT Desk [mailto:ask@dataeffects.com] 
Sent: Monday, December 04, 2006 2:04 PM
To: Tomcat Users List
Subject: jsp optimization for db driver load and connection

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