JDBC Connection Pool for GlassFish and Payara Java Application Servers

12

Java application servers as GlassFish and Payara provide a native support of the JDBC connection pooling mechanism to enhance a database access. Such implementation allows reusing database connections cached in the pool.

Configuring JDBC pool connection for your application server can reduce delays and resource consumption compared to servicing each individual request. This strongly enhances the performance of a database, especially for requests made to dynamic database-driven applications.

Follow the simple steps below to configure Java Database Connectivity for GlassFish and Payara with Jelastic PaaS.

Create Environment

1. Log into your Jelastic account and click the New environment button.

1457-1-new-environment-in-jelastic-paas

2. In the topology wizard, switch to the Java tab, pick GlassFish or Payara as your application server and add the required database (as an example, we use GlassFish and MySQL pair). Next, set the resource limits for your containers and type any preferred environment name.

1457-1-glassfish-and-mysql-pair

Click Create, wait for a few minutes to get your new environment and proceed to create JDBC connection pool.

Configure Database

1. Click the Open in browser button for your MySQL node.

1457-1-mysql-node

Use the received email with database credentials to login into the opened phpMyAdmin panel.

2. Once inside, switch to the User accounts tab and click on the Add user account link. Within the opened form, specify all of the required data and tick the Create database with the same name and grant all privileges option.

1457-1-add-phpmyadmin-user-account-in-jelastic-paas

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

Set Up Java Application Server

1. The JDBC MySQL connector is provided by default with the stack (located in the /opt/glassfish/glassfish/domains/domain1/lib directory on your GlassFish server or /opt/payara/glassfish/domains/domain1/lib on Payara), so you don’t need to upload one manually.

1457-1-jdbc-mysql-on-glassfish

2. Log into the GlassFish (or Payara) Admin panel, using credentials from the appropriate email.

1457-1-glassfish-admin-panel

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

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

1457-1-mysql-option

Click the Next button to continue.

4. Find and modify the following Additional Properties:

  • User – provides your database login (pooling in our case);
  • ServerName – specifies your database host without the protocol (e.g., node166355-gf-pooling.jelastic.com);
  • Port – sets port number to 3306;
  • DatabaseName – provides 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).

1457-1-jdbc-additional-properties

After these properties are specified, click Finish.

5. In order to verify accessibility, select your just created connection pool and click the Ping button. If everything is OK, you should see the Ping Succeeded pop-up message.

1457-1-glassfish-ping-button

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

1457-1-new-jdbc-resource

Confirm resources creation with the OK button at the top.

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, substitute the {resources} placeholder with the your JNDI name from the previous section (i.e., jdbc/mypool in our case).

Now, you can deploy your Java application to the created Jelastic PaaS environment and enjoy the benefits of GlassFish and Payara connection pooling!

Get started now with a free trial period at our Jelastic PaaS platform.

SOURCEJelastic, Inc.