2

I am trying to deploy my Jersey project on openshift. I have implemented this apple class to test the error in the another class since I guess the problem is with the establishing the database connection. in the Tails log I found this error:

Connecting to database… com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "OPENSHIFT_MYSQL_DB_PORT"'. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

package org.busTracker.serverSide;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("myresource")
public class Apple {

   //I modified my credients.
    String host = "jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:OPENSHIFT_MYSQL_DB_PORT/serverside";
    String user = "adminBjv5a4k";
    String password = "7tvPb1Bx3v8j";

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {

        Connection conn = null;  
        try {    
          Class.forName("com.mysql.jdbc.Driver");    
          System.out.println("Connecting to database…");    
          conn = DriverManager.getConnection(host,user,password);    
        } catch (Exception e) {    
          e.printStackTrace();    
        } finally {    
          if (conn != null) {    
            try {    
              conn.close();    
            } catch (SQLException e) {    
              // ignore    
            }    
          }    
        }   

        return "Hello, from apple class   14.05.15 11:35!";
    }

}

Edit: I added the following to the try block after DriverManager.getConnection():

  Map<String, String> env = System.getenv();
  for (String envName : env.keySet()) {
      System.out.format("%s=%s%n",
                        envName,
                        env.get(envName));
  }

I have tried the following but I am still getting the same error:

MrPencil
  • 934
  • 8
  • 17
  • 36
  • 1
    it is interpreting `OPENSHIFT_MYSQL_DB_PORT` as a String not as is's value – Fran Montero May 14 '15 at 09:52
  • Review [env vars](https://developers.openshift.com/en/databases-mysql.html). Maybe all yout problem is that your forgot the '$' so access the `OPENSHIFT_MYSQL_DB_PORT` system var. – Fran Montero May 14 '15 at 09:58
  • @Fran: I have added it but it did not solve the problem and I am getting the same `Caused by: java.lang.NumberFormatException: For input string: "$OPENSHIFT_MYSQL_DB_PORT"` – MrPencil May 14 '15 at 10:01
  • check if system variable OPENSHIFT_MYSQL_DB_PORT is set – Fran Montero May 14 '15 at 10:09
  • try this http://stackoverflow.com/a/20610185/2139691 – Ricardo May 14 '15 at 10:28
  • @FranMontero: How can I check whether the vairiable is being set? I tried Ricardo solution but nothing is being output. Please see my edit question. – MrPencil May 14 '15 at 10:51
  • @Ricardo: I tried with your link to check whether the env variable is being set but nothing is being output. Please check my question code. Have I set it at the right postion? – MrPencil May 14 '15 at 11:00
  • @MrPencil don't you know the port where mysql is lostening? – Fran Montero May 14 '15 at 11:54
  • @FranMontero: Is not it the port in the phpmyadmin `3306`? – MrPencil May 14 '15 at 12:05
  • 1
    @MrPencil try with the final value: `"jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:3306/serverside"` – Fran Montero May 14 '15 at 12:08
  • I have tried this solution before, when I set the value `3306` for port then I am getting `Caused by: java.net.UnknownHostException: $OPENSHIFT_MYSQL_DB_HOST` then I set the server ip in the phpmyadmin for `$OPENSHIFT_MYSQL_DB_HOST` then I am getting `Caused by: java.net.NoRouteToHostException: No route to host`. – MrPencil May 14 '15 at 12:17
  • I have pinged the ip address of the server and I am getting packets back. – MrPencil May 14 '15 at 12:20

3 Answers3

4

the problem is because of this line

String host = "jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:OPENSHIFT_MYSQL_DB_PORT/serverside";

to get the environment variable, you need to use the method System.getEnv().get("[the variable name]"). So, in your case, the host variable should looks like this

String host = "jdbc:mysql://" 
              + System.getenv().get("OPENSHIFT_MYSQL_DB_HOST") 
              + ":" 
              + System.getenv().get("OPENSHIFT_MYSQL_DB_PORT") 
              + "/serverside";

by the way, your edit does not work because the application already throws an exception before it execute the code. so, to make it work, you need to put it before the DriverManager.getConnection() function.

kucing_terbang
  • 4,991
  • 2
  • 22
  • 28
  • Aha it works now thank you :) I am using the tail files of openshift in eclipse to track what happens on openshift server but I have to request openshift from the browser to see the output in tail files if I request it from the `POSTMAN` Chrome or `HttpRquester` firefox I can not say any output in the tail files just the rendering in HTML since my another class has a POST method so I cant request it from the browser and I need a tool to do it but nothing happening in the tail files! when I user POSTMAN or HttpRequester? can you give me any advice how can I deal with this problem? – MrPencil May 14 '15 at 15:03
  • I just have to fix this problem `The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path` which I saw when I changed to the Jboss perspective and I am getting output now anyway thanks again since this problem costed my 3 days hard work over 30 hours. – MrPencil May 14 '15 at 16:32
  • I'm not quite understand what are you trying to ask. And, I'm not very familiar with those two tools. But, usually I use jquery to create and test my rest function and the log I use `rhc tail -a [apps name]` it should shows what happen in log file. – kucing_terbang May 15 '15 at 03:08
  • Please can you take a look at this question? http://stackoverflow.com/questions/30933408/openshift-does-not-react-on-interval-of-mysql – MrPencil Jun 21 '15 at 08:26
0

You could replace those variables as below .

       <property name="url"                                                                                                              
      value="jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/
       yourdatabasename" />

       can be replaced as below.

     <property name="url"  
    value="jdbc:mysql://127.12.97.2:3306/yourdatabasename"
    />

jdbc:mysql://127.12.97.2:3306/yourdatabasename"The IP address and the portnumber can be obtained from the openshift phpmyadmin page and they are usually displayed on top of the admin page.

user1419261
  • 829
  • 8
  • 5
0

I tried all the above solutions but it didn't solve my problem. So anyone still getting that exception, try this. The value stored in environment variable $OPENSHIFT_MYSQL_DB_HOST is something like

'jdbc:mysql://adminuname:adminpass@127.02.0.1:3306/appname'

. Therefore, using that value to create the url for DriverManager.getConnection() gives us the exception. Instead try to know the value stored in $OPENSHIFT_MYSQL_DB_HOST and then hard code the url into a String variable without that username and password. Something like this

'jdbc:mysql://127.02.0.1:3306/appname'

For me it started working after making this modification. All other environment variables can be used as it is.

I found this solution here.

Sahil Patel
  • 684
  • 7
  • 19