1 |
4221
|
leinfelder
|
/**
|
2 |
|
|
* '$RCSfile: PostgresDatabaseConnectionPool.java,v $'
|
3 |
|
|
*
|
4 |
|
|
* '$Author: leinfelder $'
|
5 |
|
|
* '$Date: 2007/10/19 18:50:11 $'
|
6 |
|
|
* '$Revision: 1.1 $'
|
7 |
|
|
*
|
8 |
|
|
* For Details: http://ecoinformatics.org
|
9 |
|
|
*
|
10 |
|
|
* Copyright (c) 2007 The Regents of the University of California.
|
11 |
|
|
* All rights reserved.
|
12 |
|
|
*
|
13 |
|
|
* Permission is hereby granted, without written agreement and without
|
14 |
|
|
* license or royalty fees, to use, copy, modify, and distribute this
|
15 |
|
|
* software and its documentation for any purpose, provided that the
|
16 |
|
|
* above copyright notice and the following two paragraphs appear in
|
17 |
|
|
* all copies of this software.
|
18 |
|
|
*
|
19 |
|
|
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
|
20 |
|
|
* FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
21 |
|
|
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
22 |
|
|
* IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY
|
23 |
|
|
* OF SUCH DAMAGE.
|
24 |
|
|
*
|
25 |
|
|
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
|
26 |
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
27 |
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
|
28 |
|
|
* PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY
|
29 |
|
|
* OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
|
30 |
|
|
* UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
31 |
|
|
*/
|
32 |
|
|
package edu.ucsb.nceas.metacat.dataquery;
|
33 |
|
|
|
34 |
|
|
import java.sql.Connection;
|
35 |
|
|
import java.sql.SQLException;
|
36 |
|
|
|
37 |
|
|
import org.apache.commons.logging.Log;
|
38 |
|
|
import org.apache.commons.logging.LogFactory;
|
39 |
|
|
import org.ecoinformatics.datamanager.database.ConnectionNotAvailableException;
|
40 |
|
|
import org.ecoinformatics.datamanager.database.DatabaseConnectionPoolInterface;
|
41 |
|
|
import org.postgresql.jdbc3.Jdbc3PoolingDataSource;
|
42 |
|
|
|
43 |
5030
|
daigle
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
44 |
4221
|
leinfelder
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
45 |
|
|
|
46 |
|
|
/**
|
47 |
|
|
* This class implements DataConnetionPoolInterface to provide a connection for
|
48 |
|
|
* testing. Database information in this class will be read from property file.
|
49 |
|
|
*
|
50 |
|
|
* @author tao
|
51 |
|
|
*
|
52 |
|
|
*/
|
53 |
|
|
public class PostgresDatabaseConnectionPool implements
|
54 |
|
|
DatabaseConnectionPoolInterface {
|
55 |
|
|
|
56 |
|
|
public static Log log = LogFactory.getLog(PostgresDatabaseConnectionPool.class);
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
/* Configuration properties file */
|
60 |
|
|
private static String serverName = null;
|
61 |
|
|
private static String databaseName = null;
|
62 |
|
|
private static String user = null;
|
63 |
|
|
private static String password = null;
|
64 |
|
|
private static int maxConnections = 0;
|
65 |
|
|
private static String databaseAdapterName = null;
|
66 |
|
|
|
67 |
|
|
private static Jdbc3PoolingDataSource source = null;
|
68 |
|
|
|
69 |
|
|
private static int connCount = 0;
|
70 |
|
|
|
71 |
|
|
/**
|
72 |
|
|
* Constructor. Loading database parameter from property file
|
73 |
|
|
*
|
74 |
|
|
*/
|
75 |
|
|
public PostgresDatabaseConnectionPool() {
|
76 |
|
|
try {
|
77 |
|
|
loadOptions();
|
78 |
|
|
}
|
79 |
|
|
catch (PropertyNotFoundException e) {
|
80 |
|
|
log.error(e.getMessage());
|
81 |
|
|
e.printStackTrace();
|
82 |
|
|
}
|
83 |
|
|
initPool();
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
private static void initPool() {
|
87 |
|
|
source = new Jdbc3PoolingDataSource();
|
88 |
|
|
//source.setDataSourceName(databaseAdapterName);
|
89 |
|
|
source.setServerName(serverName);
|
90 |
|
|
source.setDatabaseName(databaseName);
|
91 |
|
|
source.setUser(user);
|
92 |
|
|
source.setPassword(password);
|
93 |
|
|
source.setMaxConnections(maxConnections);
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
/**
|
97 |
|
|
* Loads Data Manager options from a configuration file.
|
98 |
|
|
* @throws PropertyNotFoundException
|
99 |
|
|
*/
|
100 |
|
|
private static void loadOptions() throws PropertyNotFoundException {
|
101 |
|
|
|
102 |
|
|
serverName = PropertyService.getProperty("datamanager.server");
|
103 |
|
|
databaseName = PropertyService.getProperty("datamanager.database");
|
104 |
|
|
user = PropertyService.getProperty("datamanager.user");
|
105 |
|
|
password = PropertyService.getProperty("datamanager.password");
|
106 |
|
|
maxConnections =
|
107 |
|
|
Integer.parseInt(
|
108 |
|
|
PropertyService.getProperty("datamanager.maxconnections"));
|
109 |
|
|
databaseAdapterName = PropertyService.getProperty("datamanager.adapter");
|
110 |
|
|
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
/**
|
114 |
|
|
* Get dabase adpater name.
|
115 |
|
|
*
|
116 |
|
|
* @return database adapter name
|
117 |
|
|
*/
|
118 |
|
|
public String getDBAdapterName() {
|
119 |
|
|
return databaseAdapterName;
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
/**
|
123 |
|
|
* Gets a database connection from the pool
|
124 |
|
|
*
|
125 |
|
|
* @return checked out connection
|
126 |
|
|
* @throws SQLException
|
127 |
|
|
*/
|
128 |
|
|
public Connection getConnection() throws SQLException,
|
129 |
|
|
ConnectionNotAvailableException {
|
130 |
|
|
Connection connection = null;
|
131 |
|
|
|
132 |
|
|
try {
|
133 |
|
|
connection = source.getConnection();
|
134 |
|
|
connCount++;
|
135 |
|
|
} catch (SQLException e) {
|
136 |
|
|
System.err.println("SQLException: " + e.getMessage());
|
137 |
|
|
throw (e);
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
return connection;
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
/**
|
144 |
|
|
* Returns checked out dabase connection to the pool
|
145 |
|
|
*
|
146 |
|
|
* @param conn
|
147 |
|
|
* Connection needs to be returned.
|
148 |
|
|
* @return indicator if the connection was returned successfully
|
149 |
|
|
*/
|
150 |
|
|
public boolean returnConnection(Connection conn) {
|
151 |
|
|
boolean success = false;
|
152 |
|
|
|
153 |
|
|
try {
|
154 |
|
|
conn.close();
|
155 |
|
|
success = true;
|
156 |
|
|
connCount--;
|
157 |
|
|
} catch (Exception e) {
|
158 |
|
|
success = false;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
//log.debug(Thread.currentThread().getName() + ": connection count=" + connCount);
|
162 |
|
|
|
163 |
|
|
return success;
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
public static void main(String arg[]) {
|
167 |
|
|
PostgresDatabaseConnectionPool pool = new PostgresDatabaseConnectionPool();
|
168 |
|
|
try {
|
169 |
|
|
Connection conn = pool.getConnection();
|
170 |
|
|
log.debug("conn=" + conn);
|
171 |
|
|
conn.close();
|
172 |
|
|
} catch (SQLException e) {
|
173 |
|
|
// TODO Auto-generated catch block
|
174 |
|
|
e.printStackTrace();
|
175 |
|
|
} catch (ConnectionNotAvailableException e) {
|
176 |
|
|
// TODO Auto-generated catch block
|
177 |
|
|
e.printStackTrace();
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
}
|