View Javadoc

1   /*
2    * Copyright (c) 2011.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.hbql.client;
22  
23  import org.apache.hadoop.hbase.HBaseConfiguration;
24  import org.apache.hadoop.hbase.hbql.impl.HConnectionPoolImpl;
25  import org.apache.hadoop.hbase.hbql.impl.Utils;
26  import org.apache.hadoop.hbase.hbql.util.Maps;
27  
28  import java.util.Map;
29  
30  public class HConnectionPoolManager {
31  
32      private static Map<String, HConnectionPool> connectionPoolMap = Maps.newConcurrentHashMap();
33  
34      private static int maxPoolReferencesPerTablePerConnection = Integer.MAX_VALUE;
35  
36      public static HConnectionPool newConnectionPool(final int initialPoolSize,
37                                                      final int maxPoolSize) throws HBqlException {
38          return HConnectionPoolManager.newConnectionPool(initialPoolSize,
39                                                          maxPoolSize,
40                                                          null,
41                                                          null);
42      }
43  
44      public static HConnectionPool newConnectionPool(final int initialPoolSize,
45                                                      final int maxPoolSize,
46                                                      final HBaseConfiguration config) throws HBqlException {
47          return HConnectionPoolManager.newConnectionPool(initialPoolSize,
48                                                          maxPoolSize,
49                                                          null,
50                                                          config);
51      }
52  
53      public static HConnectionPool newConnectionPool(final int initialPoolSize,
54                                                      final int maxPoolSize,
55                                                      final String poolName) throws HBqlException {
56          return HConnectionPoolManager.newConnectionPool(initialPoolSize,
57                                                          maxPoolSize,
58                                                          poolName,
59                                                          null);
60      }
61  
62      public static HConnectionPool newConnectionPool(final int initialPoolSize,
63                                                      final int maxPoolSize,
64                                                      final String poolName,
65                                                      final HBaseConfiguration config) throws HBqlException {
66  
67          if (Utils.isValidString(poolName) && getConnectionPoolMap().containsKey(poolName))
68              throw new HBqlException("Connection pool already exists: " + poolName);
69  
70          final HConnectionPoolImpl connectionPool = new HConnectionPoolImpl(initialPoolSize,
71                                                                             maxPoolSize,
72                                                                             poolName,
73                                                                             config,
74                                                                             getMaxPoolReferencesPerTablePerConnection());
75          // Add to map if it has valid name
76          if (Utils.isValidString(connectionPool.getName()))
77              getConnectionPoolMap().put(connectionPool.getName(), connectionPool);
78  
79          return connectionPool;
80      }
81  
82  
83      public static int getMaxPoolReferencesPerTablePerConnection() {
84          return maxPoolReferencesPerTablePerConnection;
85      }
86  
87      public static void setMaxPoolReferencesPerTablePerConnection(final int maxPoolReferencesPerTablePerConnection) {
88          HConnectionPoolManager.maxPoolReferencesPerTablePerConnection = maxPoolReferencesPerTablePerConnection;
89      }
90  
91      public static HConnectionPool getConnectionPool(final String name) {
92          return HConnectionPoolManager.getConnectionPoolMap().get(name);
93      }
94  
95      private static Map<String, HConnectionPool> getConnectionPoolMap() {
96          return HConnectionPoolManager.connectionPoolMap;
97      }
98  }