Cejv659 week09 glassfish-s14
description
Transcript of Cejv659 week09 glassfish-s14
Java IV: Web Services Development CEJV659
Configuring Glassfish for MySQL
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
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
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
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
04/10/2023 6
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
JDBC Connection Pool
Select JDBC Connection Pool on the tree and then select New on the table that appears.
04/10/2023 8
JDBC Connection Pool
Fill in the form as shown and click on Next.
04/10/2023 9
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
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
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
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
JDBC Resource
From the Common Tasks select JDBC Resources
Click on New
04/10/2023 14
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
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