4D S.A. Copyright 2001


This section describes how to connect to a 4D Server database from a 4D Open for Java component. It also discusses how to specify whether a connection should be in synchronous or asynchronous mode. By setting a connection to asynchronous mode, you can allow users to continue working while a query is executing.

The topics in this section include:

Connections

Synchronous and Asynchronous modes

Connections


Using Multiple Connections and Databases

Using the 4D Open for Java classes, a single client can open multiple connections. The client can connect to several databases simultaneously, and several times to the same database. 4D Server considers each connection to a database as a separate process.

With 4D Open for Java, each user can open six concurrent connections (processes) to the same database. Once the sixth process has been opened, an additional user is added to accommodate up to six additional connections, and so on.

Note: The maximum number of concurrent users is set by the 4D Server license that you have purchased.

Connecting to a 4D Server Database

4D Open for Java allows you to connect to a 4D Server database located on a server. You initialize the data used by 4D Open, choose a network protocol to use, and select the database.

Opening a Connection

Once a 4D Server database has been identified over the network (you know the IP address of the distant 4D Server), you can open a connection to it by creating an instance of opDriverManager and pass it as a parameter to an opConnection object. A typical call is written as follow:

   private opConnection doConnect()

      //Create a new connection and return it
      opConnection connection = null;
      try 
         opDriverManager drvrManager = new opDriverManager();
         connection = drvrManager.getConnection (IpAdr);

      catch(opException er)
         //Manage an error

      return connection;

Remember that with 4D Open, you can open several connections on the same database and connect to several databases simultaneously.

For each connection opened, you must create at least one new process in the 4D Server database. To create a new process, use the startProcess method of the opConnection class as follow:

   private opProcess doNewProcess(opConnection connexion)
   
      //Create a new process
      opProcess process = null;
      try
         process = connexion.startProcess("Open_Java","PP","","Task_Open_Java" );
      
      catch(opException er)
         //Manage an error
      
      return process;

Of course, you can create several processes for one connection.

This process is listed in the 4D Server window, which appears on the server:

For each client that has connected to the database, the 4D Server window displays the name of client station, the user, and the user's processes. You supply this information as parameters in the startProcess method.

Processes

The first time you open a connection to a database from a 4D Open for Java client, the client is registered as being connected to the database and a new process is started for the connection. If you connect to the database from the same client again, another process is added for the client. By connecting to a database multiple times, you can have multiple processes running under the same client.

In 4D Server, processes behave like separate connections to the same database. You may want to open several processes for the same client so that you can perform different operations in each one. For example, you might want to retrieve and display a list of records in one process and allow the user to add records in another process.

Using 4D Open for Java, you can get information about the processes currently open for a database on the server:

GetProcessList returns the names of the processes running on the server. The list of processes includes 4D Server's kernel processes (User/Runtime, Design, Client Manager, Cache Manager, and Indexing), which are maintained internally by 4D Server.

CountUserProcesses returns the number of open processes, excluding the kernel processes.

To get more information about each process, you can read the contents of an array of opProcess objects returned by GetProcessList. It provides the name of the user and station, as well as the process name, time spent, and state for each process.

Closing a Connection

You can close a connection by using the CloseConnection method of opConnection.java class.

Providing Access to Non-4D Clients

In 4th Dimension or 4D Client (not from 4D Server), you can allow non-4D Client applications to access a database. To do this, you select the "Allow 4D Open Connections" check box in the second tab control of the Database Properties:

If this check box has been unchecked for a database, you will not be able to connect to the database from 4D Open for Java. If you attempt to connect to the database, an exception will be generated.

Providing 4D Open Access Privileges to a Group

The designer of the 4D Server database must give special access privileges to users connecting to the database from a 4D Open for Java development. These users must be part of the password access group that has been given 4D Open access in 4D Client's Database Properties dialog box.

This group can be selected from the 4D Open access pop-up menu. If no groups have been created, "All Groups" will be chosen by default. If one or more groups have been created, no group is selected by default. The Database Properties dialog box appears as shown below:

If a user is not in the selected group from the 4D Open access pop-up menu, an exception is generated when he or she tries to connect to the database via 4D Open for Java.

Synchronous and Asynchronous Modes


To manage an asynchronous session, you must extend the class Thread.


4D S.A. Copyright 2001