Creating a basic datasource in Glassfish 4

Today we are going to see how to create a datasource in our Glassfish server in a few steps. In this case, it is going to be for a MySQL database:

First of all, if we have not done yet, we should copy the MySQL driver in our folder lib inside the domain folder where we are going to deploy our application. After that, let’s start to configure our datasource.

As default, the admin console for Glassfish server is in the port 4848. You should be able to see this admin console connecting to your server with this port. In my case, I have the server installed in my own machine, then, my URL to connect to the admin console looks like this:


If the connections is performed correctly, the browser should prompt you asking for the admin credentials, and after introduce them correctly, you should see the admin console with a menu in the left side.

In this menu, we are going to open the category:

Resources -> JDBC

Under this category, you should find two more categories:

  • JDBC Resources
  • JDBC Connection Pools

Let’s open the JDBC Connection Pools category and click in New… to create a new connection pool for our datasource.

Here, we can see that the process to create a new connection pool has two steps. We should introduce the following data:

In the first step:

  • Name: In my case, ExamplePool.
  • Resource type: We should select javax.sql.DataSource from the dropdown list.
  • Database Driver Vendor: We should select MySql for this example.

In the second step you can accept all the default values and go directly to the Additional Properties table. Here, the best option, specially if we want a full control of everything, we can delete all the properties in there, and define new properties. In this case, we are only go to add the basic ones to connect to our database, but there are a lot of different properties that can be added. Then, let’s add our properties:

  • User: The user to connect to de DB. In my case, dbuser
  • Password: The user’s password to connect. In my case, resudb. In this case, MySQL allows us to have users without passwords, but Glassfish is going to return an error if the user does not have a password. Then, if your user does not have a password, you should set one. *
  • Url: The URL to connect to our schema. In this case, I am using the schema example. The URL to connect, in my case, looks like: jdbc:mysql://localhost:3306/example

In most cases, setting these properties is enough, but I have detected that there are IDEs where is not enough. In this case, as a precaution, we can set an extra property to avoid future problems:

  • driverClass: In this case, the MySQL driver class com.mysql.jdbc.Driver

Now, it is time to save our pool. After that, if you click in the left menu in the pool’s name, you will see the configuration of the pool in the main page and a new button labeled as Ping. Let’s click on it to confirm that the configuration is correctly done. If everything goes well, a message Ping Succeeded should be shown.

Now that our pool is ready, we should create our datasource. For this, we should click in the left menu in the JDBC Resources category and click in the New… button. In here, we only need to add the JNDI name, in general, something like jdbc/example and choose in the dropdown list the pool name that we have previously created (ExamplePool). After that, we can save it and, we are ready to go.

That’s all. Now, we only need to use our new datasource in our applications.

See you.

* To set a password for a MySQL user we should execute:

UPDATE mysql.user
   SET user.Password = PASSWORD('newPassword')
 WHERE user.User = 'username';
Creating a basic datasource in Glassfish 4