KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > datasource > ResourceLimitingJdbcConnectionPool


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.datasource;
9
10 import org.apache.avalon.excalibur.pool.ValidatedResourceLimitingPool;
11
12 import java.sql.SQLException JavaDoc;
13
14 import org.apache.avalon.excalibur.datasource.JdbcConnection;
15 import org.apache.avalon.excalibur.pool.ObjectFactory;
16 import org.apache.avalon.excalibur.pool.Poolable;
17
18 /**
19  * A ResourceLimiting JdbcConnectionPool which allows for fine configuration of
20  * how the pool scales to loads.
21  *
22  * The pool supports; weak and strong pool size limits, optional blocking gets
23  * when connections are not available, and automatic trimming of unused
24  * connections.
25  *
26  * @author <a HREF="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
27  * @version CVS $Revision: 1.2 $ $Date: 2002/01/25 01:24:27 $
28  * @since 4.1
29  */

30 public class ResourceLimitingJdbcConnectionPool
31     extends ValidatedResourceLimitingPool
32 {
33     private boolean m_autoCommit;
34     
35     /*---------------------------------------------------------------
36      * Constructors
37      *-------------------------------------------------------------*/

38     /**
39      * Creates a new ResourceLimitingJdbcConnectionPool
40      *
41      * @param factory The ObjectFactory which will be used to create new connections as needed
42      * by the pool.
43      * @param max Maximum number of connections which can be stored in the pool, 0 implies
44      * no limit.
45      * @param maxStrict true if the pool should never allow more than max connections to be
46      * created. Will cause an exception to be thrown if more than max connections are
47      * requested and blocking is false.
48      * @param blocking true if the pool should cause a thread calling get() to block when
49      * connections are not currently available in the pool.
50      * @param blockTimeout The maximum amount of time, in milliseconds, that a call to get() will
51      * block before an exception is thrown. A value of 0 implies an indefinate wait.
52      * @param trimInterval The minimum interval with which old unused connections will be removed
53      * from the pool. A value of 0 will cause the pool to never trim old connections.
54      * @param autoCommit true if connections created by this pool should have autoCommit enabled.
55      */

56     public ResourceLimitingJdbcConnectionPool( final ObjectFactory factory,
57                                                int max,
58                                                boolean maxStrict,
59                                                boolean blocking,
60                                                long blockTimeout,
61                                                long trimInterval,
62                                                boolean autoCommit )
63         {
64                                 
65         super(factory, max, maxStrict, blocking, blockTimeout, trimInterval);
66         
67         m_autoCommit = autoCommit;
68     }
69     
70     /*---------------------------------------------------------------
71      * ValidatedResourceLimitingPool Methods
72      *-------------------------------------------------------------*/

73     /**
74      * Create a new poolable instance by by calling the newInstance method
75      * on the pool's ObjectFactory.
76      * This is the method to override when you need to enforce creational
77      * policies.
78      * This method is only called by threads that have _semaphore locked.
79      */

80     protected Poolable newPoolable() throws Exception JavaDoc {
81         JdbcConnection conn = (JdbcConnection)super.newPoolable();
82         
83         // Store a reference to this pool in the connection
84
conn.setPool(this);
85         
86         // Set the auto commit flag for new connections.
87
conn.setAutoCommit(m_autoCommit);
88         
89         return conn;
90     }
91     
92     /**
93      * Validates the poolable before it is provided to the caller of get on this pool.
94      * This implementation of the validation method always returns true indicating
95      * that the Poolable is valid.
96      * The pool is not locked by the current thread when this method is called.
97      *
98      * @param poolable The Poolable to be validated
99      * @returns true if the Poolable is valid, false if it should be removed from the pool.
100      */

101     protected boolean validatePoolable(Poolable poolable) {
102         JdbcConnection conn = (JdbcConnection)poolable;
103         try {
104             // Calling isClosed() may take time if the connection has not been
105
// used for a while. Is this a problem because the m_semaphore
106
// is currently locked? I am thinking no because isClosed() will
107
// return immediately when connections are being used frequently.
108
if (conn.isClosed()) {
109                 getLogger().debug("JdbcConnection was closed.");
110                 return false;
111             }
112         } catch (SQLException JavaDoc e) {
113             getLogger().debug(
114                 "Failed to check whether JdbcConnection was closed. " + e.getMessage());
115         }
116         
117         return true;
118     }
119     
120     /*---------------------------------------------------------------
121      * Public Methods
122      *-------------------------------------------------------------*/

123     
124     /*---------------------------------------------------------------
125      * Private Methods
126      *-------------------------------------------------------------*/

127 }
128
129
Popular Tags