1. /*
  2. * @(#)PooledConnection.java 1.12 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.sql;
  8. import java.sql.Connection;
  9. import java.sql.SQLException;
  10. /**
  11. * An object that provides hooks for connection pool management.
  12. * A <code>PooledConnection</code> object
  13. * represents a physical connection to a data source. The connection
  14. * can be recycled rather than being closed when an application is
  15. * finished with it, thus reducing the number of connections that
  16. * need to be made.
  17. * <P>
  18. * An application programmer does not use the <code>PooledConnection</code>
  19. * interface directly; rather, it is used by a middle tier infrastructure
  20. * that manages the pooling of connections.
  21. * <P>
  22. * When an application calls the method <code>DataSource.getConnection</code>,
  23. * it gets back a <code>Connection</code> object. If connection pooling is
  24. * being done, that <code>Connection</code> object is actually a handle to
  25. * a <code>PooledConnection</code> object, which is a physical connection.
  26. * <P>
  27. * The connection pool manager, typically the application server, maintains
  28. * a pool of <code>PooledConnection</code> objects. If there is a
  29. * <code>PooledConnection</code> object available in the pool, the
  30. * connection pool manager returns a <code>Connection</code> object that
  31. * is a handle to that physical connection.
  32. * If no <code>PooledConnection</code> object is available, the
  33. * connection pool manager calls the <code>PooledConnection</code>
  34. * method <code>getConnection</code> to create a new physical connection and
  35. * returns a handle to it.
  36. * <P>
  37. * When an application closes a connection, it calls the <code>Connection</code>
  38. * method <code>close</code>. When connection pooling is being done,
  39. * the connection pool manager is notified because it has registered itself as
  40. * a <code>ConnectionEventListener</code> object using the
  41. * <code>ConnectionPool</code> method <code>addConnectionEventListener</code>.
  42. * The connection pool manager deactivates the handle to
  43. * the <code>PooledConnection</code> object and returns the
  44. * <code>PooledConnection</code> object to the pool of connections so that
  45. * it can be used again. Thus, when an application closes its connection,
  46. * the underlying physical connection is recycled rather than being closed.
  47. * <P>
  48. * The physical connection is not closed until the connection pool manager
  49. * calls the <code>PooledConnection</code> method <code>close</code>.
  50. * This method is generally called to have an orderly shutdown of the server or
  51. * if a fatal error has made the connection unusable.
  52. *
  53. * @since 1.4
  54. */
  55. public interface PooledConnection {
  56. /**
  57. * Creates and returns a <code>Connection</code> object that is a handle
  58. * for the physical connection that
  59. * this <code>PooledConnection</code> object represents.
  60. * The connection pool manager calls this method when an application has
  61. * called the method <code>DataSource.getConnection</code> and there are
  62. * no <code>PooledConnection</code> objects available. See the
  63. * {@link PooledConnection interface description} for more information.
  64. *
  65. * @return a <code>Connection</code> object that is a handle to
  66. * this <code>PooledConnection</code> object
  67. * @exception SQLException if a database access error occurs
  68. */
  69. Connection getConnection() throws SQLException;
  70. /**
  71. * Closes the physical connection that this <code>PooledConnection</code>
  72. * object represents. An application never calls this method directly;
  73. * it is called by the connection pool module, or manager.
  74. * <P>
  75. * See the {@link PooledConnection interface description} for more
  76. * information.
  77. *
  78. * @exception SQLException if a database access error occurs
  79. */
  80. void close() throws SQLException;
  81. /**
  82. * Registers the given event listener so that it will be notified
  83. * when an event occurs on this <code>PooledConnection</code> object.
  84. *
  85. * @param listener a component, usually the connection pool manager,
  86. * that has implemented the
  87. * <code>ConnectionEventListener</code> interface and wants to be
  88. * notified when the connection is closed or has an error
  89. * @see #removeConnectionEventListener
  90. */
  91. void addConnectionEventListener(ConnectionEventListener listener);
  92. /**
  93. * Removes the given event listener from the list of components that
  94. * will be notified when an event occurs on this
  95. * <code>PooledConnection</code> object.
  96. *
  97. * @param listener a component, usually the connection pool manager,
  98. * that has implemented the
  99. * <code>ConnectionEventListener</code> interface and
  100. * been registered with this <code>PooledConnection</code> object as
  101. * a listener
  102. * @see #addConnectionEventListener
  103. */
  104. void removeConnectionEventListener(ConnectionEventListener listener);
  105. }