In this example, two beans share the same interface (Account), one uses bean-managed persistence (explicit persistence), the other uses container-managed persistence (implicit persistence). This is a good example of the two persistence techniques.
The Entity Bean example application is located in the $JONAS_ROOT/examples/src/eb directory. It is composed of the following files:
-rw-r--r-- 19456 Apr 25 2003 AccountExplBean.java -rw-r--r-- 2048 Apr 25 2003 AccountHome.java -rw-r--r-- 1024 Apr 25 2003 Account.idb -rw-r--r-- 10752 Apr 25 2003 AccountImpl2Bean.java -rw-r--r-- 11264 Apr 25 2003 AccountImplBean.java -rw-r--r-- 2048 Apr 25 2003 Account.java -rw-r--r-- 1024 Apr 25 2003 Account.pgsql -rw-r--r-- 1024 Apr 25 2003 Account.sql -rw-r--r-- 6144 Apr 25 2003 ClientAccount.java drwxr-xr-x 2048 Apr 25 2003 davidclt -rw-r--r-- 9728 Apr 25 2003 db1.prp -rw-r--r-- 4096 Apr 25 2003 eb.xml -rw-r--r-- 2048 Apr 25 2003 jonas-eb.xml -rw-r--r-- 2560 Apr 25 2003 README |
Check that all files are present before proceeding to the next section.
Here is a description of the files that reside in the $JONAS_ROOT/examples/src/eb directory:
Account.java contains the Remote interface for the bean Account.
AccountExplBean.java contains the entity bean with "bean-managed persistence".
AccountHome.java contains the Home interface for the bean Account.
AccountImpl2Bean.java contains an entity bean with "container-managed persistence version 2".
AccountImplBean.java contains an entity bean with "container-managed persistence".
ClientAccount.java contains the client account code.
jonas-eb.xml is the JOnAS-specific part of the deployment descriptor.
eb.xml is the standard part of the deployment descriptor.
The simplest way to compile this and all examples is to go to the $JONAS_ROOT/examples/src directory and enter the following command line as user jonas:
ant -find build.xml install |
This command compiles all examples in the $JONAS_ROOT/examples/src directory.
In order to run this example, you must be able to access a relational database. Typically you would use PostgreSQL, as it is preconfigured with Red Hat Application Server. JOnAS will create and use a DataSource object that must be configured for the database you intend to use. These DataSource objects are configured via properties files and are referenced in the jonas.properties file (see Section 4.1.2 JOnAS Configuration Files).
The jar file for your database needs to be in a location where your JRE can find it. (For example, if you are using PostgreSQL—Red Hat Edition and the IBM JRE, the following is necessary:
$ su # cd /usr/lib/jvm/java-java_version-ibm-IBM_java_version/jre/lib/ext # ln -s /usr/share/java/rh-postgresql3.jar . |
This example requires that a datasource named jdbc_1 is available in the JOnAS server. This is the default name used in the database properties files provided in the $JONAS_ROOT/conf directory. Red Hat Application Server provides configuration files for a variety of common RDBMSs. PostgreSQL is the preconfigured option. $JONAS_ROOT/conf/PostgreSQL1.properties is set up to work with the Red Hat Application Server default installation.
JOnAS does not automatically create the database for you—you have to create it prior to running this example. The Account.sql file can be used with most SQL-92 compatible databases. There is also an Account.idb file that can be used with InstantDB.
For example, for PostgreSQL:
$ su # su -s /bin/sh -c "psql -p 5433 db_jonas" jonas sampleaccount=# \i Account.pgsql sampleaccount=# \q |
Note | |
---|---|
The su command changes your pwd. Thus, after you run su, you must go back to the $JONAS_ROOT/examples/src/eb examples directory so that the Account.pgsql file can be found through the psql interface. |
Additional information about database configuration is available in the Configuring Database Service chapter of the Red Hat Application Server User Guide.
To do a complete and clean run of this example, you will have to first start the JOnAS server and then run the two Java clients. At the end of the execution, you may stop the JOnAS server.
Note | |
---|---|
The following example assumes that the current directory is $JONAS_ROOT/examples/src/eb. |
Here is how to proceed:
As root, restart the JOnAS server:
/sbin/service jonas start |
Load the eb.jar file. (See Section 4.1.4 Loading Beans Using jonas.properties to Section 4.1.7 Unloading Beans for further information.)
jonas admin -a eb.jar |
Start the two Java Clients:
jclient eb.ClientAccount AccountImplHome jclient eb.ClientAccount AccountExplHome |
A successful run should output:
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 100.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 |
As root, stop the JOnAS server with the following command:
/sbin/service jonas stop |
Note | |
---|---|
The jar can also be deployed using the web interface. The steps are similar to those described in Section 2.2.2 Quick Start to the SB Example, but using the file eb.jar. |