Connecting Through Data Sources
A SQL Server 2000 JDBC data source is a DataSource object that provides the connection information needed to connect to an underlying database. The main advantage of using a data source is that it works with the Java Naming Directory Interface (JNDI) naming service, and it is created and managed outside of the applications that use it. Because the connection information is outside of applications, the time it takes to reconfigure your infrastructure when a change is made is minimal. For example, if the underlying database is moved to another server and uses another port number, the administrator must change only the relevant properties of the SQL Server 2000 JDBC data source (a DataSource object). The applications using the underlying database do not need to change because they only refer to the logical name of the SQL Server 2000 JDBC data source.
How SQL Server 2000 JDBC Data Sources Are Implemented
Microsoft ships a data source class for the SQL Server 2000 JDBC driver. See "SQL Server 2000 JDBC" for the name of the class.
The SQL Server 2000 JDBC data source class provided implements the following interfaces defined in the JDBC 2.0 Optional Package:
- javax.sql.DataSource
- javax.sql.ConnectionPoolDataSource, which enables you to implement connection pooling
NOTE: You must include the javax.sql.* and javax.naming.* classes to create and use SQL Server 2000 JDBC data sources. The SQL Server 2000 JDBC driver provides all the necessary JAR files that contain the required classes and interfaces.
Calling a Data Source in an Application
Applications can call a SQL Server 2000 JDBC data source using a logical name to retrieve the javax.sql.DataSource object. This object loads the SQL Server 2000 JDBC driver and can be used to establish a connection to the underlying database.
Once a SQL Server 2000 JDBC data source has been registered with JNDI, it can be used by your JDBC application as shown in the following example:
Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("jdbc/EmployeeDB"); Connection con = ds.getConnection("scott", "tiger");In this example, the JNDI environment is first initialized. Next, the initial naming context is used to find the logical name of the SQL Server 2000 JDBC data source (EmployeeDB). The Context.lookup() method returns a reference to a Java object, which is narrowed to a javax.sql.DataSource object. Finally, the DataSource.getConnection() method is called to establish a connection with the underlying database.
Using Connection Pooling
Connection pooling allows you to reuse connections rather than create a new one every time the SQL Server 2000 JDBC driver needs to establish a connection to the underlying database. Connection pooling manages connection sharing across different user requests to maintain performance and reduce the number of new connections that must be created. For example, compare the following transaction sequences.
Example A: Without Connection Pooling
- The client application sends a data access query.
- The client application obtains the result set of the query.
- The client application displays the result set to the end user.
- The client application ends the connection.
Example B: With Connection Pooling
- If an unused connection exists, it is returned by the pool implementation; otherwise, it creates a new connection.
- The client application sends a data access query.
- The client application obtains the result set of the query.
- The client application displays the result set to the end user.
- The client application returns the connection to the pool.
NOTE: The client application still calls "close()", but the connection remains open and the pool is notified of the close request.
The pool implementation creates "real" database connections using the getPooledConnection() method of ConnectionPoolDataSource. Then, the pool implementation registers itself as a listener to the PooledConnection. When a client application requests a connection, the pool implementation (Pool Manager) assigns one of its available connections. If there is no connection available, the Pool Manager establishes a new connection and assigns it to that application. When the client application closes the connection, the Pool Manager is notified by the driver through the ConnectionEventListener interface that the connection is free and available for reuse. The pool implementation is also notified by the ConnectionEventListener interface when the client somehow corrupts the database connection, so that the pool implementation can remove that connection from the pool.
Once a SQL Server 2000 JDBC data source has been registered with JNDI, it can be used by your JDBC application as shown in the following example, typically through a third-party connection pool tool:
Context ctx = new InitialContext(); ConnectionPoolDataSource ds = (ConnectionPoolDataSource)ctx.lookup("jdbc/EmployeeDB"); pooledConnection pcon = ds.getPooledConnection("scott", "tiger");In this example, the JNDI environment is first initialized. Next, the initial naming context is used to find the logical name of the JDBC data source (EmployeeDB). The Context.lookup() method returns a reference to a Java object, which is narrowed to a javax.sql.ConnectionPoolDataSource object. Finally, the ConnectionPoolDataSource.getPooledConnection() method is called to establish a connection with the underlying database.
![]() |
Previous Page |
![]() |
Next Page |
![]() |
Synchronize with Contents |