1. /*
  2. * @(#)DataSource.java 1.8 03/01/23
  3. *
  4. * Copyright 2003 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. * <p>A factory for connections to the physical data source that this
  12. * <code>DataSource</code> object represents. An alternative to the
  13. * <code>DriverManager</code> facility, a <code>DataSource</code> object
  14. * is the preferred means of getting a connection. An object that implements
  15. * the <code>DataSource</code> interface will typically be
  16. * registered with a naming service based on the
  17. * Java<sup><font size=-2>TM</font></sup> Naming and Directory (JNDI) API.
  18. * <P>
  19. * The <code>DataSource</code> interface is implemented by a driver vendor.
  20. * There are three types of implementations:
  21. * <OL>
  22. * <LI>Basic implementation -- produces a standard <code>Connection</code>
  23. * object
  24. * <LI>Connection pooling implementation -- produces a <code>Connection</code>
  25. * object that will automatically participate in connection pooling. This
  26. * implementation works with a middle-tier connection pooling manager.
  27. * <LI>Distributed transaction implementation -- produces a
  28. * <code>Connection</code> object that may be used for distributed
  29. * transactions and almost always participates in connection pooling.
  30. * This implementation works with a middle-tier
  31. * transaction manager and almost always with a connection
  32. * pooling manager.
  33. * </OL>
  34. * <P>
  35. * A <code>DataSource</code> object has properties that can be modified
  36. * when necessary. For example, if the data source is moved to a different
  37. * server, the property for the server can be changed. The benefit is that
  38. * because the data source's properties can be changed, any code accessing
  39. * that data source does not need to be changed.
  40. * <P>
  41. * A driver that is accessed via a <code>DataSource</code> object does not
  42. * register itself with the <code>DriverManager</code>. Rather, a
  43. * <code>DataSource</code> object is retrieved though a lookup operation
  44. * and then used to create a <code>Connection</code> object. With a basic
  45. * implementation, the connection obtained through a <code>DataSource</code>
  46. * object is identical to a connection obtained through the
  47. * <code>DriverManager</code> facility.
  48. *
  49. * @since 1.4
  50. */
  51. public interface DataSource {
  52. /**
  53. * <p>Attempts to establish a connection with the data source that
  54. * this <code>DataSource</code> object represents.
  55. *
  56. * @return a connection to the data source
  57. * @exception SQLException if a database access error occurs
  58. */
  59. Connection getConnection() throws SQLException;
  60. /**
  61. * <p>Attempts to establish a connection with the data source that
  62. * this <code>DataSource</code> object represents.
  63. *
  64. * @param username the database user on whose behalf the connection is
  65. * being made
  66. * @param password the user's password
  67. * @return a connection to the data source
  68. * @exception SQLException if a database access error occurs
  69. */
  70. Connection getConnection(String username, String password)
  71. throws SQLException;
  72. /**
  73. * <p>Retrieves the log writer for this <code>DataSource</code>
  74. * object.
  75. *
  76. * <p>The log writer is a character output stream to which all logging
  77. * and tracing messages for this data source will be
  78. * printed. This includes messages printed by the methods of this
  79. * object, messages printed by methods of other objects manufactured
  80. * by this object, and so on. Messages printed to a data source
  81. * specific log writer are not printed to the log writer associated
  82. * with the <code>java.sql.Drivermanager</code> class. When a
  83. * <code>DataSource</code> object is
  84. * created, the log writer is initially null; in other words, the
  85. * default is for logging to be disabled.
  86. *
  87. * @return the log writer for this data source or null if
  88. * logging is disabled
  89. * @exception SQLException if a database access error occurs
  90. * @see #setLogWriter
  91. */
  92. java.io.PrintWriter getLogWriter() throws SQLException;
  93. /**
  94. * <p>Sets the log writer for this <code>DataSource</code>
  95. * object to the given <code>java.io.PrintWriter</code> object.
  96. *
  97. * <p>The log writer is a character output stream to which all logging
  98. * and tracing messages for this data source will be
  99. * printed. This includes messages printed by the methods of this
  100. * object, messages printed by methods of other objects manufactured
  101. * by this object, and so on. Messages printed to a data source-
  102. * specific log writer are not printed to the log writer associated
  103. * with the <code>java.sql.Drivermanager</code> class. When a
  104. * <code>DataSource</code> object is created the log writer is
  105. * initially null; in other words, the default is for logging to be
  106. * disabled.
  107. *
  108. * @param out the new log writer; to disable logging, set to null
  109. * @exception SQLException if a database access error occurs
  110. * @see #getLogWriter
  111. */
  112. void setLogWriter(java.io.PrintWriter out) throws SQLException;
  113. /**
  114. * <p>Sets the maximum time in seconds that this data source will wait
  115. * while attempting to connect to a database. A value of zero
  116. * specifies that the timeout is the default system timeout
  117. * if there is one; otherwise, it specifies that there is no timeout.
  118. * When a <code>DataSource</code> object is created, the login timeout is
  119. * initially zero.
  120. *
  121. * @param seconds the data source login time limit
  122. * @exception SQLException if a database access error occurs.
  123. * @see #getLoginTimeout
  124. */
  125. void setLoginTimeout(int seconds) throws SQLException;
  126. /**
  127. * Gets the maximum time in seconds that this data source can wait
  128. * while attempting to connect to a database. A value of zero
  129. * means that the timeout is the default system timeout
  130. * if there is one; otherwise, it means that there is no timeout.
  131. * When a <code>DataSource</code> object is created, the login timeout is
  132. * initially zero.
  133. *
  134. * @return the data source login time limit
  135. * @exception SQLException if a database access error occurs.
  136. * @see #setLoginTimeout
  137. */
  138. int getLoginTimeout() throws SQLException;
  139. }