001: /*
002: * Copyright 2003 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package velosurf.sql;
018:
019: import velosurf.util.Strings;
020: import velosurf.util.Logger;
021:
022: import java.sql.*;
023: import java.util.List;
024: import java.util.Iterator;
025: import java.util.ArrayList;
026:
027: /**
028: * Connection pool.
029: *
030: * @author <a href="mailto:claude.brisson@gmail.com">Claude Brisson</a>
031: */
032:
033: public class ConnectionPool {
034:
035: /**
036: * Constructor.
037: * @param url url
038: * @param user user
039: * @param password password
040: * @param schema schema
041: * @param driver infos on the driver
042: * @param autocommit autocommit
043: * @param min min connections
044: * @param max max connections
045: * @throws SQLException
046: */
047: public ConnectionPool(String url, String user, String password,
048: String schema, DriverInfo driver, boolean autocommit,
049: int min, int max) throws SQLException {
050: this .user = user;
051: this .password = password;
052: this .url = url;
053: this .schema = schema;
054: this .driver = driver;
055: this .autocommit = autocommit;
056: connections = new ArrayList<ConnectionWrapper>();
057: this .min = min;
058: this .max = max;
059:
060: for (int i = 0; i < this .min; i++) {
061: connections.add(createConnection());
062: }
063: }
064:
065: /**
066: * Get a connection.
067: * @return a connection
068: * @throws SQLException
069: */
070: public synchronized ConnectionWrapper getConnection()
071: throws SQLException {
072: for (Iterator it = connections.iterator(); it.hasNext();) {
073: ConnectionWrapper c = (ConnectionWrapper) it.next();
074: if (c.isClosed()) {
075: it.remove();
076: } else if (!c.isBusy()) {
077: return c;
078: }
079: }
080: if (connections.size() == max) {
081: Logger
082: .warn("Connection pool: max number of connections reached! ");
083: // return a busy connection...
084: return (ConnectionWrapper) connections.get(0);
085: }
086: ConnectionWrapper newconn = createConnection();
087: connections.add(newconn);
088: return newconn;
089: }
090:
091: /** Create a connection.
092: *
093: * @return connection
094: * @throws SQLException
095: */
096: private ConnectionWrapper createConnection() throws SQLException {
097:
098: Logger.info("Creating a new connection.");
099: Connection connection = DriverManager.getConnection(url, user,
100: password);
101:
102: // schema
103: if (schema != null) {
104: String schemaQuery = driver.getSchemaQuery();
105: if (schemaQuery != null) {
106: schemaQuery = Strings.replace(schemaQuery, "$schema",
107: schema);
108: Statement stmt = connection.createStatement();
109: stmt.executeUpdate(schemaQuery);
110: stmt.close();
111: }
112: }
113:
114: // autocommit
115: connection.setAutoCommit(autocommit);
116:
117: return new ConnectionWrapper(driver, connection);
118: }
119:
120: /*
121: private String getSchema(Connection connection) throws SQLException
122: {
123: Statement stmt = connection.createStatement();
124: ResultSet rs = stmt.executeQuery("select sys_context('userenv','current_schema') from dual");
125: rs.next();
126: return rs.getString(1);
127: }*/
128:
129: /**
130: * clear all connections.
131: */
132: public void clear() {
133: for (Iterator it = connections.iterator(); it.hasNext();) {
134: ConnectionWrapper c = (ConnectionWrapper) it.next();
135: try {
136: c.close();
137: } catch (SQLException sqle) {
138: }
139: }
140: }
141:
142: /** user */
143: private String user = null;
144: /** password */
145: private String password = null;
146: /** database url */
147: private String url = null;
148: /** optional schema */
149: private String schema = null;
150: /** infos on the driver */
151: private DriverInfo driver = null;
152: /** autocommit flag */
153: private boolean autocommit = true;
154: /** list of all connections */
155: private List<ConnectionWrapper> connections = null;
156:
157: /** Minimum number of connections. */
158: private int min;
159:
160: /** Maximum number of connections. */
161: private int max;
162:
163: }
|