Scalable Database Server, HiRDB Version 8 UAP Development Guide

[Contents][Index][Back][Next]

17.3 Database connection using a DataSource object and JNDI

The JDBC2.0 Optional Package can now use database connections that use a DataSource object and JNDI.

Although use of JNDI is not required, the advantage of using it is that you only have to set up the connection information once. The standard JDK package does not include interface definitions for the DataSource class or JNDI, so you have to download these items from the JavaSoft Web site when you develop an AP.

To connect a database by using a DataSource object and JNDI:

  1. Generate the DataSource object.
  2. Set up the connection information.
  3. Register the DataSource object into JNDI.
  4. Get the DataSource object from JNDI.
  5. Connect to the database.

If you are not using JNDI, steps 3 and 4 are not necessary.

If you are using JNDI, steps 1 to 3 need to be executed only once. Thereafter, you can connect to the database by performing only steps 4 and 5. Once you have performed step 4, you can change the connection information as necessary.

Organization of this section
(1) Generating the DataSource object
(2) Setting up connection information
(3) Registering the DataSource object into JNDI
(4) Getting the DataSource object from JNDI
(5) Connecting to the database

(1) Generating the DataSource object

Generate the DataSource class object to be provided by the JDBC driver.

The DataSource class name of the JDBC driver, which is necessary for generating the DataSource class object, is PrdbDataSource.

Below is an example of generating the DataSource class object:

 
 JP.co.Hitachi.soft.HiRDB.JDBC.PrdbDataSource ds = null ;
 ds = new JP.co.Hitachi.soft.HiRDB.JDBC.PrdbDataSource() ;
 

(2) Setting up connection information

Call the method for setting up connection information for the DataSource object, and set up the connection information. Because there is also a method for acquiring connection information, you can use it to check the current connection information. For details about the connection information setup and acquisition methods, see 17.7 Connection information setup and acquisition interface.

(3) Registering the DataSource object into JNDI

Register the DataSource object into JNDI.

JNDI can select from among several service providers, depending on the execution environment.

Shown below is an example of registering the DataSource object into JNDI (this example is for Windows). In the registration example, the File System service provider, which is one of the service providers, is used. For details about other service providers, see the JNDI documentation.

 
 // Generate DataSource class object to be provided by JDBC driver.
 JP.co.Hitachi.soft.HiRDB.JDBC.PrdbDataSource ds;
 ds = new JP.co.Hitachi.soft.HiRDB.JDBC.PrdbDataSource();
 
 // Set connection information.
         :
 
 // Get system properties.
 Properties sys_prop = System.getProperties() ;
 
 // Set properties of File System service provider.
 sys_prop.put(Context.INITIAL_CONTEXT_FACTORY,
              "com.sun.jndi.fscontext.RefFSContextFactory");
 
 // Set directory to be used by File System service provider.
 // (Register under c:\JNDI_DIR.)
 sys_prop.put(Context.PROVIDER_URL, "file:c:\\" + "JNDI_DIR");
 
 // Update system properties.
 System.setProperties(sys_prop) ;
 
 // Initialize JNDI.
 Context ctx = new InitialContext();
 
 // Register DataSource class object to be provided by HiRDB driver
 // into JNDI. Use logical name jdbc/TestDataSource.
 ctx.bind("jdbc" + "\\" + "TestDataSource", ds);
                       :
 

When you register the logical name to be registered into JNDI, the JDBC2.0 specifications recommend that you register the logical name under a subcontext called jdbc (jdbc/TestDataSource in the registration example).

(4) Getting the DataSource object from JNDI

Get the DataSource object from JNDI.

Shown below is a registration example for the DataSource object (this is an example for Windows). This registration example uses the File System service provider, which is one of the service providers. For details about other service providers, see the JNDI documentation.

 
 // Get system properties.
 Properties sys_prop = System.getProperties() ;
 
 // Set properties of File System service provider.
 sys_prop.put(Context.INITIAL_CONTEXT_FACTORY,
              "com.sun.jndi.fscontext.RefFSContextFactory");
 
 // Set directory to be used by File System service provider.
 // (Register under c:\JNDI_DIR.)
 sys_prop.put(Context.PROVIDER_URL, "file:c:\\" + "JNDI_DIR");
 
 // Update system properties.
 System.setProperties(sys_prop) ;
 
 // Initialize JNDI.
 Context ctx = new InitialContext();
 
 // Get object of local name jdbc/TestDataSource from JNDI.
 Object obj = ctx.lookup("jdbc" + "\\" + "TestDataSource") ;
 
 // Cast retrieved object to DataSource class type.
 DataSource ds = (DataSource)obj;
               :
 

(5) Connecting to the database

Call the getConnection method for the DataSource object.

Shown below is an example of calling the getConnection method.

 
 DataSource ds
 
 // Get DataSource object from JNDI.
                      :
 
 // Issue getConnection method.
 Connection con = ds.getConnection();
   or
 Connection con = ds.getConnection("USERID", "PASSWORD");#
 

#
The method's arguments (authorization identifier and password) take priority over the connection information that was set for the DataSource object. If needed connection information has not been set for the DataSource object, or if the contents of the connection information are invalid, or if connection with the HiRDB server fails, the getConnection method throws an SQLException.

After getting the DataSource object from JNDI, set up the connection information again, as necessary. In this case, you must cast the DataSource object to the DataSource class type provided by the JDBC driver before you set up the information. An example is shown below:

 
 DataSource ds
 JP.co.Hitachi.soft.HiRDB.JDBC.PrdbDataSource hirdb_ds;
 
 // Get DataSource object from JNDI.
                      :
 
 // Cast DataSource object to DataSource class type provided
 // by JDBC driver.
 dbp_ds = (JP.co.Hitachi.soft.HiRDB.JDBC.PrdbDataSource)ds;
 
 // Set up connection information again.
           :