commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Berin Loritsch <blorit...@apache.org>
Subject Re: Database Connection Pool
Date Thu, 27 Dec 2001 20:29:34 GMT
Randy Speh wrote:

> Could you please tell me where the document is on how
> to configure and start and access Avalon's connection
> pool.


The javadocs for the JDBC DataSource are here:

http://jakarta.apache.org/avalon/excalibur/api/org/apache/avalon/excalibur/datasource/JdbcDataSource.html

There is more information in the Avalon Developers Docs in Chapter 3:

http://jakarta.apache.org/avalon/developing/implementing.html

But it is interspersed in instructions for how to use other parts of
Avalon.

It is an Avalon Component, so it is not easily separated.

If you forgo the nice configuration interface, you can set it up programatically
like this:

Class.forName("oracle.jdbc.driver.OracleDriver");
JdbcConnectionFactory factory = public JdbcConnectionFactory("jdbc:oracle:thin:@localhost:1521:ORCL",
                              "scott", "tiger", false /* autocommit? */,
                              "SELECT 1 FROM DUAL",
                              "org.apache.avalon.excalibur.datasource.JdbcConnection");
DefaultPoolController controller = new DefaultPoolController( 2 /* grow amount */ );
JdbcConnectionPool datasourcePool = new JdbcConnectionPool( factory, controller, 2 /* min
*/, 10 /* max */, false /* autocommit? */

datasourcePool.setTimeout( 500 ); // set time in millis pool will block waiting for a connection
to be returned to an empty pool
datasourcePool.initialize();

// the datasourcePool is active now

(Connection) datasourcePool.get();

// when the pool is ready to be shut down
datasourcePool.dispose();


A little explanation follows:

The pool assumes that the driver is actually installed in the classpath, and
the class has been loaded.

The JdbcConnectionFactory is used to create the Connection objects.  The Connection
objects must extend AbstractJdbcConnection.  In Avalon there are two JdbcConnection
implementations: JdbcConnection for JDBC 2.0 compliant drivers, and Jdbc3Connection
for JDBC 3.0 compliant drivers (aka JDK 1.4).

The parameter names for the JdbcConnectionFactory are as follows:

public JdbcConnectionFactory(java.lang.String url,
                              java.lang.String username,
                              java.lang.String password,
                              boolean autoCommit,
                              java.lang.String keepAlive,
                              java.lang.String connectionClass)

The parameter names for the datasourcePool are as follows:

public JdbcConnectionPool(JdbcConnectionFactory factory,
                           DefaultPoolController controller,
                           int min,
                           int max,
                           boolean autoCommit)


The initialization of the pool is asynchronous, and starts when you call initialize();
If you ask for a connection before the pool is fully initialized, it will block until
the pool is ready.  This allows the pool to open new connections (which can take upwards
of a second or more per connection) in the background while your application finishes
initializing.  Most of the time, the pool is done initializing before you request the
first datasource.

There might be a detail or two I am missing, but that is because I use the JdbcDataSource
Component, which takes advantage of Avalon Framework's lifecycle methods.  The full
component takes care of all the details as long as you pass it a valid configuration
object.


> 
> Thank you,
> Randy Speh
> 
> 
> 
> --- Berin Loritsch <bloritsch@apache.org> wrote:
> 
>>Randy Speh wrote:
>>
>>
>>>Thanks for the info.  Would you mind telling me
>>>
>>the
>>
>>>differences between Avalon and Fulcrum/Commons.
>>>
>>>I just wasn't aware of Avalon for some reason. 
>>>
>>I've
>>
>>>been submerged in Turbine, Torque, Fulcrum and
>>>
>>Commons
>>
>>>code.
>>>
>>
>>Honestly, I can only tell you what Avalon does (I
>>wrote
>>the code for the DB pooling).  I don't know what
>>Fulcrum
>>is much less what it does.  As for Commons, I have
>>only
>>had a cursory look at it, so I am not the one to
>>answer this.
>>
>>
>>
>>>Thanks,
>>>Randy
>>>--- Berin Loritsch <bloritsch@apache.org> wrote:
>>>
>>>
>>>>Randy Speh wrote:
>>>>
>>>>
>>>>
>>>>>I would be very interested to see what your
>>>>>
>>>>>
>>>>connection
>>>>
>>>>
>>>>>pooling code looks like.  I been trying to choose
>>>>>
>>>>>
>>>>the
>>>>
>>>>
>>>>>most appropriate connection pooling technique and
>>>>>
>>>>>
>>>>have
>>>>
>>>>
>>>>>been considering adapting one myself.  Although,
>>>>>
>>>>>
>>>>I'd
>>>>
>>>>
>>>>>really rather use something from the open source
>>>>>community.
>>>>>
>>>>>
>>>>>
>>>>Currently there is dbcp in Commons, Turbine
>>>>connection pooling,
>>>>and Avalon connection pooling.
>>>>
>>>>Both DBCP and Avalon provide similar semantics
>>>>inspired by
>>>>the official JDBC DataSource specifications--when
>>>>you "close"
>>>>the connection, it returns the Connection to the
>>>>pool.  Turbine
>>>>(if I recall) makes you explicitly return it to
>>>>
>>the
>>
>>>>pool yourself.
>>>>
>>>>I won't get involved with which is the best
>>>>
>>pattern.
>>
>>>>Beyond that basic difference, there is alot of
>>>>difference in the
>>>>manner that the connection pools are configured. 
>>>>Since I don't
>>>>have any real information on DBCP and Turbine
>>>>pooling, I won't
>>>>talk about them.  Here is what Avalon's
>>>>Configuration looks like:
>>>>
>>>><connection>
>>>>  <pool-controller min="2" max="10" grow="2">
>>>>    <keep-alive>SELECT 1 FROM DUAL</keep-alive>
>>>>  </pool-controller>
>>>>
>>>> 
>>>>
>><driver>oracle.jdbc.driver.OracleDriver</driver>
>>
>>>> 
>>>>
>>><dburl>jdbc:oracle:thin:@localhost:1521:ORCL</dburl>
>>>
>>>>  <user>scott</user>
>>>>  <password>tiger</password>
>>>></connection>
>>>>
>>>>The Pool Controller specifies how many connections
>>>>the pool starts
>>>>with ("min"), the absolute maximum number of
>>>>connections ("max"),
>>>>and how many new connections it creates at a time
>>>>when it needs
>>>>more ("grow").  It is important to note that the
>>>>maximum is absolute.
>>>>The pool will not go beyond that maximum number. 
>>>>The reason being
>>>>that some drivers have license restrictions and it
>>>>helps the pool
>>>>to not violate the license.  The last part
>>>>("keep-alive") is the
>>>>query used by the pool to determine if the
>>>>connection has been closed
>>>>by the server.  We found out that not all
>>>>
>>databases
>>
>>>>are created
>>>>equal, and while "SELECT 1" works for many
>>>>databases, Oracle requires
>>>>that "1" be selected from the dummy table "DUAL",
>>>>and others like
>>>>Informix require you to select from an
>>>>
>>application's
>>
>>>>table.
>>>>
>>>>You can determine for yourself if this is
>>>>desirable--and possibly
>>>>DBCP might steal ideas to narrow the differences. 
>>>>Who knows....
>>>>
>>>>
>>>>-- 
>>>>
>>>>"They that give up essential liberty to obtain a
>>>>little temporary safety
>>>> deserve neither liberty nor safety."
>>>>                - Benjamin Franklin
>>>>
>>>>
>>>>--
>>>>To unsubscribe, e-mail:  
>>>>
>>><mailto:commons-dev-unsubscribe@jakarta.apache.org>
>>>
>>>>For additional commands, e-mail:
>>>><mailto:commons-dev-help@jakarta.apache.org>
>>>>
>>>
>>>__________________________________________________
>>>Do You Yahoo!?
>>>Send your FREE holiday greetings online!
>>>http://greetings.yahoo.com
>>>
>>>--
>>>To unsubscribe, e-mail:  
>>>
>><mailto:commons-dev-unsubscribe@jakarta.apache.org>
>>
>>>For additional commands, e-mail:
>>>
>><mailto:commons-dev-help@jakarta.apache.org>
>>
>>>.
>>>
>>>
>>>
>>
>>
>>-- 
>>
>>"They that give up essential liberty to obtain a
>>little temporary safety
>>  deserve neither liberty nor safety."
>>                 - Benjamin Franklin
>>
>>
>>--
>>To unsubscribe, e-mail:  
>><mailto:commons-dev-unsubscribe@jakarta.apache.org>
>>For additional commands, e-mail:
>><mailto:commons-dev-help@jakarta.apache.org>
>>
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Send your FREE holiday greetings online!
> http://greetings.yahoo.com
> 
> --
> To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>
> 
> .
> 
> 



-- 

"They that give up essential liberty to obtain a little temporary safety
  deserve neither liberty nor safety."
                 - Benjamin Franklin


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message