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
- Go to Jelastic.com 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.
- Within the Jelastic dashboard, click the Create environment button:
- 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.
It will take just a minute for your environment to be created.
2. Configure database
- Click the Open in browser button for MySQL.
- 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.
3. Connection Pool configuration
- Upload the MySQL connector to the lib folder and restart GlassFish.
- Open the GlassFish admin page in a web browser.
- Sign in with the login and password which Jelastic sent to your email.
- 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.
- Then set the connection to your database: type the port number, your database name, database URL, user name and password. Then click Finish.
- Create a new JDBC Resource: type JNDI name, choose pool name and select target (gfcluster).
As you can see the Connection Pool is successfully created.
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!