tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "daad\@libero\.it" <d...@libero.it>
Subject Re: Connection pooling, is it the right way to do it?
Date Sun, 09 Sep 2007 12:43:58 GMT
> Hi.
> 
> 1. Your docBase and path attributes are at best optional. As long as 
> your context.xml is delivered in your webapp's META-INF folder, it 
> should be used automagically.
> 

So context.xml and web.xml are fine? And yes, both are dployed with the application

> 2. I would trade the autoReconnect=true parameter to the database url 
> with a validationQuery attribute in the <Resource .../> element. That 
> tells the db pool to do use the query to do a quick test of the 
> connection (and possibly recreate it) before you get it.
> 

So i should remove autoReconnect=true from url parameter and add the validationQuery parameter?
Which value should i assign to validationQuery?

> 3. I see a lot of places where you catch and summarily eat the exception 
> with no logging or even passing it up to the method's caller. If you 
> don't pass it up and it's significant to the response, at least log it. 
> If you are throwing a new exception in response to an underlying 
> exception, pass the root cause along.

I didn't paste the whole code, just the structure of it... there are logs lines in those catches
:)
 
> 4. If the Database class is truly stateless (no class instance 
> variables) like what's listed, static methods are fine. However, I would 
> personally keep the DataSource object around after the first access in 
> some manner as a performance optimization for subsequent method calls. 
> Maybe something to the effect of:
> 
> db = new Database() ;
> Object something = db.getSomething() ;
> Object something2 = db.getSomeOtherThing() ;

In this way, i would keep the db object around but the DataSource (according to my code) would
be accessed each time due to the getConnection() method that is in each method of Database
class, isn't it?

> 
> In that case, store the DataSource object in the Database class and 
> don't make any methods static.
> 
> Otherwise it looks good. Have you tried the code?
> 

Yes, code is already in use but since i had some problems with the previous installation of
tomcat (after some days connection pooling collapsed) i just wonder if the problem was the
connection pooling or the bad tomcat installation (now i reinstalled it and seems acting in
a better way).

Anyway, in the way it is, it shouldn't cause any problem in connection pooling, i mean, the
code as it is, could cause any connection problem after some day/weeks of use? Or the pool
is handled in the right way?
Thanks


> --David
> 
> daad@libero.it wrote:
> > Hi, i have done a server web application that runs on tomcat.
> > The server handle more clients and since i have to access to db, i tried to implement
the connection pooling.
> > Since i had some problems with tomcat (crashes after some days telling thatthe connection
pool was full, or something like it), i wish to be sure i'm doing the right things to use
the connection pool.
> > Anyway, after reinstall of tomcat, it seems to work a lot better than before, but
i still wish to know if the way i do it has sense.
> >
> > The configuration of context.xml file is:
> >
> > <Context path="/SomeApp" docBase="SomeApp" debug="5" reloadable="true" crossContext="true">
> >     <Resource
> >     name="jdbc/SomeAppDB"
> >     auth="Container"
> >     type="javax.sql.DataSource"
> >     maxActive="300"
> >     maxIdle="30"
> >     maxWait="5000"
> >     username="user"
> >     password="pass"
> >     driverClassName="com.mysql.jdbc.Driver"
> >     url="jdbc:mysql://127.0.0.1:3306/db_db?autoReconnect=true"/>
> > </Context>
> >
> >
> > The setting in web.xml are:
> >
> > ...
> >   <resource-ref>
> >     <description>DB Connection</description>
> >     <res-ref-name>jdbc/SomeAppDB</res-ref-name>
> >     <res-type>javax.sql.DataSource</res-type>
> >     <res-auth>Container</res-auth>
> >   </resource-ref>
> > ...
> >
> >
> > then i have a class with all methods that access to db, and it's like:
> >
> > class Database {
> >  
> >   private Connection getConnection() throws Exception {
> >     // get context: provides the starting point for resolution of names
> >     Context ctx = new InitialContext();
> >     if (ctx == null) {
> >       throw new Exception("No Context");
> >     }
> >     // retrieve datasource
> >     DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/SomeAppDB");
> >     if (ds == null) {
> >       throw new Exception("No Datasource");
> >     }
> >     // return db connection
> >     return ds.getConnection();
> >   }
> >  
> >   public Object getSomething() {
> >     Connection con = null;
> >     Statement stmt = null;
> >     ResultSet rst = null;
> >     try {
> >       // get connection
> >       con = getConnection();
> >       if (con == null) {
> >         throw new Exception("No Connection");
> >       }
> >       stmt = con.createStatement();
> >       // perform query
> >       rst = stmt.executeQuery("SELECT something FROM some_table WHERE id = '1'");
> >       if (rst.next()) {
> >         return rst.getString("something");
> >       }
> >       else {
> >         throw new Exception("No results");
> >       }
> >     }
> >     catch (Exception e1) {
> >       return null;
> >     }
> >     // close resources
> >     finally {
> >       try {
> >         rst.close();
> >         rst = null;
> >       }
> >       catch (Exception e2) {
> >       }
> >       try {
> >         stmt.close();
> >         stmt = null;
> >       }
> >       catch (Exception e3) {
> >       }
> >       try {
> >         con.close();
> >         con = null;
> >       }
> >       catch (Exception e4) {
> >       }
> >     }
> >     return null;
> >   }
> >  
> >   public Object getSomeOtherThing() {
> >     Connection con = null;
> >     Statement stmt = null;
> >     ResultSet rst = null;
> >     try {
> >       // get connection
> >       con = getConnection();
> >       if (con == null) {
> >         throw new Exception("No Connection");
> >       }
> >     ...
> >   }
> >  
> > ...
> >
> >
> > From all other classes of the application, i just use:
> >
> > (new Database()).getSomething();
> >
> >
> > to call the method that access to db that i need.
> > Is it the correct way to use the connection pooling?
> > Database class should be done in a different way? For example, methods inside it,
should be static so i don't need to create each time a Database object just to call one?
> > Any suggestion about it is very appreciated. Thanks
> >
> >
> > ------------------------------------------------------
> > Leggi GRATIS le tue mail con il telefonino i-modeTM di Wind
> > http://i-mode.wind.it/
> >
> >
> > ---------------------------------------------------------------------
> > 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
> 


------------------------------------------------------
Leggi GRATIS le tue mail con il telefonino i-modeĀ™ di Wind
http://i-mode.wind.it/


---------------------------------------------------------------------
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