Derby Network Client


Created: 4/11/05
Last update: 10/25/05 -- changed h4 tags to h3 (forrest hid data), and removed manual table of contents


Overview

The Derby network client provides network connectivity to the Derby Network Server. It is distributed as an additional jar file, derbyclient.jar, with an entirely independent code base from the embedded driver.

The Derby network client is a type 4, JDBC compliant Driver, which attempts to match the Derby Embedded JDBC driver as much as possible. Initially, it will have some differences from the embedded driver, for example, some SQL States will remain null and error messages will not be localized in the initial offering. Hopefully, over time the functionality of the two drivers will converge.


Functionality

All functionality for the Derby network client is packaged in the derbyclient.jar file which will be added to the Derby distribution. The client application needs only to include the new jar and application classes in its classpath. derbyclient.jar has no dependencies on the tools, server, or derby database engine classes, nor do those classes depend on derbyclient.jar. Some new client classes are in the same package as the embedded driver, such as org.apache.derby.jdbc.ClientDriver, but the actual classes are contained only in the client jar file.

The Derby network client allows connections via JDBC URL using DriverManager by its implementations of JDBC20 data sources, javax.sql.DataSource, javax.sql.ConnectionPoolDataSource. An implementation of javax.sql.XADataSource is also provided for network XA Support. The Derby network client provides a tracing facility to collect JDBC trace information and view protocol flows for debugging. Security mechanisms are also provided for user and password encryption. DataSource properties and connection attributes can be used to enable tracing, as well as security settings.

Tools support for ij and dblook as well as test harness support is provided.

The sections that follow will describe the functionality and provide examples of using each characteristic mentioned here.


JDBC Driver and DataSource names

The Derby network client classes and JDBC interfaces they implement are listed below.

JDBC Interface   Derby network client class
java.sql.Driver   org.apache.derby.jdbc.ClientDriver
javax.sql.DataSource   org.apache.derby.jdbc.ClientDataSource
javax.sql.XADataSource   org.apache.derby.jdbc.ClientXADataSource
javax.sql.ConnectionPoolDataSource   org.apache.derby.jdbc.ClientConnectionPoolDataSource

Connection URL Format

The protocol for the client URL is jdbc:derby://. The embedded and client attributes are specified the same way. The embedded driver has been changed to reject URL's with a // following derby:

Derby network client URL Format

jdbc:derby://server[:port]/databaseName[;attributeKey=value]..

An example of using this URL to connect to the Derby network server running on the localhost at port 1527 in ij is:

ij> connect `jdbc:derby://localhost:1527/sample;create=true;user=user1;password=secret4me';


Ways to Connect using the Derby Client Driver

Client CLASSPATH

Prior to using the Derby network client, the derbyclient.jar file needs to be added to the classpath on the client. On Windows, to add the derbyclient.jar file to your classpath, use the following command:

set CLASSPATH=%CLASSPATH%;%DERBY_INSTALL%\lib\derbyclient.jar;

When using the Derby client driver to connect to the Derby database, there are two general methodologies used to connect. The first method is to use the DriverManager. The second way to connect is to use one of the three DataSource classes listed above.

To use the DriverManager, the first step is to load the ClientDriver.

Loading the driver:

port java.sql.* ; ... try { Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch(java.lang.InstantiationException instant) { instant.printStackTrace(); } catch(java.lang.IllegalAccessException illegal) { illegal.printStackTrace(); } ...

Now a connection can be made with the DriverManager.

Connecting with the DriverManager:

String url = "jdbc:derby://myhost.mydomain.com:1527/mydb;create=true;user=user1;password=secret4me";
connection = DriverManager.getConnection(url);

Connecting with the ClientDataSource:


import java.sql.*; import javax.sql.*; import org.apache.derby.jdbc.ClientDataSource; ... ClientDataSource ds=new ClientDataSource(); ds.setDatabaseName("sample"); ds.setServerName("localhost"); // host with listening network server. ds.setPortNumber(1527); // port of listening network server. ds.setUser("user1"); // Assign the user ID ds.setPassword("secret4me"); // Assign the password Connection con= ds.getConnection(); // Create a Connection object

Data Source properties

The following data source properties are available on the ClientDataSource, ClientConnectionPoolDataSource and the ClientXADataSource classes.

Standard JDBC Data Source Properties
Property Type Description Default Req'd Url Attr
databaseName* String name of database   X  
dataSourceName* String a data source name      
description* String a description of this data source      
user* String user's account name APP   user
password* String user's database password     password
serverName String host name or TCP/IP address where the server is listening for requests localhost    
portNumber int port number where the server is listening for requests 1527    
Client Specific Data Source Properties
traceFile String filename for tracing output. Setting this property turns on tracing. (See Tracing)     traceFile
traceDirectory String directory for tracing output. Each connection will output to a separate file. Setting this property turns on tracing. (See Tracing)     traceDirectory
traceLevel int level of client tracing if traceFile or traceDirectory are set TRACE_ALL   traceLevel
traceFileAppend boolean true if tracing output should append to the existing trace file. false   traceFileAppend
securityMechanism int security mechanism (See Security) USER_ONLY_SECURITY   securityMechanism
retrieveMessageText boolean retrieve message text from server. A stored procedure is called to retrieve the message text with each SQLException and may start a new unit of work. Set to false if you do not want the performance impact or encounter issues with a new unit of work being started. true   retrieveMessageText
Server Specific Data Source Properties
These properties are translated into server specific URL attributes which are passed to the Derby embedded driver by appending them to the databaseName. For example, calling setDatabaseName("mydb") followed by setConnectionAttributes("create=true") will cause the string mydb;create=true to be sent to the server as the database name . The URL to connect to the database will therefore include these attributes.
connectionAttributes* String set to the list of Derby embedded connection attributes separated by semi-colons. E.g., to specify an encryption bootPassword of "x8hhk2adf", and set upgrade to true, do the following: ds.setConnectionAttributes("bootPassword=x8hhk2adf;upgrade=true");     various. See Derby Documentation for a full list.

* This property is also available on the EmbeddedDataSource. All other data source properties are supplemental to the ClientDataSources.

Note: setAttributesAsPassword , which is available for the embedded data sources is not available for the client data source. It is not clear how to communicate it to the server.


Tracing

The Derby network client provides a tracing facility to collect JDBC trace information and view protocol flows. Listed below are various ways to obtain trace output. However, the easiest way to turn on tracing is to use the traceFile attribute on the url in ij. The example below shows all tracing going to the file trace.out from an ij session.

ij>connect 'jdbc:derby://localhost:1527/mydb;create=true;traceFile=trace.out;user=user1;password=secret4me';

ClientDataSource Tracing

Three methods can be used to collect tracing data if obtaining connections from the ClientDataSource.

  1. Use the setLogWriter(java.io.PrintWriter) method of ClientDataSource and set the PrintWriter to a non null value.
  2. Use the setTraceFile(String filename) method of ClientDataSource.
  3. Use the setTraceDirectory(String dirname) method of ClientDataSource to trace each connection flow in its own file for programs that have multiple connections.

DriverManager Tracing

If the DriverManager is used to obtain connections, two ways are available to enable and collect tracing information.

ClientDataSource Tracing Examples

Shown below are examples of each tracing type for the ClientDataSource Tracing.

  1. Use the setLogWriter(java.io.PrintWriter) method of ClientDataSource and set the PrintWriter to a non null value.
    
    /**
    *  Example to show how to set /enable tracing  when using DataSource
    *  - using the setLogWriter method on the datasource
    */
    private static void sampleConnectUsingSimpleDataSource()
     throws Exception
    {
    java.sql.Connection conn = null;
     try
     {
    javax.sql.DataSource ds =
    new org.apache.derby.jdbc.ClientDataSource();
    ((org.apache.derby.jdbc.ClientDataSource) ds).setServerName("localhost");
    ((org.apache.derby.jdbc.ClientDataSource) ds).setPortNumber(1527);
    ((org.apache.derby.jdbc.ClientDataSource) ds).setDatabaseName("sampleDB;create=true");
    System.out.println("---------------------------------------------------------------");
    System.out.println("[TraceWithSetLogWriter]  Setting setLogWriter to trace.log.");
    System.out.println("Traces will be written into trace.log");
    java.io.PrintWriter pw = new PrintWriter(new FileOutputStream("trace.log"),true);
    ds.setLogWriter(pw);
     
     
    // Refine the level of tracing detail
    ((org.apache.derby.jdbc.ClientDataSource) ds).
    setTraceLevel(org.apache.derby.jdbc.ClientDataSource.TRACE_CONNECTS |
    org.apache.derby.jdbc.ClientDataSource.TRACE_PROTOCOL_FLOWS);
     
    // This connection request is traced using trace level
    // TRACE_CONNECTS | TRACE_PROTOCOL_FLOWS
    conn = ds.getConnection("user1", "secretPassword");
      java.sql.Statement s = conn.createStatement();
    try
    {
     s.executeUpdate("DROP TABLE sampleTable");
      }
      catch(Exception e)
      {
      // ignore drop of table error
    }
     
    s.executeUpdate("CREATE TABLE sampleTable( sampleColumn int)");
    PreparedStatement ps = conn.prepareStatement("insert into sampleTable values(?)");
     
      // do some inserts
    conn.setAutoCommit(false);
    ps.setInt(1,1);
    ps.setInt(1,2);
    ps.executeUpdate();
    conn.commit();
     
    // cleanup resources
    s.close();
    ps.close();
    conn.close();
    pw.flush();
    pw.close();
    }
    catch(java.sql.SQLException e)
    {
    e.printStackTrace();
    }
    }
    
    
  2. Use the setTraceFile(String filename) method of ClientDataSource.
    
    /**
    *  Example to show how to set /enable tracing when obtaining connections from DataSource
    *  - using the setTraceFile method on the datasource
    */
    private static void sampleConnectUsingSimpleDataSource()
     throws Exception
    {
    java.sql.Connection conn = null;
     
     try
     {
    javax.sql.DataSource ds =
    new org.apache.derby.jdbc.ClientDataSource();
    ((org.apache.derby.jdbc.ClientDataSource) ds).setServerName("localhost");
    ((org.apache.derby.jdbc.ClientDataSource) ds).setPortNumber(1527);
    ((org.apache.derby.jdbc.ClientDataSource) ds).setDatabaseName("sampleDB;create=true");
    System.out.println("---------------------------------------------------------------");
    System.out.println("[TraceWithSetTraceFile]  Setting setTraceFile to trace.log.");
    System.out.println("Traces will be written into trace.log");
    ((org.apache.derby.jdbc.ClientDataSource) ds).setTraceFile("trace.log");
     
     
    // Refine the level of tracing detail
    ((org.apache.derby.jdbc.ClientDataSource) ds).
    setTraceLevel(org.apache.derby.jdbc.ClientDataSource.TRACE_CONNECTS |
    org.apache.derby.jdbc.ClientDataSource.TRACE_PROTOCOL_FLOWS);
     
    // This connection request is traced using trace level
    // TRACE_CONNECTS | TRACE_PROTOCOL_FLOWS
    conn = ds.getConnection("user1", "secretPassword");
       java.sql.Statement s1 = conn.createStatement();
    try
    {
     s1.executeUpdate("DROP TABLE sampleTable");
      }
      catch(Exception e)
      {
      // ignore drop of table error
    }
     
    s1.executeUpdate("CREATE TABLE sampleTable( sampleColumn int)");
    PreparedStatement ps = conn.prepareStatement("insert into sampleTable values (?)");
     
    conn.setAutoCommit(false);
    ps.setInt(1,1);
    ps.executeUpdate();
    conn.commit();
     
    // cleanup resources
    s1.close();
    ps.close();
    conn.close();
    }
    catch(java.sql.SQLException e) {
    e.printStackTrace();
    }
    }
    
    
    
  3. Use the setTraceDirectory(String dirname) method of ClientDataSource to trace each connection flow in its own file for programs that have multiple connections.

Call the setTraceDirectory(String directoryName) method on the ClientDataSource or use the URL attribute traceDirectory=<directory> to specify a directory for trace output. The directory must already exist. If the traceDirectory property is enabled, then tracing of multiple connections on the same datasource will be directed to separate files under the specified directory.

When tracing to a directory, each connection will be traced to a separate file named traceFile_origination_n, where origination indicates the origination of the log writer in use and can be "xads", "cpds", "ds", or "driver", and n represents the nth connection against the datasource. The log writer origination indicator in the file name maps to the log writer as follows: