2 * Copyright 2000-2018 Vaadin Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.vaadin.v7.data.util.sqlcontainer.connection;
18 import java.io.IOException;
19 import java.io.ObjectOutputStream;
20 import java.sql.Connection;
21 import java.sql.DriverManager;
22 import java.sql.SQLException;
23 import java.sql.Statement;
24 import java.util.HashSet;
25 import java.util.Locale;
29 * Simple implementation of the JDBCConnectionPool interface. Handles loading
30 * the JDBC driver, setting up the connections and ensuring they are still
31 * usable upon release.
33 * @deprecated As of 8.0, no replacement available.
35 @SuppressWarnings("serial")
37 public class SimpleJDBCConnectionPool implements JDBCConnectionPool {
39 private int initialConnections = 5;
40 private int maxConnections = 20;
42 private String driverName;
43 private String connectionUri;
44 private String userName;
45 private String password;
47 private transient Set<Connection> availableConnections;
48 private transient Set<Connection> reservedConnections;
50 private boolean initialized;
52 public SimpleJDBCConnectionPool(String driverName, String connectionUri,
53 String userName, String password) throws SQLException {
54 if (driverName == null) {
55 throw new IllegalArgumentException(
56 "JDBC driver class name must be given.");
58 if (connectionUri == null) {
59 throw new IllegalArgumentException(
60 "Database connection URI must be given.");
62 if (userName == null) {
63 throw new IllegalArgumentException(
64 "Database username must be given.");
66 if (password == null) {
67 throw new IllegalArgumentException(
68 "Database password must be given.");
70 this.driverName = driverName;
71 this.connectionUri = connectionUri;
72 this.userName = userName;
73 this.password = password;
75 /* Initialize JDBC driver */
77 Class.forName(driverName).newInstance();
78 } catch (Exception ex) {
79 throw new RuntimeException("Specified JDBC Driver: " + driverName
80 + " - initialization failed.", ex);
84 public SimpleJDBCConnectionPool(String driverName, String connectionUri,
85 String userName, String password, int initialConnections,
86 int maxConnections) throws SQLException {
87 this(driverName, connectionUri, userName, password);
88 this.initialConnections = initialConnections;
89 this.maxConnections = maxConnections;
92 private void initializeConnections() throws SQLException {
93 availableConnections = new HashSet<Connection>(initialConnections);
94 reservedConnections = new HashSet<Connection>(initialConnections);
95 for (int i = 0; i < initialConnections; i++) {
96 availableConnections.add(createConnection());
102 public synchronized Connection reserveConnection() throws SQLException {
104 initializeConnections();
106 if (availableConnections.isEmpty()) {
107 if (reservedConnections.size() < maxConnections) {
108 availableConnections.add(createConnection());
110 throw new SQLException("Connection limit has been reached.");
114 Connection c = availableConnections.iterator().next();
115 availableConnections.remove(c);
116 reservedConnections.add(c);
122 public synchronized void releaseConnection(Connection conn) {
123 if (conn == null || !initialized) {
126 /* Try to roll back if necessary */
128 if (!conn.getAutoCommit()) {
131 } catch (SQLException e) {
132 /* Roll back failed, close and discard connection */
135 } catch (SQLException e1) {
136 /* Nothing needs to be done */
138 reservedConnections.remove(conn);
141 reservedConnections.remove(conn);
142 availableConnections.add(conn);
145 private Connection createConnection() throws SQLException {
146 Connection c = DriverManager.getConnection(connectionUri, userName,
148 c.setAutoCommit(false);
149 if (driverName.toLowerCase(Locale.ROOT).contains("mysql")) {
151 Statement s = c.createStatement();
152 s.execute("SET SESSION sql_mode = 'ANSI'");
154 } catch (Exception e) {
155 // Failed to set ansi mode; continue
162 public void destroy() {
163 for (Connection c : availableConnections) {
166 } catch (SQLException e) {
167 // No need to do anything
170 for (Connection c : reservedConnections) {
173 } catch (SQLException e) {
174 // No need to do anything
180 private void writeObject(ObjectOutputStream out) throws IOException {
182 out.defaultWriteObject();