When getting the DataSource (Or BasicDataSource in my case) from tomcat's
JNDI/JDBC service
does Tomcat manage the connection pooling itself.
Essentially yes. You still have to be sure you close your database objects, but the rest is done by DBCP.
Do I just need to retrieve the DataSource and then get the Connection and
close the Connection
after usage?
Yes. Implement finally blocks on your try/catch exception management to guarantee they are closed and returned to the database pool regardless of outcome.
Or do I have to include a few lines of complicated as above as well?
Nope. The BasicDataSourceFactory class will handle that stuff for you. The code you cite below looks like it's for when you are creating the pool and managing it yourself, which you aren't doing in this case.

Also you didn't mention it, but I'm assuming your one and only copy of the DBCP jar file is in common/lib for tomcat 5 or /lib for tomcat 6, right?

--David

jerrycat wrote:
I have configured my web app to use tomcat's connection pooling.

and also I have modfied the default value of the factory attribute:
factory="org.apache.commons.dbcp.BasicDataSourceFactory"

Here is the context.xml
<Context>
<Resource name="jdbc/testDB" factory="org.apache.commons.dbcp.BasicDataSourceFactory" auth="Container" type="javax.sql.DataSource" maxActive="10" maxIdle="5" maxWait="10000" removeAbandoned="true"
                removeAbandonedTimeout="60"
                logAbandoned="true"
username="username" password="password" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test" />
</Context>


Here is how I retrieve the data source so that I later can ask for a
connection:


        private static BasicDataSource ds = null;

        public static DataSource getDataSource() throws SQLException {
                if (ds == null) {
                        try {
                                final Context initContext = new 
InitialContext();
                                ds = 
(BasicDataSource)initContext.lookup("java:/comp/env/jdbc/testDB");
                                initContext.close();
                                logDataSource(ds);
                                return ds;
                        } catch (final NamingException e) {
                                e.printStackTrace();
                                throw new RuntimeException("Java naming 
exception when getting
connection from tomcat pool: " + e.getMessage());
                        }
                } else {
                        logDataSource(ds);
                        return ds;
                }
    }



I have read somewhere that you have to create a PoolingDataSource if you
want
use connection pooling.

Here is a sample code that I have found:
GenericObjectPool connectionPool = new GenericObjectPool(null);
DriverManagerConnectionFactory connectionFactory = new
DriverConnectionFactory("jdbc:some:connect:string",null);
PoolableConnectionFactory poolableConnectionFactory = new
PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
PoolingDataSource dataSource = new PoolingDataSource(connectionPool);



Question:
When getting the DataSource (Or BasicDataSource in my case) from tomcat's
JNDI/JDBC service
does Tomcat manage the connection pooling itself.

Do I just need to retrieve the DataSource and then get the Connection and
close the Connection
after usage?

Or do I have to include a few lines of complicated as above as well?


Thanks!



--
David Smith
Programmer/Analyst
College of Agriculture and Life Sciences
Cornell University
B32 Morrison Hall
Ithaca, NY 14853
Phone: (607) 255-4521


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

Reply via email to