Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SimpleJDBCConnectionPool.java 5.4KB

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