View comments | RSS feed

Using databases

The most common way to generate dynamic websites is to use a database. Your web application interprets input from a user request and responds with a dynamic page using data stored in your database. This section describes the Java Database Connectivity (JDBC) mechanism and how to access databases in your web applications using JRun data sources.

JRun includes the PointBase embedded database in the installation. JRun uses this database for several sample applications, but you can also use it for your web applications. For information on using this database, see JRun Administrator's Guide or the PointBase help files at jrun_root/pointbase/docs/server_embedded/GettingStarted/serverindex.html.

Understanding JDBC

Database access in JRun is performed through the Java database connectivity API (JDBC). JDBC uses a driver manager (provided by Sun) and a JDBC driver (Sun provides the JDBC-ODBC bridge; third-party vendors provide other JDBC drivers).

Types of JDBC drivers

There are several types of JDBC drivers, as the following table describes:
Driver
Description
Native API driver
This type of JDBC driver is also called a Type 2 driver. It wraps Java code around native database libraries. JRun must have access to database client API libraries to use a Type 2 driver.
Net protocol driver
This type of JDBC driver is also called a Type 3 driver. It communicates with the database using a generic network protocol and a middleware component.
Native protocol driver
This type of JDBC driver is also called a Type 4 driver. It communicates with the database using database-specific native protocols.

JRun ships with Type 4 JDBC drivers that you can use to access a variety of relational databases. For more information, see the DataDirect Connect JDBC User's Guide and Reference, distributed with the JRun documentation.

Connecting to databases

There are many ways to connect to a database in your web application. The two most common are to use JRun data sources and create a manual connection. The following table describes these two methods:
Connection method

Description
JRun data source
If you set up a JRun data source using the JMC, your application must only perform a JNDI lookup with the data source name to get a connection to the database. This process is described in "Using JRun data sources".
Uses the JRun data source connection method. Transparently uses the built-in JRun connection pooling mechanism.
JRun includes Merant database drivers that you can use. The JMC provides driver-specific defaults for the driver class names, URLs, ports, and other settings of many popular JDBC drivers. You can also modify these settings and use other JDBC drivers, as necessary.
For more information, see the JMC online Help. For instructions on setting up a JDBC data source in the JMC, see Getting Started with JRun.
Manual database connection
To manually create a JDBC connection in your web application, you must provide the following information in your code:
  • Database driver���� Use the Class.forName method to instantiate the database driver.
  • Database connection object���� Use the DriverManager.getConnection method to establish the Connection object.
  • Database URL���� The database URL contains the protocol (jdbc), the driver subprotocol (examples include odbc and sequelink), and driver-dependent data that identifies the database. For the format and contents of the database URL, your database-driver-specific documentation.
To pool database connections that you create manually, you must implement a custom connection pooling mechanism.
For complete information on the driver class name and database URL, see the documentation for your JDBC driver.

Advantages of JRun data sources

JRun data sources provide servlet portability in addition to built-in connection pooling. Macromedia recommends this method of database access in JRun. The following sections describe the advantages of using JRun data sources.

Ease of setup

JRun includes the JDBC Data Sources panel in the JMC that makes creating, editing, and removing data sources easy. For more information, see the JMC online Help.

Portability

To connect to a database, you must provide the following information:

When you use a JRun data source, your code references the database using only the data source name rather than using hard-coded JDBC driver information in your web application. You set up the data source only once. Then, any number of servlets, EJBs, and JSPs can then access this data source by name.

With data sources, you can update the database definition in the JMC. The information about the database and database server is stored outside of your code and you can change it at any time without recompiling your application.

Another advantage of using data sources is that sensitive database connection information, such as user names and passwords, are hidden from all but the JRun administrator. The web application developers do not need this information to get the connection to the database.

Connection pooling

Connections to databases are expensive. JRun data sources use a built-in pooling mechanism. The advantage of using a JRun-defined data source is that JRun creates a pool of database connections. When you connect to the database, the pooling mechanism gives you an established connection from the pool. The response time is much quicker because the connection does not have to be created.

When you close the connection in your application, JRun does not actually close the connection but returns it to the pool.

You can turn off the connection pooling and implement your own custom connection pooling. For more information on configuring your data sources, see the JMC online Help.

Sample data sources

JRun includes three default data sources that are predefined for the samples JRun server. These data sources use the com.pointbase.jdbc.jdbcUniversalDriver database driver. The following table describes these data sources:
Data source name
Description
compass
Used by the Compass sample application. This application is installed by default on the samples JRun server in jrun_root/servers/samples/compass-ear/.
samples
Used by the Techniques sample application. This application is installed by default on the samples JRun server in jrun_root/servers/samples/techniques-ear/.
smarticket
Used by the SmarTicket sample application. This application is installed by default on the samples JRun server in jrun_root/servers/samples/smarticket.ear.

Using JRun data sources

The JRun data sources let you access data sources via JNDI lookup to the InitialContext object. To use data sources in your servlets, you must import the following packages that use the JDBC API:

In addition, you must import one of the following Java packages, depending on how you use your database connection:

The following code example uses the JRun data source service to access JDBC data source information:

...
import javax.naming.*;
import javax.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
...
Connection dbConnection = null;
ResultSet dbResultSet = null;
ResultSetMetaData rsmd = null;
try {
��InitialContext ctx = new InitialContext();
��DataSource ds = (DataSource) ctx.lookup("compass");
��dbConnection = ds.getConnection();
��Statement stmt = dbConnection.createStatement();
��dbResultSet = stmt.executeQuery("SELECT * FROM user");
��rsmd = dbResultSet.getMetaData();
} catch (Exception e) {
}
...
//process your result set and result set metadata here
...

To see a sample servlet, start the samples JRun server and open a browser to http://localhost:8200/techniques.

Improving database access performance

To improve database performance in your web application, use the following techniques:

For more information and code examples, see "Optimizing JDBC".

Comments


No screen name said on Sep 20, 2005 at 4:10 AM :
Why NOT closed InitialContext ?

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/jrun/4/Programmers_Guide/techniques_servlet16.htm