JDBC Connection Pool for GlassFish and Payara Java Application Servers

By | June 21, 2018

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.create jdbc connection pool2. 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.glassfish connection pool

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.mysql connection pool

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.database connection pooling

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.jdbc connection pooling in java

2. Login to the GlassFish (or Payara) Admin panel, using credentials from the appropriate email.glassfish caching jdbc

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

jdbc connection pooling

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)

jdbc pooling

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.configure jdbc connection pool

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.jdbc pool

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 at one of the available Jelastic service providers.

Subscribe to get the latest updates