Java Reference
In-Depth Information
Chapter 12:
Using JDBC DataSources with Servlets
and Java Server Pages
In This Chapter
Servlets and Java Server Pages (JSP) extend the power of Java technology to server-side applications.
They are Java technology's answer to CGI programming, for they enable the developer to build dynamic
Web pages combining user input with information from corporate data sources.
Server-side Java offers significant improvements in efficiency over the traditional Perl CGI, where a new
process is started for each HTTP request, since the overhead of starting the process can dominate the
execution time of the CGI program. With servlets and JSP applications, each request is handled by a
lightweight Java thread, in a Java Virtual Machine that stays up all the time, rather than as a
heavyweight operating system process.
Another major advantage of Java servlets and JSP pages is, of course, that they allow you to use a
single development language across an entire application. You can write applications for an Apache
server running on a Solaris platform but can do all your development and checkout under Linux or any
other OS that supports Java.
This chapter provides a brief introduction to using servlets and JSP to create dynamic Web pages.
These Web pages are driven by a membership database, accessed using the
DataSource
object.
Using JDBC DataSources
Database
Connections
obtained using the
DataSource
interface, introduced in the JDBC 2.0
Standard Extension API, offer the user considerably more capability than the basic
Connection
objects that the
DriverManager
provides;
DataSource
objects can support connection pooling and
distributed transactions. These features make
DataSource
objects the preferred means of getting a
Connection
to any source of data. This source can be anything from a relational database to a
spreadsheet or a file in tabular format.
There are three types of standard
DataSource
objects, each of which offers unique advantages:
The basic
DataSource
that produces standard
Connection
objects just like those the
DriverManager
produces
A
PooledDataSource
that supports connection pooling. Pooled connections are returned to a
pool for reuse by another transaction.
A
DistributedDataSource
that supports distributed transactions accessing two or more
DBMS servers.
With connection pooling, connections can be used over and over again, avoiding the overhead of
creating a new connection for every database access. Reusing connections in this way can improve
performance dramatically, since the overhead involved in creating new connections is substantial.
Distributed transactions involve tables on more than one database server. When a
DataSource
is
implemented to support distributed transactions, it is almost always implemented to produce
connections that are pooled as well.
A
DataSource
object is normally registered with a JNDI naming service. JNDI naming services are
analogous to a file directory that allows you to find and work with files by name. This means that an
application can retrieve a
DataSource
object by name from the naming service in a manner
independent of the system configuration.
Preparatory to discussing the use of JDBC
DataSource
objects in a Web application, the
next section
gives a brief introduction to Java servlets.