KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
26  * The DefaultRoundRobinDataSourceCluster allows the user to specify a cluster of DataSources
27  * which all act as one. The Cluster works by cycling through its member DataSources returning
28  * a connection from a different one with each call to getConnection().
29  * <p>
30  * This form of Clustering has the benefit that it can be used by components without requiring
31  * any changes. But care must be taken as to the kind of data written or read from the database.
32  * Wich this clustering method, there is no control over which DataSource will provide a
33  * connection for any given call.
34  * <p>
35  * Round Robin Clusters are useful in cases where lots of read-only data needs to be accessed and
36  * multiple copies of the data can be stored on different database servers to balance load.
37  * <p>
38  * The Configuration for a 2 database cluster is like this:
39  *
40  * <pre>
41  * &lt;datasources&gt;
42  * &lt;roundrobin-cluster name="mydb-cluster" size="2"&gt;
43  * &lt;dbpool index="0"&gt;mydb-0&lt;/dbpool&gt;
44  * &lt;dbpool index="1"&gt;mydb-1&lt;/dbpool&gt;
45  * &lt;/roundrobin-cluster&gt;
46  * &lt;/datasources&gt;
47  * &lt;cluster-datasources&gt;
48  * &lt;jdbc name="mydb-0"&gt;
49  * &lt;pool-controller min="1" max="10"/&gt;
50  * &lt;auto-commit&gt;true&lt;/auto-commit&gt;
51  * &lt;driver&gt;com.database.jdbc.JdbcDriver&lt;/driver&gt;
52  * &lt;dburl&gt;jdbc:driver://host0/mydb&lt;/dburl&gt;
53  * &lt;user&gt;username&lt;/user&gt;
54  * &lt;password&gt;password&lt;/password&gt;
55  * &lt;/jdbc&gt;
56  * &lt;jdbc name="mydb-1"&gt;
57  * &lt;pool-controller min="1" max="10"/&gt;
58  * &lt;auto-commit&gt;true&lt;/auto-commit&gt;
59  * &lt;driver&gt;com.database.jdbc.JdbcDriver&lt;/driver&gt;
60  * &lt;dburl&gt;jdbc:driver://host1/mydb&lt;/dburl&gt;
61  * &lt;user&gt;username&lt;/user&gt;
62  * &lt;password&gt;password&lt;/password&gt;
63  * &lt;/jdbc&gt;
64  * &lt;/cluster-datasources&gt;
65  * </pre>
66  *
67  * With the following roles declaration:
68  *
69  * <pre>
70  * &lt;role name="org.apache.avalon.excalibur.datasource.DataSourceComponentSelector"
71  * shorthand="datasources"
72  * default-class="org.apache.avalon.excalibur.component.ExcaliburComponentSelector"&gt;
73  * &lt;hint shorthand="jdbc" class="org.apache.avalon.excalibur.datasource.JdbcDataSource"/&gt;
74  * &lt;hint shorthand="j2ee" class="org.apache.avalon.excalibur.datasource.J2eeDataSource"/&gt;
75  * &lt;hint shorthand="roundrobin-cluster"
76  * class="org.apache.avalon.excalibur.datasource.cluster.DefaultRoundRobinDataSourceCluster"/&gt;
77  * &lt;/role&gt;
78  * &lt;role name="org.apache.avalon.excalibur.datasource.DataSourceComponentClusterSelector"
79  * shorthand="cluster-datasources"
80  * default-class="org.apache.avalon.excalibur.component.ExcaliburComponentSelector"&gt;
81  * &lt;hint shorthand="jdbc" class="org.apache.avalon.excalibur.datasource.JdbcDataSource"/&gt;
82  * &lt;hint shorthand="j2ee" class="org.apache.avalon.excalibur.datasource.J2eeDataSource"/&gt;
83  * &lt;/role&gt;
84  * </pre>
85  *
86  * @avalon.component
87  * @avalon.service type=org.apache.avalon.excalibur.datasource.DataSourceComponent
88  * @avalon.service type=RoundRobinDataSourceCluster
89  * @x-avalon.info name=rrobin-db-cluster
90  * @x-avalon.lifestyle type=singleton
91  *
92  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
93  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:20 $
94  * @since 4.1
95  */

96 public class DefaultRoundRobinDataSourceCluster
97     extends AbstractDataSourceCluster
98     implements RoundRobinDataSourceCluster
99 {
100     private Object JavaDoc m_semaphore = new Object JavaDoc();
101     private int m_nextIndex;
102
103     /*---------------------------------------------------------------
104      * Constructors
105      *-------------------------------------------------------------*/

106     public DefaultRoundRobinDataSourceCluster()
107     {
108     }
109
110     /*---------------------------------------------------------------
111      * DataSourceComponent Methods
112      *-------------------------------------------------------------*/

113     /**
114      * Returns a Connection to one of the Cluster's member DataSources.
115      *
116      * @throws NoValidConnectionException when there is no valid Connection wrapper
117      * available in the classloader.
118      *
119      * @throws NoValidConnectionException when there are no more available
120      * Connections in the pool.
121      */

122     public Connection JavaDoc getConnection() throws SQLException JavaDoc
123     {
124         int index;
125         synchronized( m_semaphore )
126         {
127             index = m_nextIndex;
128             if( ( ++m_nextIndex ) >= m_size )
129             {
130                 m_nextIndex = 0;
131             }
132         }
133
134         return getConnectionForIndex( index );
135     }
136 }
137
138
Popular Tags