Knowledge Base
MilesWeb / Cloud Servers

Configure JDBC Connection for GlassFish and Payara Java Application Servers

Approx. read time : 3 min

GlassFish and Payara, Java application servers provide a local support of the JDBC connection pooling method for easy access of database. This type of implementation lets the users to reuse database connections cached in the pool.

When you configure a JDBC pool connection for your application server, it can reduce time and resource utilization in comparison to each individual request servicing. This strongly helps in improving the performance of a database, especially for requests made to dynamic database-driven applications.

Below are the steps for easily configuring Java Database Connectivity for GlassFish and Payara with MilesWeb PaaS powered by Jelastic.

Related: Out-of-Box GlassFish & Payara Clustering: Running Java EE Highly-Available Applications in the Cloud

Create Environment

1. Log into your MilesWeb cloud dashboard account and click the New Environment button.

2. Then go to the Java tab, select your application server from GlassFish or Payara and add the required database (for example, here it’s GlassFish and MySQL pair). Next, decide the resource limits for your containers and give a name to your environment.

Click on Create and wait for a few minutes to get your new environment and continue with the next step of creating JDBC connection pool.

Configure Database

1. For the MySQL node, click the Open in browser button.

You will receive an email which contains the database credentials to login into the opened phpMyAdmin panel.

2. Once logged in, go to the User accounts tab and click on the Add user account link. A form will open where you need to specify all of the required data and tick the Create database with the same name and grant all privileges option.

Click Go at the bottom of the page to start the database and user addition for connection pooling.

Set Up Java Application Server

1. The JDBC MySQL connector is by default offered with the stack (located in the /opt/glassfish/glassfish/domains/domain1/libdirectory on your GlassFish server or /opt/payara/glassfish/domains/domain1/lib on Payara), so there is no need to upload one manually.

2. Use the credentials from your email to login to the GlassFish (or Payara) Admin panel.

3. Go to the Resources > JDBC > JDBC Connection Pools section and on the tools panel, click the New button. Fill in the following fields within the appeared form:

  • Pool Name – give any chosen name
  • Resource Type – from  the drop-down list select the javax.sql.DataSource item
  • Database Driver Vendor – choose the MySQL option

To continue, click on the Next button.

4. Find and modify the following Additional Properties:

  • User – provides your database login (pooling in our case)
  • ServerName – defines your database host without the protocol (e.g. node166355-gf-pooling.jelastic.com)
  • Port – sets port number to 3306
  • DatabaseName – indicates your database name (pooling in our case)
  • Password – stores a password for the specified user
  • URL and Url – sets a JDBC connection string in the jdbc:mysql://{db_host}:3306/ format; herewith, the {db_host} placeholder can be substituted with either node hostname (node166355-gf-pooling.jelastic.com) or IP address (192.168.2.57)

Click Finish, once you are done with the above properties.

5. For verifying your accessibility, select the connection you have just created and click the Ping button. If all goes well, you will see the Ping Succeeded pop-up message.

6. Go to the Resources > JDBC > JDBC Resources section and create JDBC resources for pooling by clicking on the New button. Within the opened dialog box, provide any desired JNDI Name and choose your Pool Name from the drop-down list.

Click on OK button at the top to confirm the resources creation.

Connect from Java Code

Put the following strings into the Java class of your application code:

InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup(“{resources}“);
Connection conn = ds.getConnection();

Here, replace the {resources} placeholder with the your JNDI name from the previous section (i.e. jdbc/mypool in our case).

Get started now at our MilesWeb PaaS to deploy your Java application and enjoy the benefits of GlassFish and Payara connection pooling!

Avatar
Pallavi is a Digital Marketing Executive at MilesWeb and has an experience of over 4 years in content development. She is interested in writing engaging content on business, technology, web hosting and other topics related to information technology.
Need help? We’re always here for you.