1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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 }