001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.db; 029 030import java.sql.Connection; 031import java.sql.SQLException; 032import java.util.ArrayList; 033import java.util.List; 034 035/** 036 * Superclass for all SQL manager implementations.<p> 037 * 038 * @since 6.0.0 039 */ 040public class CmsSqlManager { 041 042 /** the driver manager. */ 043 private CmsDriverManager m_driverManager; 044 045 /** 046 * Protected constructor to allow only subclassing.<p> 047 */ 048 protected CmsSqlManager() { 049 050 // hide public constructor 051 } 052 053 /** 054 * Creates a new SQL manager from the provided driver manager.<p> 055 * 056 * @param driverManager the low level database driver manager 057 */ 058 protected CmsSqlManager(CmsDriverManager driverManager) { 059 060 m_driverManager = driverManager; 061 } 062 063 /** 064 * Returns the number of active connections managed by a pool.<p> 065 * 066 * @param dbPoolUrl the url of a pool 067 * @return the number of active connections 068 * @throws CmsDbException if something goes wrong 069 */ 070 public int getActiveConnections(String dbPoolUrl) throws CmsDbException { 071 072 return m_driverManager.getActiveConnections(dbPoolUrl); 073 } 074 075 /** 076 * Returns a connection to the database using the given pool identified by its name.<p> 077 * 078 * @param dbPoolName the pool name 079 * @return a database connection 080 * @throws SQLException if something goes wrong 081 */ 082 public Connection getConnection(String dbPoolName) throws SQLException { 083 084 return getConnectionByUrl(CmsDbPoolV11.OPENCMS_URL_PREFIX + dbPoolName); 085 } 086 087 /** 088 * Returns a connection to the database using the given pool identified by its full url.<p> 089 * 090 * @param dbPoolUrl the pool url 091 * @return a database connection 092 * @throws SQLException if something goes wrong 093 */ 094 public Connection getConnectionByUrl(String dbPoolUrl) throws SQLException { 095 096 return CmsDriverManager.m_pools.get(dbPoolUrl).getConnection(); 097 } 098 099 /** 100 * Returns a list of available database connection pool names.<p> 101 * 102 * @return a list of database connection pool names 103 */ 104 public List<String> getDbPoolUrls() { 105 106 return new ArrayList<String>(CmsDriverManager.m_pools.keySet()); 107 108 } 109 110 /** 111 * Returns the name of the default database connection pool.<p> 112 * 113 * @return the name of the default database connection pool 114 */ 115 public String getDefaultDbPoolName() { 116 117 return CmsDbPoolV11.getDefaultDbPoolName(); 118 } 119 120 /** 121 * Returns the number of idle connections managed by a pool.<p> 122 * 123 * @param dbPoolUrl the url of a pool 124 * @return the number of idle connections 125 * @throws CmsDbException if something goes wrong 126 */ 127 public int getIdleConnections(String dbPoolUrl) throws CmsDbException { 128 129 return m_driverManager.getIdleConnections(dbPoolUrl); 130 } 131}