1 /*******************************************************************************
2 * Copyright (c) 2013 hangum.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser Public License v2.1
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
9 * hangum - initial API and implementation
10 ******************************************************************************/
11 package com
.hangum
.tadpole
.tajo
.core
.connections
.manager
;
13 import java
.util
.HashMap
;
16 import javax
.sql
.DataSource
;
18 import org
.apache
.commons
.dbcp
.ConnectionFactory
;
19 import org
.apache
.commons
.dbcp
.DriverManagerConnectionFactory
;
20 import org
.apache
.commons
.dbcp
.PoolableConnectionFactory
;
21 import org
.apache
.commons
.dbcp
.PoolingDataSource
;
22 import org
.apache
.commons
.pool
.impl
.GenericObjectPool
;
23 import org
.apache
.log4j
.Logger
;
25 import com
.hangum
.tadpole
.cipher
.core
.manager
.CipherManager
;
26 import com
.hangum
.tadpole
.engine
.query
.dao
.system
.UserDBDAO
;
29 * DBCP connection manager
34 public class ConnectionPoolManager
{
35 private static final Logger logger
= Logger
.getLogger(ConnectionPoolManager
.class);
39 Class
.forName("org.apache.tajo.jdbc.TajoDriver");
40 } catch(Exception e
) {
41 logger
.error("Apache Tajo Class not found exception", e
);
45 public static ConnectionPoolManager instance
= null;//new ConnectionPoolManager();
46 private static Map
<String
, DataSource
> mapDataSource
= new HashMap
<String
, DataSource
>();
47 private ConnectionPoolManager() {}
49 public static ConnectionPoolManager
getInstance(final UserDBDAO userDB
) {
50 if(instance
== null) {
51 instance
= new ConnectionPoolManager();
56 private static DataSource
makePool(UserDBDAO userDB
) {
57 GenericObjectPool connectionPool
= new GenericObjectPool();
58 connectionPool
.setMaxActive(5);
59 // connectionPool.setWhenExhaustedAction((byte)1);
60 // connectionPool.setMaxWait(1000 * 60); // 1분대기.
61 // connectionPool.setTimeBetweenEvictionRunsMillis(3 * 1000);
62 connectionPool
.setTestWhileIdle(true);
64 String passwdDecrypt
= "";
66 passwdDecrypt
= CipherManager
.getInstance().decryption(userDB
.getPasswd());
67 } catch(Exception e
) {
68 passwdDecrypt
= userDB
.getPasswd();
70 ConnectionFactory cf
= new DriverManagerConnectionFactory(userDB
.getUrl(), userDB
.getUsers(), passwdDecrypt
);
72 PoolableConnectionFactory pcf
= new PoolableConnectionFactory(cf
, connectionPool
, null, null, false, true);
73 DataSource ds
= new PoolingDataSource(connectionPool
);
74 mapDataSource
.put(getKey(userDB
), ds
);
79 public static DataSource
getDataSource(final UserDBDAO userDB
) {
80 DataSource retDataSource
= mapDataSource
.get(getKey(userDB
));
81 if(retDataSource
== null) {
82 return makePool(userDB
);
93 private static String
getKey(final UserDBDAO userDB
) {
94 return userDB
.getSeq() + userDB
.getDbms_type()+userDB
.getUrl()+userDB
.getUsers();//+dbInfo.getPasswd();