DBConnection.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 5k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  3.  * All rights reserved.
  4.  * 
  5.  * Redistribution and use in source and binary forms, 
  6.  * with or without modification, are permitted provided 
  7.  * that the following conditions are met:
  8.  * 
  9.  * 1) Redistributions of source code must retain the above 
  10.  * copyright notice, this list of conditions and the 
  11.  * following  disclaimer.
  12.  * 2)  Redistributions in binary form must reproduce the 
  13.  * above copyright notice, this list of conditions and 
  14.  * the following disclaimer in the documentation and/or 
  15.  * other materials provided with the distribution.
  16.  * 3) Neither the name of "Rafael Steil" nor 
  17.  * the names of its contributors may be used to endorse 
  18.  * or promote products derived from this software without 
  19.  * specific prior written permission.
  20.  * 
  21.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
  22.  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
  24.  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  26.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
  27.  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  29.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  30.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  31.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
  32.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  33.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
  34.  * IN CONTRACT, STRICT LIABILITY, OR TORT 
  35.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  37.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  38.  * 
  39.  * This file creation date: 25/08/2004 23:03:14
  40.  * The JForum Project
  41.  * http://www.jforum.net
  42.  */
  43. package net.jforum;
  44. import java.sql.Connection;
  45. import net.jforum.util.preferences.ConfigKeys;
  46. import net.jforum.util.preferences.SystemGlobals;
  47. import org.apache.log4j.Logger;
  48. /**
  49.  * Base class for all database connection implementations that
  50.  * may be used with JForum.
  51.  * Default implementations are <code>PooledConnection</code>, which
  52.  * is the defeault connection pool implementation, and <code>SimpleConnection</code>,
  53.  * which opens a new connection on every request.  
  54.  * 
  55.  * @author Rafael Steil
  56.  * @version $Id: DBConnection.java,v 1.14 2006/08/23 02:24:06 rafaelsteil Exp $
  57.  */
  58. public abstract class DBConnection 
  59. {
  60. private static final Logger logger = Logger.getLogger(DBConnection.class);
  61. protected boolean isDatabaseUp;
  62. private static DBConnection instance;
  63. /**
  64.  * Creates an instance of some <code>DBConnection </code>implementation. 
  65.  * 
  66.  * @return <code>true</code> if the instance was successfully created, 
  67.  * or <code>false</code> if some exception was thrown.
  68.  */
  69. public static boolean createInstance()
  70. {
  71. try {
  72. instance = (DBConnection)Class.forName(SystemGlobals.getValue(
  73. ConfigKeys.DATABASE_CONNECTION_IMPLEMENTATION)).newInstance();
  74. }
  75. catch (Exception e) {
  76.  logger.warn("Error creating the database connection implementation instance. " + e);
  77.  e.printStackTrace();
  78.  return false;
  79. }
  80. return true;
  81. }
  82. /**
  83.  * Gets the current <code>DBConnection</code> implementation's instance
  84.  * 
  85.  * @return DBConnection
  86.  */
  87. public static DBConnection getImplementation()
  88. {
  89. return instance;
  90. }
  91. /**
  92.  * Checks if database connection is up.
  93.  *  
  94.  * @return <code>true</code> if a connection to the database
  95.  * was successfully created, or <code>false</code> if not.
  96.  */
  97. public boolean isDatabaseUp()
  98. {
  99. return this.isDatabaseUp;
  100. }
  101. /**
  102.  * Inits the implementation. 
  103.  * Connection pools may use this method to init the connections from the
  104.  * database, while non-pooled implementation can provide an empty method
  105.  * block if no other initialization is necessary.
  106.  * <br>
  107.  * Please note that this method will be called just once, at system startup. 
  108.  * 
  109.  * @throws Exception
  110.  */
  111. public abstract void init() throws Exception;
  112. /**
  113.  * Gets a connection.
  114.  * Connection pools' normal behaviour will be to once connection
  115.  * from the pool, while non-pooled implementations will want to
  116.  * go to the database and get the connection in time the method
  117.  * is called.
  118.  * 
  119.  * @return Connection
  120.  */
  121. public abstract Connection getConnection();
  122. /**
  123.  * Releases a connection.
  124.  * Connection pools will want to put the connection back to the pool list,
  125.  * while non-pooled implementations should call <code>close()</code> directly
  126.  * in the connection object.
  127.  * 
  128.  * @param conn The connection to release
  129.  */
  130. public abstract void releaseConnection(Connection conn);
  131. /**
  132.  * Close all open connections.
  133.  * 
  134.  * @throws Exception
  135.  */
  136. public abstract void realReleaseAllConnections() throws Exception;
  137. }