Enterprise web programming

Modules ENTWA (Level 6) and APSW (Level 7)

Configuring a JDBC datasource in Glassfish

There are a number of ways in Glassfish of setting up the connection between your Java webapp and a database.

Using glassfish-resources.xml

This is the default and usual way.

In NetBeans, the "New persistence unit" wizard will create this for you, using a datasource name (e.g. "jdbc/mydb") and a database connection. From these it will create entries in glassfish-resources.xml for both a connection pool (pointing at the database you are using) and a datasource (pointing at the connection pool.

Sometimes, Glassfish appears to ignore glassfish-resources.xml and thus does not create the necessary objects. This is typified by an exception entry in the Glassfish server log that says it can't find a resource (e.g. "jdbc/mydb__pm"). It then becomes necessary for you to do something manual to create the objects.

Importing glassfish-resources.xml

This method requires use of the DOS command line (or equivalent on Macs).

  1. Start up a DOS shell (or equivalent)
  2. Change directory to the Glassfish bin directory. On my machine this is C:\Program Files\glassfish-4.1\bin.
  3. Give the command asadmin add-resources [path to glassfish-resources.xml]
  4. This should tell you which objects it has created. It may ask for a Glassfish admin user name and password. These are usually "admin" and "admin" (if setup by NetBeans; if you installed Glassfish yourself, it will be whatever you chose then).

Creating the objects manually

The objects can be created manually from the Glassfish admin console. This is a web application run from within Glassfish.

  1. Start the Glassfish admin console. From NetBeans, select the Services tab and go the to Servers element. Right-click the appropriate Glassfish instance and select "View Domain Admin Console". If this is greyed out, it may mean you have to start the server first.
  2. This should open up the console in your web browser. It may ask for a Glassfish admin user name and password. These are usually "admin" and "admin" (if setup by NetBeans; if you installed Glassfish yourself, it will be whatever you chose then).
  3. You need to create (in this order):
    1. a new JDBC Connection Pool
    2. a new JDBC Datasource resource
  4. There are buttons to do these on the console's "Common tasks" page. Alternatively, in the tree on the left side, you will see an item labelled "JDBC" which expands into separate "JDBC resources" and "JDBC connection pools" subtrees. From either of these there is a "new" button.
  5. New connection pool:
    1. Its pool name can be anything meaningful but "mydb_pool" would be a typical example.
    2. For resource type, choose "javax.sql.ConnectionPoolDataSource". "javax.sql.DataSource" will also work, but not provide you with connection pooling.
    3. For database driver vendor, choose the one for your database management system. For JavaDB, either "Derby" or "JavaDB" seems to work.
    4. Click next.
    5. In step 2, leave all the details at the top unchanged, but go down to "Additional properties". You need to ensure that all the property values in your glassfish-resources.xml file are set appropriately here. This will probably require you to set or change at least the following:
      1. User
      2. Password
      3. DatabaseName
      4. SecurityMechanism - which should be blank or deleted
    6. Click finish. You should see your new pool appear in the list of pools.
  6. New JDBC resource:
    1. Its JNDI name should be whatever you have used in your application's persistence.xml file (e.g. "jdbc/mydb").
    2. For its pool name, you should be able to choose the pool you created above from the menu.
    3. Click OK. You should see your new resource appear in the list of resources.

Your application should now run and find these JDBC objects. If it doesn't work, check that you have followed these steps correctly.

 

Last updated by Prof Jim Briggs of the School of Computing at the University of Portsmouth

 
The enterprise web programming modules include some material that was formerly part of the WEB1P and WEB2P units.