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.

ValidatingSimpleJDBCConnectionPool.java 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package com.vaadin.v7.data.util.sqlcontainer.query;
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6. import java.util.logging.Logger;
  7. import com.vaadin.v7.data.util.sqlcontainer.connection.JDBCConnectionPool;
  8. import com.vaadin.v7.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool;
  9. /**
  10. * Connection pool for testing SQLContainer. Ensures that only reserved
  11. * connections are released and that all connections are released before the
  12. * pool is destroyed.
  13. *
  14. * @author Vaadin Ltd
  15. */
  16. public class ValidatingSimpleJDBCConnectionPool implements JDBCConnectionPool {
  17. private JDBCConnectionPool realPool;
  18. private Set<Connection> reserved = new HashSet<Connection>();
  19. private Set<Connection> alreadyReleased = new HashSet<Connection>();
  20. public ValidatingSimpleJDBCConnectionPool(String driverName,
  21. String connectionUri, String userName, String password,
  22. int initialConnections, int maxConnections) throws SQLException {
  23. realPool = new SimpleJDBCConnectionPool(driverName, connectionUri,
  24. userName, password, initialConnections, maxConnections);
  25. }
  26. @Deprecated
  27. public JDBCConnectionPool getRealPool() {
  28. return realPool;
  29. }
  30. @Override
  31. public Connection reserveConnection() throws SQLException {
  32. Connection c = realPool.reserveConnection();
  33. reserved.add(c);
  34. return c;
  35. }
  36. @Override
  37. public void releaseConnection(Connection conn) {
  38. if (conn != null && !reserved.remove(conn)) {
  39. if (alreadyReleased.contains(conn)) {
  40. getLogger().severe("Tried to release connection (" + conn
  41. + ") which has already been released");
  42. } else {
  43. throw new RuntimeException("Tried to release connection ("
  44. + conn + ") not reserved using reserveConnection");
  45. }
  46. }
  47. realPool.releaseConnection(conn);
  48. alreadyReleased.add(conn);
  49. }
  50. @Override
  51. public void destroy() {
  52. realPool.destroy();
  53. if (!reserved.isEmpty()) {
  54. throw new RuntimeException(
  55. reserved.size() + " connections never released");
  56. }
  57. }
  58. private static Logger getLogger() {
  59. return Logger
  60. .getLogger(ValidatingSimpleJDBCConnectionPool.class.getName());
  61. }
  62. }