Archive

Getting started with IBM Bluemix and MySQL

Share this post:

While creating a web application in IBM Bluemix using J2EE and MySQL, my colleagues and I learned a few things about database connectivity that I’d like to share on this platform to help to others.

Creating the Java application with MySQL in IBM Bluemix

In order to create a Java WebApp, first create an application in the Java Web Start boilerplate from the Bluemix catalogue. Boilerplate is a common code that can be used in any application with little or no modification; it is not part of the business logic, but is interwoven with it in order to keep data structures updated or handle secondary aspects of the application. The procedure to create the boilerplate can be found on this link.

Once the boilerplate is ready, we can move ahead to create the service. There are two ways to add the service to an existing application:

 

  • Bind the existing service to the application. This option helps to share the single service instance across multiple applications and to enable data sharing. This helps in effective service utilization.
  • Add a new service and bind it to the application. Use this option if you want the service instance to be exclusive to your application.

 

 

For more details on handling services in Bluemix, you can check this link.

When you bind or add a MySQL service instance to an application, the application restarts automatically and it will subsequently begin using this new service. By that time, you will have the sample application created on Bluemix dashboard with the MySQL service instance bound to it. When you click on Show Credentials in the MySQL service instance, you will get the instantiating credentials.

The following example below may explain it better:

{
  "mysql-5.5": {
    "name": "mysql-vi",
    "label": "mysql-5.5",
    "plan": "100",
    "credentials": {
      "name": "d44555071f56d45d08d8534c0080712ff",
      "hostname": "198.11.234.73", #<hostname/IP of the server hosting mySQL service>
      "host": "198.11.234.73", #
      "port": 3307, #
      "user": "uM7MDCbhYIKbw", #
      "username": "uM7MDCbhYIKbw", #
      "password": "pNyQdGc25lkLi", #
      "uri": "mysql://uM7MDCbhYIKbw:pNyQdGc25lkLi@198.11.234.73:3307/d44555071f56d45d08d8534c0080712ff"
    }
  }
}

From the values, we are interested only in those that are present under the “credentials” section. We will need to add those to our web application. The “name” specifies the name of the database. It should be noted that you can’t connect to the MySQL service instance directly through the MySQL client. The configuration parameters will be active when the application is deployed in Bluemix environment.

Once you bind the service with the WebApp, you can download the starter kit from the Bluemix dashboard. Note that the starter kit download option is only available when you create the WebApp from the Bluemix dashboard, and not when the app is created with the cf client.

Download the starter kit and import it to your Eclipse integrated development environment (IDE) to continue writing the business logic. Verify that the MySQL service is bound with the application by checking the “services” section of manifest.yml file in the starter kit.

Using the MySQL configuration parameters in the web application

WebApps always have the requirement to create and manipulate the tables. In Bluemix, you have to create the database tables with Java code. We have used the web.xml file present in the WebApp as the store for database initialization parameters. These database parameters are stored as context parameters so they can be read from all servlets or JavaServer Pages (JSP) within the application. The initialization can be done in a web.xml element:

    dbUser
    uM7MDCbhYIKbw


    dbPassword
    pNyQdGc25lkLi


    dbURL
    jdbc:mysql://198.11.234.73:3307/d44555071f56d45d08d8534c0080712ff

Once the parameters are set in web.xml, we can create the database connection in a listener class. You have to declare this listener class in web.xml as below:

  com.resumeManagement.Listeners.DatabaseInitializationListener

In the listener class, we created DBConnection as a context attribute with the help of the following code (the DBConnection is shared across the web application):

ServletContext ctx = servletContextEvent.getServletContext();
//initialize DB Connection
    	String dbURL = ctx.getInitParameter("dbURL");
    	String user = ctx.getInitParameter("dbUser");
    	String pwd = ctx.getInitParameter("dbPassword");
		DBConnectionManager connectionManager = new DBConnectionManager(dbURL, user, pwd);
		ctx.setAttribute("DBConnection", connectionManager.getConnection());

So when you need to create the database connection for building or accessing the tables, use the following code in your servlets or JSPs:

Connection con = (Connection) getServletContext().getAttribute("DBConnection");

You can use the PreparedStatement interface to query the databases. Once the application is ready, you can push it to the Bluemix environment in the form of .war as:

cf push  -p .war

You can troubleshoot the application after successful deployment by taking the following steps in on the Bluemix dashboard:

 

  • Click on your application
  • Select Files and Logs
  • Select the log directory and you will find the stderr.log and stdout.log for getting the error details.

 

 

See this link for more assistance in getting started with MySQL service.

Good luck in your ventures with IBM Bluemix and MySQL! Please leave a comment below to let me know if this was helpful and to join in the conversation.

More stories

Why we added new map tools to Netcool

I had the opportunity to visit a number of telecommunications clients using IBM Netcool over the last year. We frequently discussed the benefits of have a geographically mapped view of topology. Not just because it was nice “eye candy” in the Network Operations Center (NOC), but because it gives an important geographically-based view of network […]

Continue reading

How to streamline continuous delivery through better auditing

IT managers, does this sound familiar? Just when everything is running smoothly, you encounter the release management process in place for upgrading business applications in the production environment. You get an error notification in one of the workflows running the release management process. It can be especially frustrating when the error is coming from the […]

Continue reading

Want to see the latest from WebSphere Liberty? Join our webcast

We just released the latest release of WebSphere Liberty, 16.0.0.4. It includes many new enhancements to its security, database management and overall performance. Interested in what’s new? Join our webcast on January 11, 2017. Why? Read on. I used to take time to reflect on the year behind me as the calendar year closed out, […]

Continue reading