0

I want to create web application on Openshift which use Tomcat, Spring and Hibernate.

First I have tried to configure the database using persistence.xml like this

<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
     <persistence-unit name="springshop_pu" transaction-type="RESOURCE_LOCAL">      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <class>com.codeonblue.springshop.model.Categoria</class>
      <class>com.codeonblue.springshop.model.Produto</class>
      <class>com.codeonblue.springshop.model.Usuario</class>    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.format_sql" value="true"/>    <property name="hibernate.connection.charset" value="UTF-8" />    
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      #{systemEnvironment[OPENSHIFT_MYSQL_DB_HOST]}
      <property name="javax.persistence.jdbc.user" value="#{systemEnvironment[OPENSHIFT_MYSQL_DB_USERNAME]}"/>
      <property name="javax.persistence.jdbc.password" value="#{systemEnvironment[OPENSHIFT_MYSQL_DB_PASSWORD]}"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://#{systemEnvironment[OPENSHIFT_MYSQL_DB_HOST]}:#{systemEnvironment[OPENSHIFT_MYSQL_DB_PORT]}/#{systemEnvironment[OPENSHIFT_APP_NAME]}"/>   

    </properties>   

using the following but when I try to create an EntityManagerFactory i get this error:

java.lang.NumberFormatException: For input string: "#{systemEnvironment[OPENSHIFT_MYSQL_DB_PORT]}"

Later I tried to modify my persistence.xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> 
  <persistence-unit name="springshop_pu" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <class>com.codeonblue.springshop.model.Categoria</class>
      <class>com.codeonblue.springshop.model.Produto</class>
      <class>com.codeonblue.springshop.model.Usuario</class>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.format_sql" value="true"/>
      <property name="hibernate.connection.charset" value="UTF-8" />    
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.user" value="${env.OPENSHIFT_MYSQL_DB_USERNAME}"/>
      <property name="javax.persistence.jdbc.password" value="${env.OPENSHIFT_MYSQL_DB_PASSWORD}"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://${env.OPENSHIFT_MYSQL_DB_HOST}:${env.OPENSHIFT_MYSQL_DB_PORT}/${env.OPENSHIFT_APP_NAME}"/>    
    </properties>
  </persistence-unit>
</persistence>  

But I have got this error:

java.sql.SQLException: Must specify port after ':' in connection string

It seems to me that the openshift database variables are null.

This is the servlet that I do the testing:

 @WebServlet("/GenerateTables")
public class GenerateTables extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public GenerateTables() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub


        System.out.println("Host: " + System.getenv("OPENSHIFT_MYSQL_DB_HOST"));
        System.out.println("Port: " + System.getenv("OPENSHIFT_MYSQL_DB_PORT"));
        System.out.println("Username: " + System.getenv("OPENSHIFT_MYSQL_DB_USERNAME"));
        System.out.println("Password: " + System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD"));


        EntityManagerFactory factory = Persistence
                .createEntityManagerFactory("springshop_pu");
        factory.close();
        System.out.println("Let's see if the tables were generated");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

However this commands in the servlet

System.out.println("Host: " + System.getenv("OPENSHIFT_MYSQL_DB_HOST"));
System.out.println("Port: " + System.getenv("OPENSHIFT_MYSQL_DB_PORT"));
System.out.println("Username: " + System.getenv("OPENSHIFT_MYSQL_DB_USERNAME"));
System.out.println("Password: " + System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD"));

show the variables values in the Tomcat Log

What i am doing wrong here?

Please help me out

James Freitas
  • 504
  • 2
  • 8
  • 21

1 Answers1

0

I gave up using openshift environment variables. I drop the project and started from scratch again but this time I have used the pre configured context that openshift provides (MySQLDS) and that worked perfectly.

First I have inserted some data in database:

--
-- Table structure for table `names`
--
CREATE TABLE IF NOT EXISTS `names` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first` varchar(50) NOT NULL,
  `last` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Data for table `names`
--
INSERT INTO `names` (`id`, `first`, `last`) VALUES
(1, 'John', 'Doe'),
(2, 'Jane', 'Doe'),
(3, 'John', 'Smith'),
(4, 'Jane', 'Smith');

Then i have created the file mysql.jsp as below:

<%@ page language="java" contentType="text/html; charset=US-ASCII" pageEncoding="US-ASCII"%>
<%@ page import='java.sql.*' %>
<%@ page import='javax.sql.*' %>
<%@ page import='javax.naming.*' %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Tomcat 6 &amp; Tomcat 7 (JBOSS EWS) Example</title>
</head>
<body>
<%
Connection result = null;
try {
    InitialContext ic = new InitialContext();
    Context initialContext = (Context) ic.lookup("java:comp/env");
    DataSource datasource = (DataSource) initialContext.lookup("jdbc/MySQLDS");
    result = datasource.getConnection();
    Statement stmt = result.createStatement() ;
    String query = "select * from names;" ;
    ResultSet rs = stmt.executeQuery(query) ;
    while (rs.next()) {
        out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + "<br />");
    }
} catch (Exception ex) {
    out.println("Exception: " + ex + ex.getMessage());
}
%>
</body>
</html>

The code above worked very well.

If somebody wants more detail, follow the instructions in this link: https://help.openshift.com/hc/en-us/articles/202399720-How-to-use-the-pre-configured-MySQLDS-and-PostgreSQLDS-data-sources-in-the-Java-cartridges

Hope that helps someone else.

Cheers!

James Freitas
  • 504
  • 2
  • 8
  • 21