2.2. More Complex Examples

2.2.1. Other JOnAS Examples

The following examples are located under $JONAS_ROOT/examples/src:

Each directory contains a README that explains how to build and run each example.

2.2.2. An Example With Database Access

The eb example contains two Entity beans that manage Account objects.

The two beans share the same interface (Account); one with bean-managed persistence (BMP, explicit persistence), the other with container-managed persistence (CMP, implicit persistence). The default CMP implementation is CMP 1.1. A CMP 2.0 implementation is also provided and its use is described in the README.

Before running this example, perform the steps in:

2.2.2.1. Configuring Database Access

In order to be able to access your relational database, JOnAS will create and use a DataSource object that must be configured according to the database that will be used.

These DataSource objects are configured via properties files. $JONAS_ROOT/conf contains templates for configuring DataSource objects for databases such as Oracle and PostgreSQL:

Depending on your database, you can customize one of these files with values appropriate for your installation. After doing so, you must update the property jonas.service.dbm.datasources in the jonas.properties file.

For example, for the Oracle1.properties file.

jonas.service.dbm.datasources    Oracle1

Section 3.5.7 Configuring the Database Service provides more details about DataSource objects and their configuration.

2.2.2.2. Creating the Table in the Database

The $JONAS_ROOT/examples/src/eb directory contains an SQL script for Oracle: Account.sql ($JONAS_ROOT/examples/src/eb/Account.sql). If your Oracle server is running and you are using CMP 1.1, you can create the table used by the example. If you are using CMP 2.0, do not create the table.

2.2.2.2.1. Example: Creating the Table in Oracle

sqlplus user/passwd

SQL> @Account.sql

SQL> quit

2.2.2.3. Configuring the Classpath

The JDBC driver classes must be accessible from the classpath. To enable that, update the config_env file (http://jonas.objectweb.org/current/bin/unix/config_env).

In this file, set one of the following variables: IDB_CLASSES, ORACLE_CLASSES, or POSTGRE_CLASSES with the appropriate value for your database installation.

2.2.2.4. Building the eb Example

The simplest way to compile this example is to go to the $JONAS_ROOT/examples/src/eb directory ($JONAS_ROOT/examples/src/eb/) and use the compile.sh shell script ($JONAS_ROOT/examples/src/eb/compile.sh).

If the Ant 1.5 build tool is installed on your machine, you can build the JOnAS examples by using the build.xml files located in the $JONAS_ROOT/examples or $JONAS_ROOT/examples/src directories. To do this, use the build.sh shell script.

2.2.2.5. Running the eb Example

Here, again, two processes are involved:

  • The JOnAS server in which beans will be loaded

  • The Java client that creates instances of beans and calls business methods on it.

To run this example:

  1. Run the JOnAS server to make beans available to clients:

    service jonas start
    
    jonas admin -a eb.jar

    The following messages are displayed on the standard output:

    The JOnAS Server 'jonas' version-number is ready and running on rmi
    
    message-header : AccountExpl available
    
    message-header : AccountImpl available
  2. Run the Java clients in another terminal emulator window:

    jclient eb.ClientAccount AccountImplHome
    
    jclient eb.ClientAccount AccountExplHome

    The example eb has run successfully if the following output displays:

    Getting a UserTransaction object from JNDI
    
    Connecting to the AccountHome
    
    Getting the list of existing accounts in database
    
    101 Antoine de St Exupery 200.0
    102 alexandre dumas fils 400.0
    103 conan doyle 500.0
    104 alfred de musset 100.0
    105 phileas lebegue 350.0
    106 alphonse de lamartine 650.0
    
    Creating a new Account in database
    
    Finding an Account by its number in database
    
    Starting a first transaction, that will be committed
    
    Starting a second transaction, that will be rolled back
    
    Getting the new list of accounts in database
    
    101 Antoine de St Exupery 200.0
    102 alexandre dumas fils 300.0
    103 conan doyle 500.0
    104 alfred de musset 100.0
    105 phileas lebegue 350.0
    106 alphonse de lamartine 650.0
    109 John Smith 100.0
    
    Removing Account previously created in database
    
    ClientAccount terminated
  3. Before ending this session, be sure to stop the JOnAS server:

    service jonas stop