View Javadoc

1   /*
2    * Copyright (c) 2010.  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.jdbc;
22  
23  import org.apache.hadoop.hbase.HBaseConfiguration;
24  import org.apache.hadoop.hbase.hbql.client.HBqlException;
25  import org.apache.hadoop.hbase.hbql.client.HConnectionPool;
26  import org.apache.hadoop.hbase.hbql.client.HConnectionPoolManager;
27  import org.apache.hadoop.hbase.jdbc.impl.ConnectionImpl;
28  
29  import javax.sql.ConnectionPoolDataSource;
30  import javax.sql.PooledConnection;
31  import java.io.PrintWriter;
32  import java.sql.SQLException;
33  
34  public class ConnectionPool implements ConnectionPoolDataSource {
35  
36      private final HConnectionPool connectionPool;
37  
38      public ConnectionPool(final int initPoolSize,
39                            final int maxConnectionPoolSize) throws HBqlException {
40          this(initPoolSize, maxConnectionPoolSize, null, null);
41      }
42  
43      public ConnectionPool(final int initPoolSize,
44                            final int maxConnectionPoolSize,
45                            final String connectionPoolName) throws HBqlException {
46          this(initPoolSize, maxConnectionPoolSize, connectionPoolName, null);
47      }
48  
49      public ConnectionPool(final int initConnectionPoolSize,
50                            final int maxConnectionPoolSize,
51                            final HBaseConfiguration config) throws HBqlException {
52          this(initConnectionPoolSize, maxConnectionPoolSize, null, config);
53      }
54  
55      public ConnectionPool(final int initPoolSize,
56                            final int maxConnectionPoolSize,
57                            final String poolName,
58                            final HBaseConfiguration config) throws HBqlException {
59          this.connectionPool = HConnectionPoolManager.newConnectionPool(initPoolSize,
60                                                                         maxConnectionPoolSize,
61                                                                         poolName,
62                                                                         config);
63      }
64  
65      public static int getMaxPoolReferencesPerTablePerConnection() {
66          return HConnectionPoolManager.getMaxPoolReferencesPerTablePerConnection();
67      }
68  
69      public static void setMaxPoolReferencesPerTablePerConnection(final int maxPoolReferencesPerTablePerConnection) {
70          HConnectionPoolManager.setMaxPoolReferencesPerTablePerConnection(maxPoolReferencesPerTablePerConnection);
71      }
72  
73      private HConnectionPool getConnectionPool() {
74          return this.connectionPool;
75      }
76  
77      public PooledConnection getPooledConnection() throws SQLException {
78          return new ConnectionImpl(this.getConnectionPool().takeConnection());
79      }
80  
81      public PooledConnection getPooledConnection(final String s, final String s1) throws SQLException {
82          return this.getPooledConnection();
83      }
84  
85      public PrintWriter getLogWriter() throws SQLException {
86          return null;
87      }
88  
89      public void setLogWriter(final PrintWriter printWriter) throws SQLException {
90  
91      }
92  
93      public void setLoginTimeout(final int i) throws SQLException {
94  
95      }
96  
97      public int getLoginTimeout() throws SQLException {
98          return 0;
99      }
100 }