6.9 & Glassfish V3
There are some problems with the
6.9 /Glassfish V3 combination used in
the laboratories. Operations like “create DataSource” can be performed in different
in some they work, in others they don’t.
The following sequence of actions should be followed to get your
set up correctly. Really, the Appserver parts (Glassfish V3) is only needed for the
EJB assignment but it would be best to set up everything when you first start.
If you h
in earlier subjects such as CSCI213, CSCI222, or
CSCI399 then you should remove all existing directories associated with
Look for hidden folders .
.personaldomain etc. Remove the
projects from the
to save space.
6.9 from the Applications/Programming menu:
First, set up a database connection so that you can access your tables on the Oracle
ystem that runs on the “wraith” computer:
You will need to add the ojdbc14.jar driver
it’s in /usr/local/instantclient_10_2.
Then create a connection that uses this driver
connecting to database csci at port
1521 on the server “wraith”; supply you
r database user name and password.
Check that it is working and that you can view your tables in the Oracle database.
Close the connection.
Checkout the basics in NetBeans.
Create a standard Java application
NetBeans only loads elements when fi
so wait while Java application
side of NetBeans gets set up.
You simply need a “Hello World” app for this test:
(User “Mark” is one of the fake accounts with student level privileges that I use
Code and build, then run
Close that test project, and create an “Enterprise Project”
Enterprise projects have to run on application servers
you haven’t got one yet so
you are prompted to choose one
; pick Glassfish Server 3
It’s located in /usr/local. Once you have
selected that you will be prompted to
define a “domain”
essentially its your private configuration for running a copy
of Glassfish. Specify a folder, called domain1, in your home directory.
Your domain gets created
you can look at the creation re
port later once the
project set up is completed:
With a server chosen, you can continue to specify project details
will want both web and application clients (only the application client is actually
created in this test sequence).
The next task is to define connection pools and JDBC data sources that will be
hosted in the server. (It’s here that there are inconsistencies in the way NetBeans
handles things. It should be possible to define these connections later when
but when done that way it seems that
NetBeans 6.9 misses out on copying the database drivers to a directory where
Glassfish can find them. So define the connection pool and JDBC resource now
to avoid problems later.)
Pick the new
application, and add a “Glassfish” file
one for connection pools:
The pool is to be created using the information that you entered when you tested
your database connection back in step 3.
Need to invent a connection pool name
and later a JNDI name
e.g. oraclePool, jdbc/ora
You have only defined some data for connections
no code yet. But “deploy” the
application to get these new database components registered and the driver files
copied to appropriate places:
Look at your servers in
the services tab.
Unfortunately, NetBeans 6.9 is listing the server that belongs to “root” (this is the
one that would be used in a production environment) as well as your personal
configuration. Then, NetBeans realizes that you cannot access the control
that server and insists on telling you this
again, and again, and again.
It is possible to right click the listing and get the menu with a remove option
keep entering enter to dismiss the warning dialogs.
You should then be able
to see details of your server with the new JDBC
resources (connection pool and JDBC resource that you added).
View the “Admin Console” of your server
Check that the database connections really are set up:
You need to be able to successf
ully “Ping” your database:
That is all that has to be done now.
The rest of the work can be deferred until there is an assignment or exercise using
either the “web” container (servlets) or the EJB container (“session beans).
But it would be sensi
ble to complete setting things up now rather than later.
Put a password on your administrator.
Add a new user (e.g. “admin”) to the admin
realm with an assigned password.
on the next time you use the admin console
, log in as admin and remov
Also, it is probably useful to create a few users
tom, dick, harry, and sue
couple of groups for any later exercises involving controlled role
access to resources.
Resuming the creation of a little “Enterp
First, need to create code for handling object
Here using the JPA
probably the TopLink implementation, though NetBeans 6.9
Will need to grab the table definitions from Oracle, and generate the en
This example assumes that you have created the Soccerleague table that is used in
other CSCI399/CSCI398 exercises and is used more extensively in the
CSCI398 EJB exercise:
drop table soccerleague;
drop sequence leagueseq;
nce leagueseq increment by 1 start with 1;
create table soccerleague (
gameid number primary key,
insert into soccerleague values (
'Members Equity Stadium',
'Perth', 'Newcastle', 0, 0);
The application built here will simply retrieve all rows from the table.
The entity classes are best created in their own separate project
a “Java Class
Library” project. I named the project “entities”.
This needs to reference the generic “Persistence” library (but not an actual
implementation like Toplink)
Let the system write the code for dealing with the entities:
ustments are often necessary.
If you are using Oracle sequences to provide “surrogate keys” then you need to
add these details (they aren’t captured directly as not clearly represented in Oracle
Sometimes the mappings are odd
it chose to us
e BigInteger values for my soccer
scores (it would be an unusual game that had a score that required a BigInteger to
encode it). I changed these back to “int”.
Compile the “entities” project
this results in a .jar file with the class files.
e entities.jar file to the “ejb” subproject of the Enterprise Application:
(Note the “relative path” / “absolute path” controls. If you intend to partly build
an application on your own computer and then try completing it in the lab, you
had better ch
oose “relative path”.)
Define a package in the ejb sub
project for the beans.
Define a session bean
this will contain all the business code and will be called
from both web (servlet) and rich client application clients.
It should have both “local”
(web) and “remote” (application client) interfaces:
Use “Insert Code” to add a “business method”. It will be added to the local and
remote interfaces (if you use a class like BigDecimal as a result or an argument,
you must either specify it completely
BigDecimal and then edit the “bean” and “interface” files to “Fix imports”).
The ejb subproject needs to have the Toplink library as well as the entities.jar file
listed in its libraries.
It also needs a persistence
The data source is the jdbc/ora resource created earlier. The persistence provider
should be Toplink. You don’t want it to drop your existing tables when it
You need to tell it that it works with the entity classes from the entiti
The EJB container will “inject” an entity manager into your session bean
just have to tell it which one (reference to the name in the persistence unit):
You can now “build” the EJB subproject.
Next, work on the client application
Add code (“Call Enterprise Bean” that will allow connection to the session bean:
Obviously, the client needs the entities.jar library (this contains the definition of
the soccerleague entities that will be returned.
It’s not obvious wh
y it needs the library with the persistence implementation. But
it does. It’s because if a collection data member is “lazy loaded” and hasn’t been
loaded before an entity is returned, you get back an entity where the Collection
data member is not null bu
t is instead an instance of a place holder class from the
persistence implementation. So you need the implementation library to get that
The Enterprise application will, by default, be set up to run the web client. We
haven’t built one
. So change the Properties/Run to specify use of the client
It should run:
Remove the test application from your server and invoke server shutdown.
You won’t need the AppServer until you get to a “web” or “ejb” assignment or