1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A class represent a DBConnection pool. Another user can use the
|
4
|
* object to initial a connection pool, get db connection or return it.
|
5
|
* Copyright: 2000 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Jing Tao
|
8
|
* Release: @release@
|
9
|
*
|
10
|
* '$Author: tao $'
|
11
|
* '$Date: 2002-05-15 17:29:36 -0700 (Wed, 15 May 2002) $'
|
12
|
* '$Revision: 1087 $'
|
13
|
*
|
14
|
* This program is free software; you can redistribute it and/or modify
|
15
|
* it under the terms of the GNU General Public License as published by
|
16
|
* the Free Software Foundation; either version 2 of the License, or
|
17
|
* (at your option) any later version.
|
18
|
*
|
19
|
* This program is distributed in the hope that it will be useful,
|
20
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
22
|
* GNU General Public License for more details.
|
23
|
*
|
24
|
* You should have received a copy of the GNU General Public License
|
25
|
* along with this program; if not, write to the Free Software
|
26
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
27
|
*/
|
28
|
|
29
|
package edu.ucsb.nceas.metacat;
|
30
|
|
31
|
import java.io.*;
|
32
|
import java.util.Vector;
|
33
|
import java.util.zip.*;
|
34
|
import java.net.URL;
|
35
|
import java.net.MalformedURLException;
|
36
|
import java.sql.*;
|
37
|
import java.util.Stack;
|
38
|
import java.util.Hashtable;
|
39
|
import java.util.Enumeration;
|
40
|
|
41
|
/**
|
42
|
* A class represent a DBConnection pool. Another user can use the
|
43
|
* object to initial a connection pool, get db connection or return it.
|
44
|
* This a singleton class, this means only one instance of this class could
|
45
|
* be in the program at one time.
|
46
|
*/
|
47
|
public class DBConnectionPool
|
48
|
{
|
49
|
private String DBDriver;
|
50
|
private String DBConnectedJDBC;
|
51
|
private String userName;
|
52
|
private String passWord;
|
53
|
private int currentConnectionNumber;
|
54
|
|
55
|
//static attributes
|
56
|
private static DBConnectionPool instance;
|
57
|
private static Vector connectionPool;
|
58
|
|
59
|
|
60
|
final static int maximumConnectionNumber=
|
61
|
Integer.parseInt(MetaCatUtil.getOption("maximumConnections"));
|
62
|
final static int initialConnectionNumber=
|
63
|
Integer.parseInt(MetaCatUtil.getOption("initialConnections"));
|
64
|
|
65
|
/**
|
66
|
* Returns the single instance, creating one if it's the
|
67
|
* first time this method is called.
|
68
|
*/
|
69
|
static synchronized public DBConnectionPool getInstance()
|
70
|
throws SQLException, ClassNotFoundException
|
71
|
{
|
72
|
if (instance == null)
|
73
|
{
|
74
|
instance = new DBConnectionPool();
|
75
|
}
|
76
|
return instance;
|
77
|
}//getInstance
|
78
|
|
79
|
/**
|
80
|
* This is a private constructor since it is singleton
|
81
|
*/
|
82
|
|
83
|
private DBConnectionPool() throws SQLException, ClassNotFoundException
|
84
|
{
|
85
|
DBDriver=MetaCatUtil.getOption("dbDriver");
|
86
|
DBConnectedJDBC=MetaCatUtil.getOption("defaultDB");
|
87
|
userName=MetaCatUtil.getOption("user");
|
88
|
passWord=MetaCatUtil.getOption("password");
|
89
|
}//DBConnection
|
90
|
|
91
|
/**
|
92
|
* Method to establish a JDBC database connection
|
93
|
*/
|
94
|
private Connection openConnection()
|
95
|
throws SQLException, ClassNotFoundException
|
96
|
{
|
97
|
return openConnection(DBDriver, DBConnectedJDBC, userName, passWord);
|
98
|
}//openDBConnection
|
99
|
|
100
|
/**
|
101
|
* Method to establish a JDBC database connection
|
102
|
*
|
103
|
* @param dbDriver the string representing the database driver
|
104
|
* @param connection the string representing the database connectin parameters
|
105
|
* @param user name of the user to use for database connection
|
106
|
* @param password password for the user to use for database connection
|
107
|
*/
|
108
|
private Connection openConnection(String dbDriver, String connection,
|
109
|
String user, String password)
|
110
|
throws SQLException, ClassNotFoundException
|
111
|
{
|
112
|
|
113
|
// Load the Oracle JDBC driver
|
114
|
Class.forName (dbDriver);
|
115
|
// Connect to the database
|
116
|
Connection conn = DriverManager.getConnection( connection, user, password);
|
117
|
return conn;
|
118
|
}//OpenDBConnection
|
119
|
|
120
|
/**
|
121
|
* Method to initial a pool of DBConnection objects
|
122
|
*/
|
123
|
public Vector initialDBConnectionPool()
|
124
|
throws SQLException, ClassNotFoundException
|
125
|
{
|
126
|
|
127
|
Connection conn = null;
|
128
|
DBConnection dbConn = null;
|
129
|
|
130
|
for ( int i = 0; i < initialConnectionNumber; i++ )
|
131
|
{
|
132
|
//open a connection to db
|
133
|
conn = openConnection();
|
134
|
//create a new object of DBConnection
|
135
|
dbConn = new DBConnection();
|
136
|
//set connetion to DBConnection
|
137
|
dbConn.setDBConnection(conn);
|
138
|
//put DBConnection into vetor
|
139
|
connectionPool.add(dbConn);
|
140
|
}
|
141
|
|
142
|
return connectionPool;
|
143
|
}
|
144
|
|
145
|
|
146
|
|
147
|
|
148
|
|
149
|
}//DBConnectionPool
|