How to use GlassFish's Connection Pool in Jelastic

| March 14, 2012

GlassFish provides its Connection Pooling mechanism for resources such as EIS Server, JMS Server and Database Server. How can it help you?

Connection Pooling is intended to share server resources among requesting clients. Creating a new connection for each request can take a lot of time, but pooling connections fixes this drawback by eliminating the multiple, individual requests. When a user makes a request, a connection from the pool is directed and when he or she is finished with the connection, it is returned to the pool, making it available for any consequent requests. Connection Pooling decreases the response time for the application to get, use and close connections, as the actual connections to the data store are managed by the pool.

Let's check out how to setup GlassFish's Connection Pool in Jelastic!

1. Create an environment

  1. Go to and sign up if you haven't done so yet or log in with your Jelastic credentials by clicking the Sign In link on the page.
  2. Within the Jelastic dashboard, click the Create environment button:
glassfish connection pool
  1. In the Environment Topology dialog, pick GlassFish as your application server and the database you want to use (for example MySQL). Increase your limit to 12 cloudlets. Then type your environment name--for example, testCP.
glassfish pool

It will take just a minute for your environment to be created.

glassfish's connection pool

2. Configure database

  1. Click the Open in browser button for MySQL.
pool connection for glassfish
  1. When you created the environment, Jelastic sent you an email with credentials to the database. Use these credentials to create a user account and the database with the application.
glassfish database connection
glassfish db connection

3. Connection Pool configuration

  1. Upload the MySQL connector to the lib folder and restart GlassFish.
jdbc connection pool
  1. Open the GlassFish admin page in a web browser.
  1. Sign in with the login and password which Jelastic sent to your email.
  2. Create a new JDBC Connection Pool. At the first step, type pool name, choose resource type (javax.sql.DataSource), database driver vendor (MySQL) and click on the Next button.
connection pool
  1. Then set the connection to your database: type the port number, your database name, database URL, user name and password. Then click Finish.
glassfish db config
establish glassfish pool

As you can see the Connection Pool is successfully created.

  1. Create a new JDBC Resource: type JNDI name, choose pool name and select target (gfcluster).
jdbc pool

4. Getting a connection from your Java code

Put the following code into your Java class:

InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/testcp");
Connection conn = ds.getConnection();
Now you can deploy your application to Jelastic Cloud and enjoy!