On Mar 31, 2014, at 7:05 AM, Saurabh Saraswat <ssaras...@pivotalindia.com> 
wrote:

> Dear All,
> 
> I am doing connection pooling with tomcat 6. And i am doing this very first
> time before today i had no idea about connection pooling. I want to ensure
> that it is the correct way or not.
> Please do me correct if i am doing wrong anywhere. I am explaining you all
> steps done by me-
> 
> *1. Have created a context.xml*

Using “conf/context.xml” works, but it will create the resource that you define 
for every application that you deploy to Tomcat.  Sometimes this is the desired 
effect and sometimes this ends up creating a lot of extra pools that are not 
needed.

If you want to create a pool for one app, you can put it in 
“conf/Catalina/localhost/<app>.xml” or inside your WAR file at 
“META-INF/context.xml”.  These are locations for context configuration that is 
specific to an application and when resources are placed in one of these two 
locations (don’t put them in both), the resource will only be created once, for 
the specific app.

Alternatively, you can put your resource definitions in “conf/server.xml” 
(inside the GlobalNamingResources block) and it’ll allow you to create one pool 
and share it across multiple applications.  The nice thing about this approach 
is that with multiple applications using the same pool, you can generally use 
your connections more efficiently.

Which one you pick depends on your environment and what makes sense there.

> 
> <?xml version="1.0" encoding="UTF-8"?>
> 
> <Context>
>  <Resource name="jdbc/MaxDB" auth="Container" type="javax.sql.DataSource"
>               maxActive="100" maxIdle="30" maxWait="10000"
>               username="root" password=“root"
> driverClassName="com.mysql.jdbc.Driver"
> 
> url="jdbc:MySQL://localhost:3306/MaxDB?zeroDateTimeBehavior=convertToNull"/>
> 
> </Context>

Looks OK.

> 
> *2. Mapping in web.xml*
> 
> <resource-ref>
> <description>MySql DataSource</description>
> <res-ref-name>jdbc/MaxDB</res-ref-name>
> <res-type>javax.sql.DataSource</res-type>
> <res-auth>Container</res-auth>
> </resource-ref>

I don’t believe that this is needed by Tomcat.

> 
> *3. Then on my servlet i am getting the object of connection like this-*
> 
>    private static InitialContext ic;
>    protected static DataSource datasource;
>    private static Context ctx;
> 
>   protected static Connection getConnection() throws DatabaseException
> {
>        Connection conn = null;
>        try
>        {
>            ctx = new InitialContext();
>            datasource = (DataSource)
> ctx.lookup("java:/comp/env/jdbc/MaxDB");
>            conn = datasource.getConnection();
>        }

I didn’t run this code, but at a glance it looks OK.

>        catch (Exception ex)
>        {
> 
>        }
> 
>        return conn;
>    }
> 
> Is that it or we need to do anything else for connection pooling. As i
> google then i found there is an API Commons DBCP so tomcat use it
> internally or we have to do something with this.

Yes.  Tomcat will use DBCP internally.  There’s nothing additional you need to 
do, just define your resources.

If you want to use a different connection pool, you can do that.  You just need 
to specify the “factory” attribute and the class name of the factory to use to 
create the pool.  Another commonly used pool is Tomcat’s jdbc-pool, which ships 
as a second option in Tomcat 7.

> Using this i am able to get the connection object.But at the second request
> how i will validate that its taking the connection object from pool and not
> creating the new con object. Even i am not sire that here i am using
> connection pooling or getting object of connection simply using datasource.

You can connect with jconsole / jvisualvm and look at the mbeans.  Tomcat 
exports mbeans for the resources that you define.  Through them you can see the 
stats for your connection pool.

Dan

> 
> Please assist me!
> 
> Thanking You!
> 
> *Best Regards,    *
> 
> *Saurabh Sarasvat*


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to