You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Seva Popov <se...@tvworks.com> on 2007/06/14 01:04:01 UTC

[DBCP] The PerUserPoolDataSource/SharedPoolDataSource does not reuse/pool the db connections

Hi,

I am using Tomcat 5.5.9 and have configured
OracleConnectionPoolDataSource as the physical connection to the
database and DBCP's PerUserPoolDataSource as the pool of logical
connections to the same database (i.e. using the mentioned
OracleConnectionPoolDataSource).

Here is the configuration of my PerUserPoolDataSource (note the pool
size):

<Resource name="jdbc/MyPool"
        auth="Container"
 
type="org.apache.tomcat.dbcp.dbcp.datasources.PerUserPoolDataSource"
factory="org.apache.tomcat.dbcp.dbcp.datasources.PerUserPoolDataSourceFa
ctory"
        dataSourceName="java:comp/env/jdbc/MyCPDS"
        maxActive="2"
        maxIdle="2"
        maxWait="5"/>

Now, I am using the below test JSP page to see that the connections are
reused from the pool:

<jsp:directive.page import="javax.naming.Context" />
<jsp:directive.page import="javax.naming.InitialContext" />
<jsp:directive.page import="javax.sql.DataSource" />
<jsp:directive.page import="java.sql.Connection" />
  <html>
    Hello
<%
Context initContext = new InitialContext();
Context envContext  = (Context) initContext.lookup("java:/comp/env");

DataSource ds = (DataSource) envContext.lookup("jdbc/MyPool");
System.out.println("DataSource: " + ds);

Connection conn1 = ds.getConnection("user", "psw ");
System.out.println("Connection 1: " + conn1);

Connection conn2 = ds.getConnection("user", "psw ");
System.out.println("Connection 2: " + conn2);

conn2.close(); // closing this connection to return it to the pool

Connection conn3 = ds.getConnection("user", "psw ");
System.out.println("Connection 3: " + conn3); 

Conn1.close();
conn3.close();
%>
</html>

Here is the output (and as you see all connections are different
objects):
DataSource:
org.apache.tomcat.dbcp.dbcp.datasources.PerUserPoolDataSource@513bb5
Connection 1: oracle.jdbc.driver.OracleConnection@1bdedf8
Connection 2: oracle.jdbc.driver.OracleConnection@15daaef
Connection 3: oracle.jdbc.driver.OracleConnection@572554

My questions:
1. Firstly, I've expected to get the same connection object (as for the
connection #2) when I've obtained the 3thd connection because the pool
max size is "2", the connection #1 is still active at the moment, and
the connection #2 is closed and supposed to be returned to the pool.
2. Secondly, I've expected to get the DBCP's logical
org.apache.tomcat.dbcp.dbcp.PoolableConnection but I've got the physical
Oracle connection instead.

I've got the same results with the DBCP's SharedPoolDataSource.

So, for me it seems that neither SharedPoolDataSource nor
PerUserPoolDataSource are really keeping and reusing the connections in
the pool of database connections.

Just to be complete with my testing I've verified that the Tomcat's
default DBCP's BasicDataSource does reusing the connections from the
pool. I.e. when I've configured BasicDataSource and used the similar
test JSP page I saw the output as expected like this (the
PoolableConnections #2 and #3 are the same i.e. they are reused):

DataSource: org.apache.tomcat.dbcp.dbcp.BasicDataSource@12a4caf
Connection 1: org.apache.tomcat.dbcp.dbcp.PoolableConnection@12b7083
Connection 2: org.apache.tomcat.dbcp.dbcp.PoolableConnection@13a8045
Connection 3: org.apache.tomcat.dbcp.dbcp.PoolableConnection@13a8045

Am I missing something here?

Thanks,
Seva 

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


RE: [DBCP] The PerUserPoolDataSource/SharedPoolDataSource does not reuse/pool the db connections

Posted by Seva Popov <se...@tvworks.com>.
Phil,
 
First of all, let me say thank you for your time and for the most useful detailed responses. I've just realized that you are one of the DBCP's project developers and it's nice to know that you guys care about the project and its users.
 
Now, I understand how the DBCP's InstanceKeyDataSource pool is communicating with the CPDS implementation. My previous understanding of the contract between a pool manager and a CPDS was wrong but I guess I got it right now. The provided links to the Oracle documentation proved to be very useful and explanatory as well.
 
Yes, I am using an older Oracle version 9i with the ojdbc14-9i client driver jar. The main source of confusion I believe stems from the fact that in this version of Oracle the OracleDataSource and the OracleConnectionPoolDataSource both return the object of type OracleConnection when the client calls ds.getConnection(). However, in the former case the OracleConnection object represents a physical database connection but in the latter case the same OracleConnection object is a logical connection i.e. the temporary handle to the physical database connection. I guess Oracle guys realized that this was not very good and cleaned up their jdbc implementation in the later releases. Yet, it'd be interesting to decompile the OracleConnection and see how it's working, for example, look at its close() method.
 
Anyway, I've tried to check the number of physical connections open in my test. I do not have Oracle monitor installed nor could I use the ps since I am using the Oracle jdbc thin driver in my configuration (I guess you used oci driver + oci client but that does not replicate my test configuration).
 
But, concluding from the results of netstat -apn | grep 1521, I did verified that there are only 2 physical connections open to the database after I've run the test.
 
So, the pool is working correctly and all my questions are resolved. Thanks again for support.
 
--Seva

________________________________

From: Phil Steitz [mailto:phil.steitz@gmail.com]
Sent: Tue 6/19/2007 7:09 PM
To: Jakarta Commons Users List
Subject: Re: [DBCP] The PerUserPoolDataSource/SharedPoolDataSource does not reuse/pool the db connections



On 6/18/07, Seva Popov <se...@tvworks.com> wrote:
> Hi Phil,
>
> Here is the config for the physical connections:
>
>          <Resource name="jdbc/MyCPDS"
>             auth="Container"
>             type="oracle.jdbc.pool.OracleConnectionPoolDataSource"
>             factory="oracle.jdbc.pool.OracleDataSourceFactory"
>             url="jdbc:oracle:thin:@some_url:1521:some_sid"/> >
> Yes, I know about the differences between PerUserPoolDataSource and
> SharedPoolDataSource and I agree that the test for PerUserPoolDataSource
> was not valid as you've indicated and I will try it again.
>
Actually, looking at the example again, even with the defaults it
should only open two connections.  See comments below.

> However, I've used the same configuration for the SharedPoolDataSource
> with the same negative results.
>
> Also, in both cases when I've used ds.getConnection() I've obtained the
> raw Oracle connection but I've expected to get
> org.apache.tomcat.dbcp.dbcp.PoolableConnection as with the
> BasicDataSource.  Note, that I am not expecting to get
> OraclePooledConnection as you've indicated as this would be wrong as the
> OraclePooledConnection (that implements javax.sql.PooledConnection) is
> supposed to be used internally by the Pool Manager implementation. The
> OraclePooledConnection does not implement java.sql.Connection.  So, what
> the DBCP's Pool Manager should do is to wrap the OraclePooledConnection
> with the org.apache.tomcat.dbcp.dbcp.PoolableConnection (or some other
> wrapper that implements java.sql.Connection interface) and return the
> latter to the client.
>
> Is it not the case with DBCP?

InstanceKeyDataSource (which both PerUserPoolDataSource and
SharedPoolDataSource extend) does not wrap connection handles returned
by ConnectionPoolDataSource impls.  The pool maintains
PooledConnections obtained from the configured
ConnectionPoolDataSource and the InstanceKeyDataSource getConnection
method returns the result of getConnection applied to
PooledConnections in the pool.  The client should get back connection
handles provided by the driver.  The pool (actually the factory)
implements the ConnectionEventListener interface.

Tomcat 5.5.9 bundles (repackaged) commons dbcp 1.2.1.  You can see the
sources for this version of dbcp either by downloading the 1.2.1
source distribution here:
http://archive.apache.org/dist/jakarta/commons/dbcp/source/
or online via the svn viewer:
http://svn.apache.org/viewvc/jakarta/commons/proper/dbcp/tags/DBCP_1_2_1/

I am by no means an Oracle expert, but using Oracle Database 10g
Express Edition Release 10.2.0.1.0 and the client driver jar it ships
with on Fedora Core 4, tomcat 5.5.9,  I think I was able to verify
correct functioning of both PerUserPoolDataSource and
SharedPoolDataSource with your example jsp.  My output for
PerUserPoolDataSource looks like this:

DataSource: org.apache.tomcat.dbcp.dbcp.datasources.PerUserPoolDataSource@15bdc50
Connection 1: oracle.jdbc.driver.LogicalConnection@e020c9
Connection 2: oracle.jdbc.driver.LogicalConnection@39e5b5
Connection 3: oracle.jdbc.driver.LogicalConnection@117f31e

Note the difference in types from your output.  The LogicalConnections
are handles.  Looking at the Oracle monitor and also ps, I can see
that there are in fact only 2 physical connections open to the
database (at least that what it looks like to me - as I said, I am not
an Oracle expert, I am concluding this from looking at "sessions"
identified in the web monitor and ps -aux | grep Oracle on the command
line).

What version of Oracle and Oracle drivers are you using?  From the docs here:
http://download-east.oracle.com/docs/cd/A87861_01/NT817EE/java.817/a83724/connpoc2.htm
it looks like previous versions somehow used OracleConnection
instances as handles (as your output above indicates).  Can you check
the number of physical connections open somehow?

Phil

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org





RE: [DBCP] The PerUserPoolDataSource/SharedPoolDataSource does not reuse/pool the db connections

Posted by Seva Popov <se...@tvworks.com>.
inline

________________________________

From: Phil Steitz [mailto:phil.steitz@gmail.com]
Sent: Tue 6/19/2007 9:49 PM
To: Jakarta Commons Users List
Subject: Re: [DBCP] The PerUserPoolDataSource/SharedPoolDataSource does not reuse/pool the db connections



Two more comments on this.  First, by modifying the jsp to cast the ds
as a SharedPoolDataSource, you can call the getNumActive and
getNumIdle methods to verify that the pool is working correctly:

[Seva] Yes, this test verifies that the pool observes the max active and max idle config parameters. And it seems that after the connection was active it becomes idle, i.e. it is still in the pool.  But this test can not verify for sure that the pool is reusing the physical database connections. For example, one can easily break the above by closing the underlying physical connection directly in the client code. 


<jsp:directive.page
import="org.apache.tomcat.dbcp.dbcp.datasources.SharedPoolDataSource"
/>
...
SharedPoolDataSource ds = (SharedPoolDataSource)
envContext.lookup("jdbc/MyPool");
System.out.println("DataSource: " + ds);
System.out.println("Num Active init " + ds.getNumActive());
System.out.println("Num Idle init " + ds.getNumIdle());

Connection conn1 = ds.getConnection("phil", "zft0done");
System.out.println("Connection 1: " + conn1);
System.out.println("Num Active one open " + ds.getNumActive());
System.out.println("Num Idle one open " + ds.getNumIdle());
...

For PerUserPoolDataSource, you need to supply uid/pwd to the getNumXxx
methods per the javadoc to get correct counts.

For SharedPoolDataSource, I get the expected output with the setup I
described above:
DataSource: org.apache.tomcat.dbcp.dbcp.datasources.SharedPoolDataSource@1ad98ef
Num Active init 0
Num Idle init 0
Connection 1: oracle.jdbc.driver.LogicalConnection@16d2702
Num Active one open 1
Num Idle one open 0
Connection 2: oracle.jdbc.driver.LogicalConnection@1556d12
Num Active two open 2
Num Idle two open 0
Num Active 2 closed 1
Num Idle 2 closed 1
Connection 3: oracle.jdbc.driver.LogicalConnection@16be68f
Num Active 3rd open 2
Num Idle 3rd open 0
Num Active 1 closed 1
Num Idle 1 closed 1
Num Active all closed 0
Num Idle all closed 2

Second, if for some reason your version of the Oracle drivers is not
working correctly, you can use a cpds adapter.  See the Wiki example
referenced above or the package javadoc for o.a.c.dbcp.cpsdadapter for
info on how to configure this.

[Seva] Yes, I've realized the same today and was going to switch to CPDS adapter tomorrow but after I got your previous email things were clarified and I verified the Oracle driver is working correctly.

Phil

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org





Re: [DBCP] The PerUserPoolDataSource/SharedPoolDataSource does not reuse/pool the db connections

Posted by Phil Steitz <ph...@gmail.com>.
Two more comments on this.  First, by modifying the jsp to cast the ds
as a SharedPoolDataSource, you can call the getNumActive and
getNumIdle methods to verify that the pool is working correctly:
<jsp:directive.page
import="org.apache.tomcat.dbcp.dbcp.datasources.SharedPoolDataSource"
/>
...
SharedPoolDataSource ds = (SharedPoolDataSource)
envContext.lookup("jdbc/MyPool");
System.out.println("DataSource: " + ds);
System.out.println("Num Active init " + ds.getNumActive());
System.out.println("Num Idle init " + ds.getNumIdle());

Connection conn1 = ds.getConnection("phil", "zft0done");
System.out.println("Connection 1: " + conn1);
System.out.println("Num Active one open " + ds.getNumActive());
System.out.println("Num Idle one open " + ds.getNumIdle());
...

For PerUserPoolDataSource, you need to supply uid/pwd to the getNumXxx
methods per the javadoc to get correct counts.

For SharedPoolDataSource, I get the expected output with the setup I
described above:
DataSource: org.apache.tomcat.dbcp.dbcp.datasources.SharedPoolDataSource@1ad98ef
Num Active init 0
Num Idle init 0
Connection 1: oracle.jdbc.driver.LogicalConnection@16d2702
Num Active one open 1
Num Idle one open 0
Connection 2: oracle.jdbc.driver.LogicalConnection@1556d12
Num Active two open 2
Num Idle two open 0
Num Active 2 closed 1
Num Idle 2 closed 1
Connection 3: oracle.jdbc.driver.LogicalConnection@16be68f
Num Active 3rd open 2
Num Idle 3rd open 0
Num Active 1 closed 1
Num Idle 1 closed 1
Num Active all closed 0
Num Idle all closed 2

Second, if for some reason your version of the Oracle drivers is not
working correctly, you can use a cpds adapter.  See the Wiki example
referenced above or the package javadoc for o.a.c.dbcp.cpsdadapter for
info on how to configure this.

Phil

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [DBCP] The PerUserPoolDataSource/SharedPoolDataSource does not reuse/pool the db connections

Posted by Phil Steitz <ph...@gmail.com>.
On 6/18/07, Seva Popov <se...@tvworks.com> wrote:
> Hi Phil,
>
> Here is the config for the physical connections:
>
>          <Resource name="jdbc/MyCPDS"
>             auth="Container"
>             type="oracle.jdbc.pool.OracleConnectionPoolDataSource"
>             factory="oracle.jdbc.pool.OracleDataSourceFactory"
>             url="jdbc:oracle:thin:@some_url:1521:some_sid"/> >
> Yes, I know about the differences between PerUserPoolDataSource and
> SharedPoolDataSource and I agree that the test for PerUserPoolDataSource
> was not valid as you've indicated and I will try it again.
>
Actually, looking at the example again, even with the defaults it
should only open two connections.  See comments below.

> However, I've used the same configuration for the SharedPoolDataSource
> with the same negative results.
>
> Also, in both cases when I've used ds.getConnection() I've obtained the
> raw Oracle connection but I've expected to get
> org.apache.tomcat.dbcp.dbcp.PoolableConnection as with the
> BasicDataSource.  Note, that I am not expecting to get
> OraclePooledConnection as you've indicated as this would be wrong as the
> OraclePooledConnection (that implements javax.sql.PooledConnection) is
> supposed to be used internally by the Pool Manager implementation. The
> OraclePooledConnection does not implement java.sql.Connection.  So, what
> the DBCP's Pool Manager should do is to wrap the OraclePooledConnection
> with the org.apache.tomcat.dbcp.dbcp.PoolableConnection (or some other
> wrapper that implements java.sql.Connection interface) and return the
> latter to the client.
>
> Is it not the case with DBCP?

InstanceKeyDataSource (which both PerUserPoolDataSource and
SharedPoolDataSource extend) does not wrap connection handles returned
by ConnectionPoolDataSource impls.  The pool maintains
PooledConnections obtained from the configured
ConnectionPoolDataSource and the InstanceKeyDataSource getConnection
method returns the result of getConnection applied to
PooledConnections in the pool.  The client should get back connection
handles provided by the driver.  The pool (actually the factory)
implements the ConnectionEventListener interface.

Tomcat 5.5.9 bundles (repackaged) commons dbcp 1.2.1.  You can see the
sources for this version of dbcp either by downloading the 1.2.1
source distribution here:
http://archive.apache.org/dist/jakarta/commons/dbcp/source/
or online via the svn viewer:
http://svn.apache.org/viewvc/jakarta/commons/proper/dbcp/tags/DBCP_1_2_1/

I am by no means an Oracle expert, but using Oracle Database 10g
Express Edition Release 10.2.0.1.0 and the client driver jar it ships
with on Fedora Core 4, tomcat 5.5.9,  I think I was able to verify
correct functioning of both PerUserPoolDataSource and
SharedPoolDataSource with your example jsp.  My output for
PerUserPoolDataSource looks like this:

DataSource: org.apache.tomcat.dbcp.dbcp.datasources.PerUserPoolDataSource@15bdc50
Connection 1: oracle.jdbc.driver.LogicalConnection@e020c9
Connection 2: oracle.jdbc.driver.LogicalConnection@39e5b5
Connection 3: oracle.jdbc.driver.LogicalConnection@117f31e

Note the difference in types from your output.  The LogicalConnections
are handles.  Looking at the Oracle monitor and also ps, I can see
that there are in fact only 2 physical connections open to the
database (at least that what it looks like to me - as I said, I am not
an Oracle expert, I am concluding this from looking at "sessions"
identified in the web monitor and ps -aux | grep Oracle on the command
line).

What version of Oracle and Oracle drivers are you using?  From the docs here:
http://download-east.oracle.com/docs/cd/A87861_01/NT817EE/java.817/a83724/connpoc2.htm
it looks like previous versions somehow used OracleConnection
instances as handles (as your output above indicates).  Can you check
the number of physical connections open somehow?

Phil

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


RE: [DBCP] The PerUserPoolDataSource/SharedPoolDataSource does not reuse/pool the db connections

Posted by Seva Popov <se...@tvworks.com>.
Hi Phil,

Here is the config for the physical connections:

         <Resource name="jdbc/MyCPDS"
            auth="Container"
            type="oracle.jdbc.pool.OracleConnectionPoolDataSource"
            factory="oracle.jdbc.pool.OracleDataSourceFactory"
            url="jdbc:oracle:thin:@some_url:1521:some_sid"/>


Yes, I know about the differences between PerUserPoolDataSource and
SharedPoolDataSource and I agree that the test for PerUserPoolDataSource
was not valid as you've indicated and I will try it again.  

However, I've used the same configuration for the SharedPoolDataSource
with the same negative results. 

Also, in both cases when I've used ds.getConnection() I've obtained the
raw Oracle connection but I've expected to get
org.apache.tomcat.dbcp.dbcp.PoolableConnection as with the
BasicDataSource.  Note, that I am not expecting to get
OraclePooledConnection as you've indicated as this would be wrong as the
OraclePooledConnection (that implements javax.sql.PooledConnection) is
supposed to be used internally by the Pool Manager implementation. The
OraclePooledConnection does not implement java.sql.Connection.  So, what
the DBCP's Pool Manager should do is to wrap the OraclePooledConnection
with the org.apache.tomcat.dbcp.dbcp.PoolableConnection (or some other
wrapper that implements java.sql.Connection interface) and return the
latter to the client.

Is it not the case with DBCP?


-----Original Message-----
From: Phil Steitz [mailto:phil.steitz@gmail.com] 
Sent: Sunday, June 17, 2007 3:32 PM
To: Jakarta Commons Users List
Subject: Re: [DBCP] The PerUserPoolDataSource/SharedPoolDataSource does
not reuse/pool the db connections

On 6/13/07, Seva Popov <se...@tvworks.com> wrote:
>
> Hi,
>
> I am using Tomcat 5.5.9 and have configured
> OracleConnectionPoolDataSource as the physical connection to the
> database and DBCP's PerUserPoolDataSource as the pool of logical
> connections to the same database (i.e. using the mentioned
> OracleConnectionPoolDataSource).
>
> Here is the configuration of my PerUserPoolDataSource (note the pool
> size):
>
> <Resource name="jdbc/MyPool"
>         auth="Container"
>
> type="org.apache.tomcat.dbcp.dbcp.datasources.PerUserPoolDataSource"
>
factory="org.apache.tomcat.dbcp.dbcp.datasources.PerUserPoolDataSourceFa
> ctory"
>         dataSourceName="java:comp/env/jdbc/MyCPDS"
>         maxActive="2"
>         maxIdle="2"
>         maxWait="5"/>
>

For PerUserPoolDataSource, the parameters are specified differently,
since you can set defaults for the pool and also override them per
user.  See
http://jakarta.apache.org/commons/dbcp/api-1.2.2/org/apache/commons/dbcp
/datasources/PerUserPoolDataSource.html
for the property names and
http://wiki.apache.org/jakarta-commons/DBCP for an example config.

> Now, I am using the below test JSP page to see that the connections
are
> reused from the pool:
>
> <jsp:directive.page import="javax.naming.Context" />
> <jsp:directive.page import="javax.naming.InitialContext" />
> <jsp:directive.page import="javax.sql.DataSource" />
> <jsp:directive.page import="java.sql.Connection" />
>   <html>
>     Hello
> <%
> Context initContext = new InitialContext();
> Context envContext  = (Context) initContext.lookup("java:/comp/env");
>
> DataSource ds = (DataSource) envContext.lookup("jdbc/MyPool");
> System.out.println("DataSource: " + ds);
>
> Connection conn1 = ds.getConnection("user", "psw ");
> System.out.println("Connection 1: " + conn1);
>
> Connection conn2 = ds.getConnection("user", "psw ");
> System.out.println("Connection 2: " + conn2);
>
> conn2.close(); // closing this connection to return it to the pool
>
> Connection conn3 = ds.getConnection("user", "psw ");
> System.out.println("Connection 3: " + conn3);
>
> Conn1.close();
> conn3.close();
> %>
> </html>
>
> Here is the output (and as you see all connections are different
> objects):
> DataSource:
> org.apache.tomcat.dbcp.dbcp.datasources.PerUserPoolDataSource@513bb5
> Connection 1: oracle.jdbc.driver.OracleConnection@1bdedf8
> Connection 2: oracle.jdbc.driver.OracleConnection@15daaef
> Connection 3: oracle.jdbc.driver.OracleConnection@572554
>
> My questions:
> 1. Firstly, I've expected to get the same connection object (as for
the
> connection #2) when I've obtained the 3thd connection because the pool
> max size is "2", the connection #1 is still active at the moment, and
> the connection #2 is closed and supposed to be returned to the pool.

For PerUserPoolDataSource this is most likely due to maxActive,
maxIdle parameters not being recognized.

> 2. Secondly, I've expected to get the DBCP's logical
> org.apache.tomcat.dbcp.dbcp.PoolableConnection but I've got the
physical
> Oracle connection instead.
>
Where and how do you specify the Oracle driver?  What you should get
back is a a PooledConnection.

> I've got the same results with the DBCP's SharedPoolDataSource.

The maxActive, maxIdle parameters should work for
SharedPoolDataSource.  Can you share the config that you used for this
test?

Phil

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [DBCP] The PerUserPoolDataSource/SharedPoolDataSource does not reuse/pool the db connections

Posted by Phil Steitz <ph...@gmail.com>.
On 6/13/07, Seva Popov <se...@tvworks.com> wrote:
>
> Hi,
>
> I am using Tomcat 5.5.9 and have configured
> OracleConnectionPoolDataSource as the physical connection to the
> database and DBCP's PerUserPoolDataSource as the pool of logical
> connections to the same database (i.e. using the mentioned
> OracleConnectionPoolDataSource).
>
> Here is the configuration of my PerUserPoolDataSource (note the pool
> size):
>
> <Resource name="jdbc/MyPool"
>         auth="Container"
>
> type="org.apache.tomcat.dbcp.dbcp.datasources.PerUserPoolDataSource"
> factory="org.apache.tomcat.dbcp.dbcp.datasources.PerUserPoolDataSourceFa
> ctory"
>         dataSourceName="java:comp/env/jdbc/MyCPDS"
>         maxActive="2"
>         maxIdle="2"
>         maxWait="5"/>
>

For PerUserPoolDataSource, the parameters are specified differently,
since you can set defaults for the pool and also override them per
user.  See
http://jakarta.apache.org/commons/dbcp/api-1.2.2/org/apache/commons/dbcp/datasources/PerUserPoolDataSource.html
for the property names and
http://wiki.apache.org/jakarta-commons/DBCP for an example config.

> Now, I am using the below test JSP page to see that the connections are
> reused from the pool:
>
> <jsp:directive.page import="javax.naming.Context" />
> <jsp:directive.page import="javax.naming.InitialContext" />
> <jsp:directive.page import="javax.sql.DataSource" />
> <jsp:directive.page import="java.sql.Connection" />
>   <html>
>     Hello
> <%
> Context initContext = new InitialContext();
> Context envContext  = (Context) initContext.lookup("java:/comp/env");
>
> DataSource ds = (DataSource) envContext.lookup("jdbc/MyPool");
> System.out.println("DataSource: " + ds);
>
> Connection conn1 = ds.getConnection("user", "psw ");
> System.out.println("Connection 1: " + conn1);
>
> Connection conn2 = ds.getConnection("user", "psw ");
> System.out.println("Connection 2: " + conn2);
>
> conn2.close(); // closing this connection to return it to the pool
>
> Connection conn3 = ds.getConnection("user", "psw ");
> System.out.println("Connection 3: " + conn3);
>
> Conn1.close();
> conn3.close();
> %>
> </html>
>
> Here is the output (and as you see all connections are different
> objects):
> DataSource:
> org.apache.tomcat.dbcp.dbcp.datasources.PerUserPoolDataSource@513bb5
> Connection 1: oracle.jdbc.driver.OracleConnection@1bdedf8
> Connection 2: oracle.jdbc.driver.OracleConnection@15daaef
> Connection 3: oracle.jdbc.driver.OracleConnection@572554
>
> My questions:
> 1. Firstly, I've expected to get the same connection object (as for the
> connection #2) when I've obtained the 3thd connection because the pool
> max size is "2", the connection #1 is still active at the moment, and
> the connection #2 is closed and supposed to be returned to the pool.

For PerUserPoolDataSource this is most likely due to maxActive,
maxIdle parameters not being recognized.

> 2. Secondly, I've expected to get the DBCP's logical
> org.apache.tomcat.dbcp.dbcp.PoolableConnection but I've got the physical
> Oracle connection instead.
>
Where and how do you specify the Oracle driver?  What you should get
back is a a PooledConnection.

> I've got the same results with the DBCP's SharedPoolDataSource.

The maxActive, maxIdle parameters should work for
SharedPoolDataSource.  Can you share the config that you used for this
test?

Phil

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


RE: [DBCP] The PerUserPoolDataSource/SharedPoolDataSource does not reuse/pool the db connections

Posted by Seva Popov <se...@tvworks.com>.
I thought you might be wondering whether my
OracleConnectionPoolDataSource is somehow configured incorrectly as the
physical connection to the database. 

To show that it's just fine I've added the following to my test JSP
page:

DataSource cpds = (DataSource) envContext.lookup("jdbc/MyCPDS ");
System.out.println("DataSource: " + cpds);

oracle.jdbc.pool.OracleConnectionPoolDataSource ocpdc =
(oracle.jdbc.pool.OracleConnectionPoolDataSource) cpds;

javax.sql.PooledConnection pc = ocpdc.getPooledConnection("user", "psw
"); 
System.out.println("Oracle pooled connection: " + pc);

Connection conn8 = pc.getConnection(); 
System.out.println("Underlying physical connection: " + conn8);

And the output looks good:

DataSource: oracle.jdbc.pool.OracleConnectionPoolDataSource@be4d44
Oracle pooled connection: oracle.jdbc.pool.OraclePooledConnection@51dee1
Underlying physical connection:
oracle.jdbc.driver.OracleConnection@ff8f2c
 

-----Original Message-----
From: Seva Popov [mailto:seva_popov@tvworks.com] 
Sent: Wednesday, June 13, 2007 4:04 PM
To: commons-user@jakarta.apache.org
Subject: [DBCP] The PerUserPoolDataSource/SharedPoolDataSource does not
reuse/pool the db connections


Hi,

I am using Tomcat 5.5.9 and have configured
OracleConnectionPoolDataSource as the physical connection to the
database and DBCP's PerUserPoolDataSource as the pool of logical
connections to the same database (i.e. using the mentioned
OracleConnectionPoolDataSource).

Here is the configuration of my PerUserPoolDataSource (note the pool
size):

<Resource name="jdbc/MyPool"
        auth="Container"
 
type="org.apache.tomcat.dbcp.dbcp.datasources.PerUserPoolDataSource"
factory="org.apache.tomcat.dbcp.dbcp.datasources.PerUserPoolDataSourceFa
ctory"
        dataSourceName="java:comp/env/jdbc/MyCPDS"
        maxActive="2"
        maxIdle="2"
        maxWait="5"/>

Now, I am using the below test JSP page to see that the connections are
reused from the pool:

<jsp:directive.page import="javax.naming.Context" />
<jsp:directive.page import="javax.naming.InitialContext" />
<jsp:directive.page import="javax.sql.DataSource" />
<jsp:directive.page import="java.sql.Connection" />
  <html>
    Hello
<%
Context initContext = new InitialContext();
Context envContext  = (Context) initContext.lookup("java:/comp/env");

DataSource ds = (DataSource) envContext.lookup("jdbc/MyPool");
System.out.println("DataSource: " + ds);

Connection conn1 = ds.getConnection("user", "psw ");
System.out.println("Connection 1: " + conn1);

Connection conn2 = ds.getConnection("user", "psw ");
System.out.println("Connection 2: " + conn2);

conn2.close(); // closing this connection to return it to the pool

Connection conn3 = ds.getConnection("user", "psw ");
System.out.println("Connection 3: " + conn3); 

Conn1.close();
conn3.close();
%>
</html>

Here is the output (and as you see all connections are different
objects):
DataSource:
org.apache.tomcat.dbcp.dbcp.datasources.PerUserPoolDataSource@513bb5
Connection 1: oracle.jdbc.driver.OracleConnection@1bdedf8
Connection 2: oracle.jdbc.driver.OracleConnection@15daaef
Connection 3: oracle.jdbc.driver.OracleConnection@572554

My questions:
1. Firstly, I've expected to get the same connection object (as for the
connection #2) when I've obtained the 3thd connection because the pool
max size is "2", the connection #1 is still active at the moment, and
the connection #2 is closed and supposed to be returned to the pool.
2. Secondly, I've expected to get the DBCP's logical
org.apache.tomcat.dbcp.dbcp.PoolableConnection but I've got the physical
Oracle connection instead.

I've got the same results with the DBCP's SharedPoolDataSource.

So, for me it seems that neither SharedPoolDataSource nor
PerUserPoolDataSource are really keeping and reusing the connections in
the pool of database connections.

Just to be complete with my testing I've verified that the Tomcat's
default DBCP's BasicDataSource does reusing the connections from the
pool. I.e. when I've configured BasicDataSource and used the similar
test JSP page I saw the output as expected like this (the
PoolableConnections #2 and #3 are the same i.e. they are reused):

DataSource: org.apache.tomcat.dbcp.dbcp.BasicDataSource@12a4caf
Connection 1: org.apache.tomcat.dbcp.dbcp.PoolableConnection@12b7083
Connection 2: org.apache.tomcat.dbcp.dbcp.PoolableConnection@13a8045
Connection 3: org.apache.tomcat.dbcp.dbcp.PoolableConnection@13a8045

Am I missing something here?

Thanks,
Seva 

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org