View Javadoc
1 package org.apache.turbine.services.db; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.sql.Connection; 58 import org.apache.torque.adapter.DB; 59 import org.apache.torque.map.DatabaseMap; 60 import org.apache.turbine.services.TurbineServices; 61 import org.apache.turbine.util.TurbineException; 62 63 /*** 64 * This class provides a common front end to all database - related 65 * services in Turbine. Currently these are {@link PoolBrokerService} and 66 * {@link MapBrokerService}. This class contains static methods that you 67 * can call to access the methods of system's configured service 68 * implementations. 69 * <p> 70 * <b> This class is deprecated you should use org.apache.torque.Torque</b> 71 * 72 * Connection dbConn = null; 73 * try 74 * { 75 * dbConn = Torque.getConnection(); 76 * // Do something with the connection here... 77 * } 78 * catch (Exception e) 79 * { 80 * // Either from obtaining the connection or from your application code. 81 * } 82 * finally 83 * { 84 * try 85 * { 86 * dbConn.close(); 87 * } 88 * catch (Exception e) 89 * { 90 * // Error releasing database connection back to pool. 91 * } 92 * } 93 * </pre></code></blockquote> 94 * 95 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a> 96 * @version $Id: TurbineDB.java,v 1.5 2002/07/12 20:46:55 mpoeschl Exp $ 97 * @deprecated use org.apache.torque.Torque 98 */ 99 public abstract class TurbineDB 100 { 101 /////////////////////////////////////////////////////////////////////////// 102 // Database maps 103 /////////////////////////////////////////////////////////////////////////// 104 105 /*** 106 * Returns the map name for the default database. 107 * 108 * @return the map name for the default database. 109 */ 110 public static String getDefaultMap() 111 { 112 // Required due to the nasty coupling between 113 // torque in 2.x and the db related services. 114 // This is only called once in the peer so the 115 // hit of catching the exception will only happen 116 // once and while running torque from the command 117 // line it won't incur an unbearable wait. 118 try 119 { 120 return getMapBroker().getDefaultMap(); 121 } 122 catch(Exception e) 123 { 124 // do nothing 125 } 126 127 return MapBrokerService.DEFAULT; 128 } 129 130 /*** 131 * Returns the default database map information. 132 * 133 * @return A DatabaseMap. 134 * @throws TurbineException Any exceptions caught during processing will be 135 * rethrown wrapped into a TurbineException. 136 */ 137 public static DatabaseMap getDatabaseMap() 138 throws TurbineException 139 { 140 return getMapBroker().getDatabaseMap(); 141 } 142 143 /*** 144 * Returns the database map information. Name relates to the name 145 * of the connection pool to associate with the map. 146 * 147 * @param name The name of the <code>DatabaseMap</code> to 148 * retrieve. 149 * @return The named <code>DatabaseMap</code>. 150 * @throws TurbineException Any exceptions caught during processing will be 151 * rethrown wrapped into a TurbineException. 152 */ 153 public static DatabaseMap getDatabaseMap(String name) 154 throws TurbineException 155 { 156 return getMapBroker().getDatabaseMap(name); 157 } 158 159 /////////////////////////////////////////////////////////////////////////// 160 // Connection pooling 161 /////////////////////////////////////////////////////////////////////////// 162 163 /*** 164 * Returns the pool name for the default database. 165 * 166 * @return the pool name for the default database. 167 */ 168 public static String getDefaultDB() 169 { 170 // Required due to the nasty coupling between 171 // torque in 2.x and the db related services. 172 // This is only called once in the peer so the 173 // hit of catching the exception will only happen 174 // once and while running torque from the command 175 // line it won't incur an unbearable wait. 176 try 177 { 178 return getPoolBroker().getDefaultDB(); 179 } 180 catch(Exception e) 181 { 182 // do nothing 183 } 184 185 return PoolBrokerService.DEFAULT; 186 } 187 188 /*** 189 * This method returns a DBConnection from the default pool. 190 * 191 * @return The requested connection. 192 * @throws Exception Any exceptions caught during processing will be 193 * rethrown wrapped into a TurbineException. 194 */ 195 public static Connection getConnection() 196 throws Exception 197 { 198 return getPoolBroker().getConnection(); 199 } 200 201 /*** 202 * This method returns a DBConnection from the pool with the 203 * specified name. The pool must either have been registered 204 * with the {@link #registerPool(String,String,String,String,String)} 205 * method, or be specified in the property file using the 206 * following syntax: 207 * 208 * <pre> 209 * database.[name].driver 210 * database.[name].url 211 * database.[name].username 212 * database.[name].password 213 * </pre> 214 * 215 * @param name The name of the pool to get a connection from. 216 * @return The requested connection. 217 * @throws Exception Any exceptions caught during processing will be 218 * rethrown wrapped into a TurbineException. 219 */ 220 public static Connection getConnection(String name) 221 throws Exception 222 { 223 return getPoolBroker().getConnection(name); 224 } 225 226 /*** 227 * Release a connection back to the database pool. 228 * 229 * @throws TurbineException Any exceptions caught during processing will be 230 * rethrown wrapped into a TurbineException. 231 * @exception Exception A generic exception. 232 */ 233 public static void releaseConnection(Connection dbconn) 234 throws Exception 235 { 236 getPoolBroker().releaseConnection(dbconn); 237 } 238 239 /*** 240 * This method registers a new pool using the given parameters. 241 * 242 * @param name The name of the pool to register. 243 * @param driver The fully-qualified name of the JDBC driver to use. 244 * @param url The URL of the database to use. 245 * @param username The name of the database user. 246 * @param password The password of the database user. 247 * 248 * @throws TurbineException Any exceptions caught during processing will be 249 * rethrown wrapped into a TurbineException. 250 */ 251 /* 252 public static void registerPool( String name, 253 String driver, 254 String url, 255 String username, 256 String password ) 257 throws Exception 258 { 259 getPoolBroker().registerPool(name, driver, url, username, password); 260 } 261 */ 262 /////////////////////////////////////////////////////////////////////////// 263 // DB Adapters 264 /////////////////////////////////////////////////////////////////////////// 265 266 /*** 267 * Returns the database adapter for the default connection pool. 268 * 269 * @return The database adapter. 270 * @throws Exception Any exceptions caught during processing will be 271 * rethrown wrapped into a TurbineException. 272 */ 273 public static DB getDB() 274 throws Exception 275 { 276 return getPoolBroker().getDB(); 277 } 278 279 /*** 280 * Returns database adapter for a specific connection pool. 281 * 282 * @param name A pool name. 283 * @return The corresponding database adapter. 284 * @throws Exception Any exceptions caught during processing will be 285 * rethrown wrapped into a TurbineException. 286 */ 287 public static DB getDB(String name) 288 throws Exception 289 { 290 return getPoolBroker().getDB(name); 291 } 292 293 /////////////////////////////////////////////////////////////////////////// 294 // Service access 295 /////////////////////////////////////////////////////////////////////////// 296 297 /*** 298 * Returns the system's configured MapBrokerService implementation. 299 * 300 * @return a MapBrokerService 301 */ 302 private static MapBrokerService getMapBroker() 303 { 304 return (MapBrokerService)TurbineServices.getInstance().getService( 305 MapBrokerService.SERVICE_NAME); 306 } 307 308 /*** 309 * Returns the system's configured PoolBrokerService implementation. 310 * 311 * @return a PoolBrokerService 312 */ 313 private static PoolBrokerService getPoolBroker() 314 { 315 return (PoolBrokerService)TurbineServices.getInstance().getService( 316 PoolBrokerService.SERVICE_NAME); 317 } 318 }

This page was automatically generated by Maven