Scalable Database Server, HiRDB Version 8 UAP Development Guide

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

16.4.1 Database connection using DataSource and JNDI

Database connection using DataSource and JNDI can now be used by the JDBC2.0 Optional Package.

Although it is not essential to use JNDI, using it offers a benefit in that you need to specify the connection information only once. Because DataSource class interface definition and JNDI are not included in JDK as standard features, you need to obtain them from the JavaSoft web site when developing application programs.

To connect to a database using DataSource and JNDI:

  1. Create a DataSource object.
  2. Set up connection information.
  3. Register DataSource in JNDI.
  4. Obtain DataSource from JNDI.
  5. Connect to the database.

If you do not use JNDI, the operations in Steps 3 and 4 are unnecessary.

If you use JNDI, execute the operations in Steps 1 through 3 only once. Afterwards, you can connect to the database by performing the operations in Steps 4 and 5 only. Furthermore, after the operation in Step 4, you can modify the connection information as needed.

Organization of this subsection
(1) Creating a DataSource object
(2) Setting up connection information
(3) Registering DataSource in JNDI
(4) Obtaining DataSource from JNDI
(5) Connecting to the database

(1) Creating a DataSource object

Generate the DataSource class objects provided by the JDBC driver.

The DataSource class name of the JDBC driver required to generate the DataSource class objects is JdbhDataSource.

A DataSource class object creation example follows:

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

(2) Setting up connection information

Call up a connection information setup method for the DataSource object and set up connection information. Because a connection information acquisition method can also be used, you can also check the current connection information. For details on the connection information setup/acquisition method, see 16.11 Connection information setup/acquisition interface.

(3) Registering DataSource in JNDI

Register the DataSource object in JNDI.

In JNDI, you can select a service provider from several that are available.

An example of obtaining a DataSource object in JNDI is shown as follows (for Windows). Note that this obtaining example uses File System, which is one of the service providers. For information on other service providers, see the JNDI documentation.

 // Generate a DataSource class object provided by the JDBC driver.
JP.co.Hitachi.soft.HiRDB.JDBC.JdbhDataSource ds;
ds = new JP.co.Hitachi.soft.HiRDB.JDBC.JdbhDataSource();
 
 // Specify connection information.
         ...
 
 // Obtain system properties.
 Properties sys_prop = System.getProperties() ;
 
 // Set up properties for the File System service provider.
 sys_prop.put(Context.INITIAL_CONTEXT_FACTORY,
              "com.sun.jndi.fscontext.RefFSContextFactory");
 
 // Set up the directory to be used by the File System service provider.
 // (In this case, the directory is registered under c:\JNDI_DIR.)
 sys_prop.put(Context.PROVIDER_URL, "file:c:\\" + "JNDI_DIR");
 
 // Update the system properties.
 System.setProperties(sys_prop) ;
 
 // Initialize JNDI.
 Context ctx = new InitialContext();
 
 // Register a DataSource class object provided by the HiRDB driver in JNDI
 // under a logical name called jdbc/TestDataSource.
 ctx.bind("jdbc" + "\\" + "TestDataSource", ds);
                       ...

Note that the JDBC2.0 specification recommends that the logical name to be registered in JNDI be registered under a subcontext called jdbc (jdbc/TestDataSource in the registration example).

(4) Obtaining DataSource from JNDI

Obtain the DataSource object from JNDI.

An example of obtaining a DataSource object from JNDI is shown as follows (for Windows). Note that this obtaining example uses File System, which is one of service providers. For information on other service providers, see the JNDI documentation.

 // Obtain system properties.
 Properties sys_prop = System.getProperties() ;
 
 // Set up properties for the File System service provider.
 sys_prop.put(Context.INITIAL_CONTEXT_FACTORY,
              "com.sun.jndi.fscontext.RefFSContextFactory");
 
 // Set up the directory to be used by the File System service provider.
 // (In this case, the directory is registered under c:\JNDI_DIR.)
 sys_prop.put(Context.PROVIDER_URL, "file:c:\\" + "JNDI_DIR");
 
 // Update the system properties.
 System.setProperties(sys_prop) ;
 
 // Initialize JNDI.
 Context ctx = new InitialContext();
 
 // Obtain an object with a logical name jdbc/TestDataSource from JNDI.
 Object obj = ctx.lookup("jdbc" + "\\" + "TestDataSource") ;
 
 // Cast the extracted object into the DataSource class type.
 DataSource ds = (DataSource)obj;
               ...

(5) Connecting to the database

Invoke the getConnection method for the DataSource object.

An example of calling the getConnection method follows:

 DataSource ds
 
 // Obtain a DataSource object from JNDI.
                      ...
 
 // Issue the getConnection method.
 Connection con = ds.getConnection();
   or
 Connection con = ds.getConnection("USERID", "PASSWORD");*

* The method's arguments (authorization identifier and password) take precedence over the connection information set for the DataSource objects. If the necessary connection information is not set for the DataSource object and the connection information is invalid or connection with the HiRDB server fails, the getConnection method throws an SQLException.

You can set connection information again as necessary after the Datasource object is obtained from JNDI. In such a case, you must cast the Datasource object to the DataSource class type provided by the JDBC driver and then set the connection information.

 DataSource ds
 JP.co.Hitachi.soft.HiRDB.JDBC.JdbhDataSource hirdb_ds;
 
 // Obtain a DataSource object from JNDI.
                      ...
 
 // Cast the DataSource object to the DataSource class type provided by the JDBC driver.
 dbp_ds = (JP.co.Hitachi.soft.HiRDB.JDBC.JdbhDataSource)ds;
 
 // Reset the connection information.
           ...