KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > rolap > RolapConnectionPool


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/rolap/RolapConnectionPool.java#8 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2003-2006 Robin Bagot, Julian Hyde and others
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.rolap;
11
12 import mondrian.olap.Util;
13 import org.apache.commons.dbcp.*;
14 import org.apache.commons.pool.ObjectPool;
15 import org.apache.commons.pool.impl.GenericObjectPool;
16
17 import javax.sql.DataSource JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.HashMap JavaDoc;
20
21 /**
22  * Singleton class that holds a connection pool.
23  * Call RolapConnectionPool.instance().getPoolingDataSource(connectionFactory)
24  * to get a DataSource in return that is a pooled data source.
25  */

26 class RolapConnectionPool {
27
28     public static RolapConnectionPool instance() {
29         return instance;
30     }
31     private static final RolapConnectionPool instance = new RolapConnectionPool();
32
33     private final Map JavaDoc<Object JavaDoc, ObjectPool> mapConnectKeyToPool =
34         new HashMap JavaDoc<Object JavaDoc, ObjectPool>();
35
36     private RolapConnectionPool() {
37     }
38
39
40     /**
41      * Sets up a pooling data source for connection pooling.
42      * This can be used if the application server does not have a pooling
43      * dataSource already configured.
44      * This takes a normal jdbc connection string, and requires a jdbc
45      * driver to be loaded, and then uses a
46      * {@link DriverManagerConnectionFactory} to create connections to the
47      * database.
48      * An alternative method of configuring a pooling driver is to use an external
49      * configuration file. See the the Apache jakarta-commons commons-pool
50      * documentation.
51      *
52      * @param key Identifies which connection factory to use. A typical key is
53      * a JDBC connect string, since each JDBC connect string requires a
54      * different connection factory.
55      * @param connectionFactory Creates connections from an underlying
56      * JDBC connect string or DataSource
57      * @return a pooling DataSource object
58      */

59     public synchronized DataSource JavaDoc getPoolingDataSource(Object JavaDoc key,
60                                        ConnectionFactory connectionFactory) {
61         ObjectPool connectionPool = getPool(key, connectionFactory);
62         // create pooling datasource
63
return new PoolingDataSource(connectionPool);
64     }
65
66     /**
67      * Clears the connection pool for testing purposes
68      */

69     void clearPool() {
70         mapConnectKeyToPool.clear();
71     }
72
73     /**
74      * Gets or creates a connection pool for a particular connect
75      * specification.
76      */

77     private synchronized ObjectPool getPool(
78         Object JavaDoc key,
79         ConnectionFactory connectionFactory)
80     {
81         ObjectPool connectionPool = mapConnectKeyToPool.get(key);
82         if (connectionPool == null) {
83             // use GenericObjectPool, which provides for resource limits
84
connectionPool = new GenericObjectPool(
85                 null, // PoolableObjectFactory, can be null
86
50, // max active
87
GenericObjectPool.WHEN_EXHAUSTED_BLOCK, // action when exhausted
88
3000, // max wait (milli seconds)
89
10, // max idle
90
false, // test on borrow
91
false, // test on return
92
60000, // time between eviction runs (millis)
93
5, // number to test on eviction run
94
30000, // min evictable idle time (millis)
95
true // test while idle
96
);
97
98             // create a PoolableConnectionFactory
99
AbandonedConfig abandonedConfig = new AbandonedConfig();
100             // flag to remove abandoned connections from pool
101
abandonedConfig.setRemoveAbandoned(true);
102             // timeout (seconds) before removing abandoned connections
103
abandonedConfig.setRemoveAbandonedTimeout(300);
104             // Flag to log stack traces for application code which abandoned a
105
// Statement or Connection
106
abandonedConfig.setLogAbandoned(true);
107             PoolableConnectionFactory poolableConnectionFactory
108                 = new PoolableConnectionFactory(
109                     // the connection factory
110
connectionFactory,
111                     // the object pool
112
connectionPool,
113                     // statement pool factory for pooling prepared statements,
114
// or null for no pooling
115
null,
116                     // validation query (must return at least 1 row e.g. Oracle:
117
// select count(*) from dual) to test connection, can be
118
// null
119
null,
120                     // default "read only" setting for borrowed connections
121
false,
122                     // default "auto commit" setting for returned connections
123
true,
124                     // AbandonedConfig object configures how to handle abandoned
125
// connections
126
abandonedConfig
127             );
128             // "poolableConnectionFactory" has registered itself with
129
// "connectionPool", somehow, so we don't need the value any more.
130
Util.discard(poolableConnectionFactory);
131             mapConnectKeyToPool.put(key, connectionPool);
132         }
133         return connectionPool;
134     }
135
136 }
137
138 // End RolapConnectionPool.java
139
Popular Tags