|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | +* @Title: ConnectionManager.java |
| 20 | +* @Package openthinks.libs.sql.dao |
| 21 | +* @Description: TODO |
| 22 | +* @author dailey.yet@outlook.com |
| 23 | +* @date Jul 30, 2015 |
| 24 | +* @version V1.0 |
| 25 | +*/ |
| 26 | +package openthinks.libs.sql.dao; |
| 27 | + |
| 28 | +import java.sql.Connection; |
| 29 | +import java.sql.DriverManager; |
| 30 | +import java.sql.SQLException; |
| 31 | + |
| 32 | +import org.apache.log4j.Logger; |
| 33 | + |
| 34 | +import openthinks.libs.sql.dao.pool.ConnectionPool; |
| 35 | +import openthinks.libs.sql.dao.pool.ConnectionPoolManager; |
| 36 | +import openthinks.libs.sql.lang.Configurator; |
| 37 | +import openthinks.libs.utilities.Checker; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author dailey.yet@outlook.com |
| 41 | + * |
| 42 | + */ |
| 43 | +public final class ConnectionManager { |
| 44 | + private static Logger logger = Logger.getLogger(ConnectionManager.class.getName()); |
| 45 | + /** |
| 46 | + * @param configurator Configurator |
| 47 | + * @return Connection |
| 48 | + * @throws ClassNotFoundException |
| 49 | + * @throws SQLException |
| 50 | + */ |
| 51 | + public static Connection getConnection(Configurator configurator) throws ClassNotFoundException, SQLException { |
| 52 | + Checker.require(configurator).notNull(); |
| 53 | + Connection conn = null; |
| 54 | + if(configurator.isUsePool()){ |
| 55 | + ConnectionPool connPool=ConnectionPoolManager.getInstance(configurator); |
| 56 | + conn = connPool.request(); |
| 57 | + }else{ |
| 58 | + Class.forName(configurator.getDriver()); |
| 59 | + conn = DriverManager.getConnection(configurator.getUrl(), |
| 60 | + configurator.getUserName(), configurator.getUserPwd()); |
| 61 | + |
| 62 | + } |
| 63 | + return conn; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param configurator Configurator |
| 68 | + * @param conns Connection[] |
| 69 | + */ |
| 70 | + public static void closeConnection(Configurator configurator, Connection... conns) { |
| 71 | + if(conns==null || conns.length==0) return; |
| 72 | + Checker.require(configurator).notNull(); |
| 73 | + if(configurator.isUsePool()){ |
| 74 | + ConnectionPool connPool=ConnectionPoolManager.getInstance(configurator); |
| 75 | + connPool.recycle(conns); |
| 76 | + }else{ |
| 77 | + for(Connection conn:conns){ |
| 78 | + if (conn != null) { |
| 79 | + try { |
| 80 | + if (!conn.isClosed()) { |
| 81 | + conn.close(); |
| 82 | + conn = null; |
| 83 | + } |
| 84 | + } catch (SQLException e) { |
| 85 | + logger.error(e); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | +} |
0 commit comments