KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > datasource > cluster > DefaultIndexedDataSourceCluster


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.avalon.excalibur.datasource.cluster;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.SQLException JavaDoc;
24
25 import org.apache.avalon.excalibur.datasource.NoValidConnectionException;
26
27 /**
28  * The DefaultIndexedDataSourceCluster requires that the user implement their own method of
29  * selecting which DataSource in the cluster to use for each connection request. Calls to
30  * getConnection() will throw an exception. Components which make use of this class must call
31  * the getConnectionForIndex(int index) method instead.
32  * <p>
33  * The Configuration for a 2 database cluster is like this:
34  *
35  * <pre>
36  * &lt;datasources&gt;
37  * &lt;indexed-cluster name="mydb-cluster" size="2"&gt;
38  * &lt;dbpool index="0"&gt;mydb-0&lt;/dbpool&gt;
39  * &lt;dbpool index="1"&gt;mydb-1&lt;/dbpool&gt;
40  * &lt;/indexed-cluster&gt;
41  * &lt;/datasources&gt;
42  * &lt;cluster-datasources&gt;
43  * &lt;jdbc name="mydb-0"&gt;
44  * &lt;pool-controller min="1" max="10"/&gt;
45  * &lt;auto-commit&gt;true&lt;/auto-commit&gt;
46  * &lt;driver&gt;com.database.jdbc.JdbcDriver&lt;/driver&gt;
47  * &lt;dburl&gt;jdbc:driver://host0/mydb&lt;/dburl&gt;
48  * &lt;user&gt;username&lt;/user&gt;
49  * &lt;password&gt;password&lt;/password&gt;
50  * &lt;/jdbc&gt;
51  * &lt;jdbc name="mydb-1"&gt;
52  * &lt;pool-controller min="1" max="10"/&gt;
53  * &lt;auto-commit&gt;true&lt;/auto-commit&gt;
54  * &lt;driver&gt;com.database.jdbc.JdbcDriver&lt;/driver&gt;
55  * &lt;dburl&gt;jdbc:driver://host1/mydb&lt;/dburl&gt;
56  * &lt;user&gt;username&lt;/user&gt;
57  * &lt;password&gt;password&lt;/password&gt;
58  * &lt;/jdbc&gt;
59  * &lt;/cluster-datasources&gt;
60  * </pre>
61  *
62  * With the following roles declaration:
63  *
64  * <pre>
65  * &lt;role name="org.apache.avalon.excalibur.datasource.DataSourceComponentSelector"
66  * shorthand="datasources"
67  * default-class="org.apache.avalon.excalibur.component.ExcaliburComponentSelector"&gt;
68  * &lt;hint shorthand="jdbc" class="org.apache.avalon.excalibur.datasource.JdbcDataSource"/&gt;
69  * &lt;hint shorthand="j2ee" class="org.apache.avalon.excalibur.datasource.J2eeDataSource"/&gt;
70  * &lt;hint shorthand="indexed-cluster"
71  * class="org.apache.avalon.excalibur.datasource.cluster.DefaultIndexedDataSourceCluster"/&gt;
72  * &lt;/role&gt;
73  * &lt;role name="org.apache.avalon.excalibur.datasource.DataSourceComponentClusterSelector"
74  * shorthand="cluster-datasources"
75  * default-class="org.apache.avalon.excalibur.component.ExcaliburComponentSelector"&gt;
76  * &lt;hint shorthand="jdbc" class="org.apache.avalon.excalibur.datasource.JdbcDataSource"/&gt;
77  * &lt;hint shorthand="j2ee" class="org.apache.avalon.excalibur.datasource.J2eeDataSource"/&gt;
78  * &lt;/role&gt;
79  * </pre>
80  *
81  * An indexed-cluster definition enforces that the configuration specify a size. This size must
82  * equal the number of datasources referenced as being members of the cluster. Any datasource can
83  * be a member of the cluster.
84  * <p>
85  * The indexed-cluster can be obtained in the same manner as a non-clustered datasource. The only
86  * difference is in how it is used. The IndexedDataSourceCluster requires that the caller specify
87  * the index of the cluster member to use when requesting a connection.
88  * <p>
89  * The following code demonstrates a change that can be made to database enabled components so that
90  * they will be able to work with both IndexedDataSourceCluster DataSources and regular
91  * DataSources.
92  * <p>
93  * old:
94  * <pre>
95  * Connection connection = m_dataSource.getConnection();
96  * </pre>
97  *
98  * new:
99  * <pre>
100  * Connection connection;
101  * if ( m_dataSource instanceof IndexedDataSourceCluster )
102  * {
103  * connection = ((IndexedDataSourceCluster)m_dataSource).getConnectionForIndex( index );
104  * }
105  * else
106  * {
107  * connection = m_dataSource.getConnection();
108  * }
109  * </pre>
110  *
111  * @avalon.component
112  * @avalon.service type=org.apache.avalon.excalibur.datasource.DataSourceComponent
113  * @avalon.service type=IndexedDataSourceCluster
114  * @x-avalon.info name=index-db-cluster
115  * @x-avalon.lifestyle type=singleton
116  *
117  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
118  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:20 $
119  * @since 4.1
120  */

121 public class DefaultIndexedDataSourceCluster
122     extends AbstractDataSourceCluster
123     implements IndexedDataSourceCluster
124 {
125
126     /*---------------------------------------------------------------
127      * Constructors
128      *-------------------------------------------------------------*/

129     public DefaultIndexedDataSourceCluster()
130     {
131     }
132
133     /*---------------------------------------------------------------
134      * DataSourceComponent Methods
135      *-------------------------------------------------------------*/

136     /**
137      * Not supported in this component. Will throw a NoValidConnectionException.
138      */

139     public Connection JavaDoc getConnection() throws SQLException JavaDoc
140     {
141         throw new NoValidConnectionException(
142             "getConnection() should not be called for a " + getClass().getName() + ". " +
143             "Please verify your configuration." );
144     }
145
146     /*---------------------------------------------------------------
147      * IndexedDataSourceCluster Methods
148      *-------------------------------------------------------------*/

149     // public int getClusterSize()
150
// declared in AbstractDataSourceCluster
151

152     // public Connection getConnectionForIndex( int index ) throws SQLException
153
// declared in AbstractDataSourceCluster
154
}
155
156
Popular Tags