-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Saurabh,

On 3/31/14, 7:05 AM, Saurabh Saraswat wrote:
> 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.

Glad to see you are taking a step in writing a scalable web
application. Connection pooling helps you scale better by both
reducing the time it takes to connect to your database and also by
limiting the number of concurrent connections to the database (which
keeps it healthy).

> 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*
> 
> <?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>
> 
> *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>
> 
> *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(); } catch (Exception ex) {
> 
> }
> 
> return conn; }

Looks good so far. When you're done with the Connection object, just
call close() on it and it will be returned to the pool. Make sure to
do it in a "finally" block. (I have lots more tips for JDBC
connections on an old blog post I wrote which can be found here:
http://blog.christopherschultz.net/index.php/2009/03/16/properly-handling-pooled-jdbc-connections/)

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

You do not need to use any special APIs beyond what you have above:
the JNDI stuff and JDBC.

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

MySQL/Maria support the SLEEP() function in queries. Try this.
Configure your pool with *only* a single connection (which you should
always do in development anyway), then issue a "SELECT SLEEP(3)" from
your web application. While the thread is sleeping, do a "SHOW
PROCESSLIST" from the MySQL CLI. You'll see the connection id along
with the query being executed.

Then when the request completes (after ~3 seconds), re-run "SHOW
PROCESSLIST" to convince yourself that there are no queries running.
Then, make the request again to execute the "SELECT SLEEP(3)" and do
"SHOW PROCESSLIST" in the MySQL CLI again: you should see the same
connection id running the query.

You could also try to load-test your web application with a single
connection in the pool and then go ask MySQL to SHOW PROCESSLIST, and
you should see only a single query executing at a time.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJTOdppAAoJEBzwKT+lPKRYXioP/jza7PD2N+O0Y7VBCnA7UDob
aAkF0boQ242ZtZQh2LkdRISxSp03ZO/8x+w504so/hTJ/1nxHvl+RvRhuDqfaxYP
pzA5MjxN72h2NGf5IYNVmALNZCJI8HlC45UDQ762VnHAGy6ZKv4HZUcvKhkR++Xl
BPtVAUO6g9AI8BCbe+j9fgHpwMCd12KyD3bxFFUxLh1ZP1Y7FH8gThHyE9d0NzP8
iuJgaLvIs/Be1OAlogq95H45d4sO7MNMPqo4OsDafW4RNOAOHFfVGx5IIp2vVVRJ
QABUzZNUiQakCmOTJu5q/NMx6PpN71qxvakVofAk00ZzT9wzbuHZmE3vTnBH1gTQ
eyY6mJskqv8jKKeogtCtpeeLEPQxLpy/fWL289jsFJSDIq3HZJWkKcIbbOywjLnP
X7PQ/wAACQ4sm0ieUrz/Vytd9k59+bjiW/Q0GzmZvRPf7IZhHcjsY4Q/rIRRo7Y4
f1V8llfOdfDSv1kaD0oD1dsLoTEmsWGMKK05PoF4EcXRTpBxbNpg5H5cTLmuLHSr
U8zHnHayHJ/rJcj+raeYTj/dqLhYnTZGyCnn7gNdQZJssCoTzeUU7Ay8kw4gDghY
Vr1fbbz7XOz9cO9vm0M/ornb5DvU3QbFamQgymTjRUvLCQ9eMhsyH9qT5OtCh6PR
MIdAkqbnmCTSlqHMrPdj
=xz7b
-----END PGP SIGNATURE-----

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

Reply via email to