You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleJDBCConnectionPool.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  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
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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
  14. * the License.
  15. */
  16. package com.vaadin.v7.data.util.sqlcontainer.connection;
  17. import java.io.IOException;
  18. import java.sql.Connection;
  19. import java.sql.DriverManager;
  20. import java.sql.SQLException;
  21. import java.sql.Statement;
  22. import java.util.HashSet;
  23. import java.util.Set;
  24. /**
  25. * Simple implementation of the JDBCConnectionPool interface. Handles loading
  26. * the JDBC driver, setting up the connections and ensuring they are still
  27. * usable upon release.
  28. */
  29. @SuppressWarnings("serial")
  30. @Deprecated
  31. public class SimpleJDBCConnectionPool implements JDBCConnectionPool {
  32. private int initialConnections = 5;
  33. private int maxConnections = 20;
  34. private String driverName;
  35. private String connectionUri;
  36. private String userName;
  37. private String password;
  38. private transient Set<Connection> availableConnections;
  39. private transient Set<Connection> reservedConnections;
  40. private boolean initialized;
  41. public SimpleJDBCConnectionPool(String driverName, String connectionUri,
  42. String userName, String password) throws SQLException {
  43. if (driverName == null) {
  44. throw new IllegalArgumentException(
  45. "JDBC driver class name must be given.");
  46. }
  47. if (connectionUri == null) {
  48. throw new IllegalArgumentException(
  49. "Database connection URI must be given.");
  50. }
  51. if (userName == null) {
  52. throw new IllegalArgumentException(
  53. "Database username must be given.");
  54. }
  55. if (password == null) {
  56. throw new IllegalArgumentException(
  57. "Database password must be given.");
  58. }
  59. this.driverName = driverName;
  60. this.connectionUri = connectionUri;
  61. this.userName = userName;
  62. this.password = password;
  63. /* Initialize JDBC driver */
  64. try {
  65. Class.forName(driverName).newInstance();
  66. } catch (Exception ex) {
  67. throw new RuntimeException("Specified JDBC Driver: " + driverName
  68. + " - initialization failed.", ex);
  69. }
  70. }
  71. public SimpleJDBCConnectionPool(String driverName, String connectionUri,
  72. String userName, String password, int initialConnections,
  73. int maxConnections) throws SQLException {
  74. this(driverName, connectionUri, userName, password);
  75. this.initialConnections = initialConnections;
  76. this.maxConnections = maxConnections;
  77. }
  78. private void initializeConnections() throws SQLException {
  79. availableConnections = new HashSet<Connection>(initialConnections);
  80. reservedConnections = new HashSet<Connection>(initialConnections);
  81. for (int i = 0; i < initialConnections; i++) {
  82. availableConnections.add(createConnection());
  83. }
  84. initialized = true;
  85. }
  86. @Override
  87. public synchronized Connection reserveConnection() throws SQLException {
  88. if (!initialized) {
  89. initializeConnections();
  90. }
  91. if (availableConnections.isEmpty()) {
  92. if (reservedConnections.size() < maxConnections) {
  93. availableConnections.add(createConnection());
  94. } else {
  95. throw new SQLException("Connection limit has been reached.");
  96. }
  97. }
  98. Connection c = availableConnections.iterator().next();
  99. availableConnections.remove(c);
  100. reservedConnections.add(c);
  101. return c;
  102. }
  103. @Override
  104. public synchronized void releaseConnection(Connection conn) {
  105. if (conn == null || !initialized) {
  106. return;
  107. }
  108. /* Try to roll back if necessary */
  109. try {
  110. if (!conn.getAutoCommit()) {
  111. conn.rollback();
  112. }
  113. } catch (SQLException e) {
  114. /* Roll back failed, close and discard connection */
  115. try {
  116. conn.close();
  117. } catch (SQLException e1) {
  118. /* Nothing needs to be done */
  119. }
  120. reservedConnections.remove(conn);
  121. return;
  122. }
  123. reservedConnections.remove(conn);
  124. availableConnections.add(conn);
  125. }
  126. private Connection createConnection() throws SQLException {
  127. Connection c = DriverManager.getConnection(connectionUri, userName,
  128. password);
  129. c.setAutoCommit(false);
  130. if (driverName.toLowerCase().contains("mysql")) {
  131. try {
  132. Statement s = c.createStatement();
  133. s.execute("SET SESSION sql_mode = 'ANSI'");
  134. s.close();
  135. } catch (Exception e) {
  136. // Failed to set ansi mode; continue
  137. }
  138. }
  139. return c;
  140. }
  141. @Override
  142. public void destroy() {
  143. for (Connection c : availableConnections) {
  144. try {
  145. c.close();
  146. } catch (SQLException e) {
  147. // No need to do anything
  148. }
  149. }
  150. for (Connection c : reservedConnections) {
  151. try {
  152. c.close();
  153. } catch (SQLException e) {
  154. // No need to do anything
  155. }
  156. }
  157. }
  158. private void writeObject(java.io.ObjectOutputStream out)
  159. throws IOException {
  160. initialized = false;
  161. out.defaultWriteObject();
  162. }
  163. }