KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > DBConnection


1 /*
2  * Copyright (c) 2003, Rafael Steil
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
45 import java.sql.Connection JavaDoc;
46
47 import net.jforum.util.preferences.ConfigKeys;
48 import net.jforum.util.preferences.SystemGlobals;
49
50 import org.apache.log4j.Logger;
51
52 /**
53  * Base class for all database connection implementations that
54  * may be used with JForum.
55  * Default implementations are <code>PooledConnection</code>, which
56  * is the defeault connection pool implementation, and <code>SimpleConnection</code>,
57  * which opens a new connection on every request.
58  *
59  * @author Rafael Steil
60  * @version $Id: DBConnection.java,v 1.11 2005/07/26 04:01:17 diegopires Exp $
61  */

62 public abstract class DBConnection
63 {
64     private static final Logger logger = Logger.getLogger(DBConnection.class);
65     protected boolean isDatabaseUp;
66     
67     private static DBConnection instance;
68
69     /**
70      * Creates an instance of some <code>DBConnection </code>implementation.
71      *
72      * @return <code>true</code> if the instance was successfully created,
73      * or <code>false</code> if some exception was thrown.
74      */

75     public static final boolean createInstance()
76     {
77         try {
78             instance = (DBConnection)Class.forName(SystemGlobals.getValue(
79                     ConfigKeys.DATABASE_CONNECTION_IMPLEMENTATION)).newInstance();
80         }
81         catch (Exception JavaDoc e) {
82              logger.warn("Error creating the database connection implementation instance. " + e);
83              e.printStackTrace();
84              return false;
85         }
86         
87         return true;
88     }
89     
90     /**
91      * Gets the current <code>DBConnection</code> implementation's instance
92      *
93      * @return
94      */

95     public static DBConnection getImplementation()
96     {
97         return instance;
98     }
99     
100     /**
101      * Checks if database connection is up.
102      *
103      * @return <code>true</code> if a connection to the database
104      * was successfully created, or <code>false</code> if not.
105      */

106     public boolean isDatabaseUp()
107     {
108         return this.isDatabaseUp;
109     }
110     
111     /**
112      * Inits the implementation.
113      * Connection pools may use this method to init the connections from the
114      * database, while non-pooled implementation can provide an empty method
115      * block if no other initialization is necessary.
116      * <br>
117      * Please note that this method will be called just once, at system startup.
118      *
119      * @throws Exception
120      */

121     public abstract void init() throws Exception JavaDoc;
122     
123     /**
124      * Gets a connection.
125      * Connection pools' normal behaviour will be to once connection
126      * from the pool, while non-pooled implementations will want to
127      * go to the database and get the connection in time the method
128      * is called.
129      *
130      * @return
131      * @throws Exception
132      */

133     public abstract Connection JavaDoc getConnection();
134     
135     /**
136      * Releases a connection.
137      * Connection pools will want to put the connection back to the pool list,
138      * while non-pooled implementations should call <code>close()</code> directly
139      * in the connection object.
140      *
141      * @param conn The connection to release
142      * @throws Exception
143      */

144     public abstract void releaseConnection(Connection JavaDoc conn);
145     
146     /**
147      * Close all open connections.
148      *
149      * @throws Exception
150      */

151     public abstract void realReleaseAllConnections() throws Exception JavaDoc;
152 }
153
Popular Tags