Cejv659 week09 glassfish-s14

16
Java IV: Web Services Development CEJV659 Configuring Glassfish for MySQL

description

From my course on Web services at Concordia University's Continuing Education Computer Institute. This presentation shows how to configure GlassFish 4.0 with MySQL 5.6 for use in NetBeans 8.0.

Transcript of Cejv659 week09 glassfish-s14

Page 1: Cejv659 week09 glassfish-s14

Java IV: Web Services Development CEJV659

Configuring Glassfish for MySQL

Page 2: Cejv659 week09 glassfish-s14

REST, JPA, NetBeans and GlassFish

NetBeans provides wizards for creating RESTful CRUD applications

They can be created from either existing JPA entities or directly from a database

The wizards create stateless session beans for the web service

While TomEE supports session beans the wizard generates code that is not compatible with TomEE

Therefore we need to use the GlassFish server

04/10/2023 2

Page 3: Cejv659 week09 glassfish-s14

REST, JPA, NetBeans and Glassfish

A second problem is the database support in GlassFish When installed this server is configured with the Derby

sample database The purpose of these slides is show how to configure

GlassFish for MySQL and then use the wizards The database we will use is the single table Aquarium The goal is to create web services that sample programs

used in Java II and Java III can use

04/10/2023 3

Page 4: Cejv659 week09 glassfish-s14

MySQL

Before you can configure GlassFish you must have MySQL running and the database created

Next, you must copy the mysql driver to GlassFish Download the latest driver from MySQL For Windows it is now an install (msi) file When run the driver will be found in

C:\Program Files (x86)\MySQL\MySQL Connector J\ Copy mysql-connector-java-5.1.30-bin.jar to

glassfish-install-path\domains\domain-name\lib If GlassFish is running then restart the server04/10/2023 4

Page 5: Cejv659 week09 glassfish-s14

Configuring Glassfish

Step 1 On the Services tab open Servers and right mouse click

on GlassFish Server 4 Select Start Once GlassFish is running right mouse click on

GlassFish Server 4 again and click on View Domain Admin Console

04/10/2023 5

Page 6: Cejv659 week09 glassfish-s14

04/10/2023 6

Page 7: Cejv659 week09 glassfish-s14

Resource and Connection Pool

To work with a database you need to configure the resource and the pool

Resources provide applications a means to connect to a database

Connection Pools provide reusable connections to the database

The application uses a resource to access the database and the resource uses the pool to get a connection

We need to create the pool before the resource

04/10/2023 7

Page 8: Cejv659 week09 glassfish-s14

JDBC Connection Pool

Select JDBC Connection Pool on the tree and then select New on the table that appears.

04/10/2023 8

Page 9: Cejv659 week09 glassfish-s14

JDBC Connection Pool

Fill in the form as shown and click on Next.

04/10/2023 9

Page 10: Cejv659 week09 glassfish-s14

JDBC Connection Pool

The next form has been filled out based on the choice of MySQL as the database

Nothing has to be changed except that you should check Ping: Enabled

10

Page 11: Cejv659 week09 glassfish-s14

JDBC Connection Pool

If you scroll down on the form you will come to Additional Properties

After adding the 4 values click on Finish

04/10/2023 11

Page 12: Cejv659 week09 glassfish-s14

JDBC Connection Pool

This what you should see

If the Ping was not successful just delete the resource or even the pool and start over.

12

Page 13: Cejv659 week09 glassfish-s14

JDBC Resource

A connection pool object will now manage the connections to the database

Our application needs to request a connection from this object that is not part of our application

The Java Naming and Directory Interface (JNDI) allows us to refer to an object that belongs to another process, such as GlassFish, by a name

A JDBC Resource allows you to assign a JNDI name for the connection pool you wish to use

When your code uses a JNDI name the object is located and made available

04/10/2023 13

Page 14: Cejv659 week09 glassfish-s14

JDBC Resource

From the Common Tasks select JDBC Resources

Click on New

04/10/2023 14

Page 15: Cejv659 week09 glassfish-s14

JDBC Resource

Now you need to give the resource its JNDI name It usually begins with a prefix that identifies the type of resource

followed by the name you have chosen such as jdbc/aquarium Select the pool to associate with the resource from the drop down

Pool Name.

15

Page 16: Cejv659 week09 glassfish-s14

JDBC Resource

Here is what you should see if you have been successful

You are now ready to use the NetBeans wizards

04/10/2023 16