1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.pool;
  17. /**
  18. * A pooling interface.
  19. * <p>
  20. * <code>ObjectPool</code> defines a trivially simple pooling interface. The only
  21. * required methods are {@link #borrowObject borrowObject} and {@link #returnObject returnObject}.
  22. * <p>
  23. * Example of use:
  24. * <table border="1" cellspacing="0" cellpadding="3" align="center" bgcolor="#FFFFFF"><tr><td><pre>
  25. * Object obj = <font color="#0000CC">null</font>
  26. *
  27. * <font color="#0000CC">try</font> {
  28. * obj = pool.borrowObject();
  29. * <font color="#00CC00">//...use the object...</font>
  30. * } <font color="#0000CC">catch</font>(Exception e) {
  31. * <font color="#00CC00">//...handle any exceptions...</font>
  32. * } <font color="#0000CC">finally</font> {
  33. * <font color="#00CC00">// make sure the object is returned to the pool</font>
  34. * <font color="#0000CC">if</font>(<font color="#0000CC">null</font> != obj) {
  35. * pool.returnObject(obj);
  36. * }
  37. * }</pre></td></tr></table>
  38. * See {@link org.apache.commons.pool.BaseObjectPool BaseObjectPool} for a simple base implementation.
  39. *
  40. * @author Rodney Waldhoff
  41. * @version $Revision: 1.11 $ $Date: 2004/02/28 11:46:33 $
  42. *
  43. */
  44. public interface ObjectPool {
  45. /**
  46. * Obtain an instance from my pool.
  47. * By contract, clients MUST return
  48. * the borrowed instance using
  49. * {@link #returnObject(java.lang.Object) returnObject}
  50. * or a related method as defined in an implementation
  51. * or sub-interface.
  52. * <p>
  53. * The behaviour of this method when the pool has been exhausted
  54. * is not specified (although it may be specified by implementations).
  55. *
  56. * @return an instance from my pool.
  57. */
  58. Object borrowObject() throws Exception;
  59. /**
  60. * Return an instance to my pool.
  61. * By contract, <i>obj</i> MUST have been obtained
  62. * using {@link #borrowObject() borrowObject}
  63. * or a related method as defined in an implementation
  64. * or sub-interface.
  65. *
  66. * @param obj a {@link #borrowObject borrowed} instance to be returned.
  67. */
  68. void returnObject(Object obj) throws Exception;
  69. /**
  70. * Invalidates an object from the pool
  71. * By contract, <i>obj</i> MUST have been obtained
  72. * using {@link #borrowObject() borrowObject}
  73. * or a related method as defined in an implementation
  74. * or sub-interface.
  75. * <p>
  76. * This method should be used when an object that has been borrowed
  77. * is determined (due to an exception or other problem) to be invalid.
  78. * If the connection should be validated before or after borrowing,
  79. * then the {@link PoolableObjectFactory#validateObject} method should be
  80. * used instead.
  81. *
  82. * @param obj a {@link #borrowObject borrowed} instance to be returned.
  83. */
  84. void invalidateObject(Object obj) throws Exception;
  85. /**
  86. * Create an object using my {@link #setFactory factory} or other
  87. * implementation dependent mechanism, and place it into the pool.
  88. * addObject() is useful for "pre-loading" a pool with idle objects.
  89. * (Optional operation).
  90. */
  91. void addObject() throws Exception;
  92. /**
  93. * Return the number of instances
  94. * currently idle in my pool (optional operation).
  95. * This may be considered an approximation of the number
  96. * of objects that can be {@link #borrowObject borrowed}
  97. * without creating any new instances.
  98. *
  99. * @return the number of instances currently idle in my pool
  100. * @throws UnsupportedOperationException if this implementation does not support the operation
  101. */
  102. int getNumIdle() throws UnsupportedOperationException;
  103. /**
  104. * Return the number of instances
  105. * currently borrowed from my pool
  106. * (optional operation).
  107. *
  108. * @return the number of instances currently borrowed in my pool
  109. * @throws UnsupportedOperationException if this implementation does not support the operation
  110. */
  111. int getNumActive() throws UnsupportedOperationException;
  112. /**
  113. * Clears any objects sitting idle in the pool, releasing any
  114. * associated resources (optional operation).
  115. *
  116. * @throws UnsupportedOperationException if this implementation does not support the operation
  117. */
  118. void clear() throws Exception, UnsupportedOperationException;
  119. /**
  120. * Close this pool, and free any resources associated with it.
  121. */
  122. void close() throws Exception;
  123. /**
  124. * Sets the {@link PoolableObjectFactory factory} I use
  125. * to create new instances (optional operation).
  126. * @param factory the {@link PoolableObjectFactory} I use to create new instances.
  127. *
  128. * @throws IllegalStateException when the factory cannot be set at this time
  129. * @throws UnsupportedOperationException if this implementation does not support the operation
  130. */
  131. void setFactory(PoolableObjectFactory factory) throws IllegalStateException, UnsupportedOperationException;
  132. }