Nonstop Database, HiRDB Version 9 UAP Development Guide

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

19.3.7 Description of connection to and disconnection from a HiRDB server

SQLJ has no CONNECT or DISCONNECT statement. Therefore, connection to or disconnection from a HiRDB server is coded as Java instructions.

Organization of this subsection
(1) Connection to a HiRDB server
(2) Disconnecting from a HiRDB server
(3) Default connection

(1) Connection to a HiRDB server

To connect to a HiRDB server, use the following coding using a connection context.

(a) Defining a connection context class

Define a class for the connection context. Class-name indicates a Java identifier. The defined class inherits sqlj.runtime.ConnectionContext.

 #sql modifier context class-name ;
(b) Declaring connection context

Using the declared class, declare the connection context (as a Java variable declaration). Connection-context indicates a Java identifier.

 modifier class-name connection-context ;
(c) Connecting to a HiRDB server

Create a connection context object using a new operator. During this step, connection is made to the HiRDB server. For the connection parameters, describe the HiRDB server at the connection destination, port number, authorization identifier, and password in the same format as that used for JDBC.

 connection-context = new class-name(connection-parameter) ;
(d) Connecting to the HiRDB server when the native interface is used

When the native interface is used, there are three ways of connecting to the HiRDB server:

These connection methods are described below.

1. Describing the connection as a Java instruction
Use the new operator to generate a connection context object. However, since JDBC is not being used, specify an authorization identifier, a password, a server name, and a port number in the connection parameters.
 
 connection-context = new class-name(connection-parameters);
 
If no connection parameters are specified, the HiRDB server checks the client environment variables.
 
 connection-context = new class-name();
 
An example of creating a connection context follows:
 
 #sql context Ctx;
 String Userid=new String("user1");
 String Passwd=new String("puser1");
 String Host=new String("HiRDB_SV");
 short port=22000;
 
 Ctx con = new Ctx(:Userid,:Passwd,:Host,:port);
 

2. Using the CONNECT statement
Specify an authorization identifier and a password in the connection parameters.
The HiRDB server checks the client environment definitions for the port number and the server name.
 
 #sql [connection-context]{CONNECT USER :embedded variable USING :embedded variable};
 or
 #sql [connection-context]{CONNECT :embedded variable IDENTIFIED BY :embedded variable};
 
If connection parameters are not specified, the HiRDB server checks the client environment definitions.
 
 #sql [connection-context]{CONNECT};
 
An example of the CONNECT statement follows:
 
 #sql context Ctx;
 String Userid=new String("user1");
 String Passwd=new String("puser1");
 Ctx con;
 
 #sql [con] {CONNECT USER :Userid USING :Passwd };
 

3. Using the JDBC connection object (Connection)
Use the new operator to generate a connection context object. In the connection parameters, specify the JDBC connection object (java.sql.Connection).
 
 connection-context = new class-name(connection-object);
 
An example of creating a connection context follows:
 
 #sql context Ctx;
 java sql.Connection con =
 
 java.sql.DriverManager.getConnection("jdbc:hitachi:PrdbDrive://DBID=22200,
 DBHOST=HiRDB_SV","user1","user1");
 
 Ctx ctx = new Ctx(con);
 

(2) Disconnecting from a HiRDB server

To disconnect from the HiRDB server, invoke the close method for the connection context. Note that there is no reconnection method. To reconnect, create a new object.

 connection-context.close() ;

An example of invoking the close method for the connection context follows:

 #sql context DeptContext;
            ...
 {
     DeptContext deptCtx = new DeptContext(deptURL,true);
     #sql [deptCtx] { DELETE FROM TAB };
     deptCtx.close();
 }

When using the native interface edition, you can use the DISCONNECT statement instead of invoking the close method for the connection context.

 
 #sql[connection-context]{DISCONNECT};
 

An example of the DISCONNECT statement follows:

 
 #sql context Ctx;
 Ctx con;
 #sql[con]{CONNECT};
 
 #sql[con]{DISCONNECT};
 

(3) Default connection

(a) Standard interface edition

For the standard interface edition, the default connection context is assumed if no connection context is specified in an SQL statement.

To use the default connection context, a UAP must create a connection context in advance, and set it as the default connection context. Once the default connection context is set, it remains valid until the close() method for the default connection context is issued or a new connection context is set as the default connection context.

The default connection context is held by a variable inside the default connection context class (JP.co.Hitachi.soft.HiRDB.sqj.runtime.PrdbContext).

The default connection context has multiple constructors that have the different arguments described as follows.

To specify the URL of the connection destination, authorization identifier, and password, use the same format as is used for the JDBC driver of HiRDB.

In SQLJ, to use a constructor that includes a connection URL during the creation of a connection context, you must specify autoCommit, and specify TRUE to enable it and FALSE to disable it.

If the default connection context is created from the JDBC connection context, the autoCommit setting in the JDBC connection context is inherited.

(b) Native interface edition

For the native interface edition, the default connection context class is held in a variable of JP.co.Hitachi.soft.HiRDB.pdjpp.runtime.PrdbContext.

The default connection context class has the following constructors:

An example of releasing and resetting the default connection context follows:

 
 import JP.co.Hitachi.soft.HiRDB.pdjpp.runtime.PrdbContext;
                          :
 PrdbContext pctx = new PrdbContext(user,passwd,host,port);
 PrdbContext.setDefaultContext(pctx);
                          :
 pctx.close();
 PrdbContext new_pctx = new PrdbContext(user,passwd,host,port);
 
 PrdbContext.setDefaultContext(new_pctx);
 

A coding example in which the default context is implicitly specified follows:

 
void print_address(String name) throws SQLException;
{
  String telno;
  JP.co.Hitachi.soft.HiRDB.pdjpp.runtime.ConnectionContext ctx;
  ctx = JP.co.Hitachi.soft.HiRDB.sqj.runtime.PrdbContext.getDefaultContext();
  #sql [ctx] { SELECT TELNO INTO :telno FROM PERSON WHERE :name = NAME } ;
}