FAQ
History
PreviousHomeNext Search
Feedback
Divider

Accessing Databases from Web Applications

Data that is shared between Web components and persistent between invocations of a Web application is usually maintained in a database. Web applications use the JDBC 2.0 API to access relational databases. In the JDBC API, databases are accessed via DataSource objects. A DataSource has a set of properties that identify and describe the real world data source that it represents. These properties include information like the location of the database server, the name of the database, the network protocol to use to communicate with the server, and so on.

Applications access a data source using a connection, and a DataSource object can be thought of as a factory for connections to the particular data source that the DataSource instance represents. In a basic DataSource implementation, a call to the method DataSource.getConnection returns a connection object that is a physical connection to the data source.

If a DataSource object is registered with a JNDI naming service, an application can use the JNDI API to access that DataSource object, which can then be used to connect to the data source it represents.

DataSource objects that implement connection pooling also produce a connection to the particular data source that the DataSource class represents. The connection object that the method DataSource.getConnection returns is a handle to a PooledConnection object rather than being a physical connection. An application uses the connection object just as it usually does and is generally unaware that it is in any way different. Connection pooling has no effect whatever on application code except that a pooled connection, as is true with all connections, should always be explicitly closed. When an application closes a connection that is pooled, the connection is returned to a pool of reusable connections. The next time DataSource.getConnection is called, a handle to one of these pooled connections will be returned if one is available. Because connection pooling avoids creating a new physical connection every time one is requested, it can help to make applications run significantly faster.

The Duke's Bookstore examples use the PointBase database shipped with Sun ONE Application Server 7 to maintain the catalog of books. This section describes how to:

Starting the PointBase Database Server

The Sun ONE Application Server 7 is distributed with a development version of the PointBase database server. To start the server:

  1. Open a terminal window.
  2. Navigate to <S1AS7_HOME>/pointbase/server.
  3. Execute StartServer.

Populating the Example Database

The Sun ONE Application Server 7 is distributed with a sample database that contains the table required for the Duke's Bookstore examples. The table is accessible only to the user name BOOKSTORE. If you need to repopulate the database:

  1. Start the PointBase console tool:
    1. Open a terminal window.
    2. Navigate to <S1AS7_HOME>/pointbase/client_tools.
    3. Execute PB_console.
  2. In the PointBase console window, connect to the database jdbc:pointbase:server://localhost/sun-appserv-samples with user name PBPUBLIC and password PBPUBLIC.
  3. In the IDE, mount the filesystem
    <INSTALL>/j2eetutorial/examples/web/bookstore.
  4. Expand the bookstore node.
  5. In the PointBase console window, execute the SQL statements in createUser.sql:
    1. Copy the SQL statements in createUser.sql.
    2. Paste the SQL statements into the text area labeled Enter SQL Commands by choosing EditRight ArrowPaste in the PointBase console window.
    3. Choose SQLRight ArrowExecute All.
  6. Choose DBARight ArrowDisconnect from Database.
  7. Choose DBARight ArrowConnect to Database.
  8. Type BOOKSTORE for the user name and BOOKSTORE for the password.
  9. In the PointBase console window, execute the SQL statements in bookstore.sql. At the end of the processing, you should see the following output:
  10. [java] ID
    [java] ----------
    [java] 201
    [java] 202
    [java] 203
    [java] 204
    [java] 205
    [java] 206
    [java] 207
    [java]
    [java] 7 Rows Selected.
    [java]
    [java] SQL>
    [java]
    [java] COMMIT;
    [java] OK 
    

Add PointBase JDBC Driver to the Application Server's Classpath

You can add the PointBase JDBC driver to the application server's classpath in one of two ways:

Then, restart the application server to make the server aware of the driver:

  1. Click the General tab of the administration console.
  2. Click Stop to stop the server and Start to restart it.

Defining a Data Source in Sun ONE Application Server 7

Data sources in the Sun ONE Application Server 7 implement connection pooling. Thus, to define the Duke's Bookstore data source, you first need to define a data pool as follows:

  1. In the IDE, select the Runtime tab of the Explorer.
  2. Expand the nodes Server RegistryRight ArrowInstalled ServersRight ArrowSun ONE Application Server 7Right Arrowlocalhost:4848.
  3. Right-click Unregistered JDBC Connection Pools and select Add New JDBC Data Pool.
  4. Type com.pointbase.jdbc.jdbcDataSource for the DataSource Classname.
  5. Type bookstore-pool for the Name.
  6. Click Properties and open the property editor.
  7. Add the properties and values listed in Table 2-1:
    Table 2-1 Bookstore Pool Properties
    Property
    Value
    DatabaseName
    jdbc:pointbase:server://localhost/sun-appserv-samples
    User
    BOOKSTORE
    Password
    BOOKSTORE
  8. Right-click the bookstore-pool data pool and choose Register.
  9. Select the application server that you want the pool to be registered in and click Register.
  10. Click OK.
  11. Expand the Registered JDBC Connection Pools node and notice that bookstore-pool is listed.

Then, create the data source as follows:

  1. In the IDE, select the Runtime tab of the Explorer.
  2. Expand the nodes Server RegistryRight ArrowInstalled ServersRight ArrowSun ONE Application Server 7Right Arrowlocalhost:4848.
  3. Right-click Unregistered JDBC Data Sources and select Add a new JDBC Data Source.
  4. Type jdbc/BookDB for the JNDI Name.
  5. Choose bookstore-pool for the Pool Name.
  6. Right-click the jdbc/BookDB data source and choose Register.
  7. Select the application server that you want the data source to be registered in and click Register.
  8. Click OK.
  9. Expand the Registered JDBC Connection Data Sources node and notice that jdbc/BookDB is listed.

Configuring the Web Application to Reference a Data Source with JNDI

In order to access a database from a Web application, you must declare resource reference in the application's Web application deployment descriptor (see References to Environment Entries, Resource Environment Entries, or Resources). The resource reference declares a JNDI name, the type of the data resource, and the kind of authentication used when the resource is accessed. The JNDI name is used to create a data source object in the database helper class database.BookDB:

public BookDB () throws Exception {
  try  { 
    Context initCtx = new InitialContext();
    Context envCtx = (Context)
      initCtx.lookup("java:comp/env");
    DataSource ds = (DataSource) envCtx.lookup("jdbc/BookDB");
    con =  ds.getConnection();
    System.out.println("Created connection to database.");
  } catch (Exception ex) {
    System.out.println("Couldn't create connection." + 
    ex.getMessage());
    throw new 
      Exception("Couldn't open connection to database: "
    +ex.getMessage());
  }  

To specify a resource reference to the bookstore data source:

  1. Select the Web module.
  2. Expand the WEB-INF node.
  3. Select the web.xml.
  4. Select the References tab in the property sheet.
  5. Click the Resource References Property and open the property editor.
  6. Click Add.
  7. Type jdbc/BookDB in the Name field.
  8. Leave the default type javax.sql.DataSource.
  9. Leave the default authorization Container.
  10. Choose Shareable for Sharing Scope.

Mapping the Web Application JNDI Name to a Data Source

Since the resource reference declared in the Web application deployment descriptor uses a JNDI name to refer to the data source, you must connect the name to a data source defined by the application server as follows:

  1. Select the Web module.
  2. Expand the WEB-INF node.
  3. Select the web.xml file.
  4. Select the Sun ONE AS property sheet.
  5. Select the Resource Reference Mapping property and open the property editor.
  6. For the Resource Reference Name jdbc/BookDB, type jdbc/BookDB in the JNDI Name field.
Divider
FAQ
History
PreviousHomeNext Search
Feedback
Divider

All of the material in The J2EE Tutorial for the Sun ONE Platform is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.