Note: This is a beta release of Red Hat Bugzilla 5.0. The data contained within is a snapshot of the live data so any changes you make will not be reflected in the production Bugzilla. Also email is disabled so feel free to test any aspect of the site that you want. File any problems you find or give feedback here.
Bug 1065106 - Can't connect to mysql 5.5 in scaled application
Summary: Can't connect to mysql 5.5 in scaled application
Alias: None
Product: OpenShift Online
Classification: Red Hat
Component: Image
Version: 2.x
Hardware: Unspecified
OS: Unspecified
Target Milestone: ---
: ---
Assignee: Ben Parees
QA Contact: libra bugs
Depends On:
TreeView+ depends on / blocked
Reported: 2014-02-13 23:45 UTC by Arvid
Modified: 2015-05-15 00:37 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Last Closed: 2014-02-14 23:01:11 UTC
Target Upstream Version:

Attachments (Terms of Use)

Description Arvid 2014-02-13 23:45:49 UTC
Description of problem:
I have a scaled app running tomcat 7. I have also added the mysql 5.5 cartridge.
The problem is that my java application can't connect to the database. I use the host specified in the env variables, in my case:

I can connect to the database if I SSH into my application, or use port forward and use mysql workbench.

In my java application I got the following error:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
	at java.lang.reflect.Constructor.newInstance(
	at com.mysql.jdbc.Util.handleNewInstance(
	at com.mysql.jdbc.SQLError.createCommunicationsException(
	at com.mysql.jdbc.MysqlIO.<init>(
	at com.mysql.jdbc.ConnectionImpl.createNewIO(
	... 86 more
Caused by: Connection timed out
	at Method)
	at com.mysql.jdbc.StandardSocketFactory.connect(
	at com.mysql.jdbc.MysqlIO.<init>(
	... 87 more

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:

Actual results:

Expected results:

Additional info:

Comment 1 Ben Parees 2014-02-14 00:33:35 UTC
Does your app work successfully when the app is not scaled?

Comment 2 Ben Parees 2014-02-14 00:35:31 UTC
Also can you share w/ me the exact code and environment variables you are using to make the connection?

Comment 3 Arvid 2014-02-14 06:47:54 UTC
Yes it worked when it was not scaled. Then I used a IP adress to connect to the db.

Now I got a timeout, so it seems that my application server can't reach the db host.

I create the db connection with spring and the following config:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://"/>
        <property name="username" value="USERNAME"/>
        <property name="password" value="PASSWORD"/>
        <property name="testOnBorrow" value="true" />
        <property name="validationQuery" value="select 1" />

Comment 4 Zhe Wang 2014-02-14 10:47:18 UTC
From QE:

I was not able to reproduce this bug with the following steps in devenv:

1. create a scalable jbossews-2.0 + mysql-5.5 app
2. write a simple test page to play with the DB

<%@ page contentType="text/plain" language="java" import="java.sql.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="*"  %>
<%@ page import="java.util.*"  %>
<%@ page import="java.text.*"  %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.sql.*" %>
String version = request.getParameter("version");
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/MysqlDS");
Connection connection=ds.getConnection();
Statement statement = connection.createStatement();
statement.executeUpdate("CREATE TABLE IF NOT EXISTS factory(id INT UNSIGNED NOT NULL, data TEXT, PRIMARY KEY(id));");
ResultSet rscount = statement.executeQuery("SELECT COUNT(*) FROM factory;");
if( {
    if(Integer.parseInt(rscount.getString(1)) == 0) {
        statement.executeUpdate("INSERT INTO factory VALUES(1, '1');");
if(version != null) {
    statement.executeUpdate("UPDATE factory SET data=" + version + " WHERE id=1;");
    out.print("The mysql factory is modified");
else {
    ResultSet rs = statement.executeQuery("SELECT * FROM factory;");
    if( {
        out.print("version " + rs.getString(2));

and the version of the DB connector is mysql-connector-java-5.1.20-bin.jar, placed under $APP_REPO/src/main/webapp/WEB-INF/lib/

3. The test page works well after scaling the apps:

Comment 5 Arvid 2014-02-14 11:51:12 UTC
Ok, I will test with your testpage.

How do you setup the datasource comp/env/jdbc/MysqlDS? Is it created automatically when creating app or do you config it yourself?


Comment 6 Ben Parees 2014-02-14 14:37:13 UTC
yes it's automatically setup, see:  <GEAR_HOME>/jbossews/conf/context.xml

You can also edit the context.xml in your git repo (repo/.openshift/config/context.xml) and it will get variable-substituted during app startup.

Comment 7 Arvid 2014-02-14 23:01:11 UTC
Ooh, thanks. I solved the problem. 

I had to update my mysql connector, because I used mysql 5.1 before. Thanks for your help.

Comment 8 Ben Parees 2014-02-14 23:09:40 UTC
Great! glad we could help.

Comment 9 Ben Parees 2014-02-15 23:53:45 UTC
I tested this using a tomcat7 app, it appears to work for me, here's the code I used:

      Context context = new InitialContext();
      Context envCtx = (Context) context.lookup("java:comp/env");
      ds = (DataSource) envCtx.lookup("jdbc/MysqlDS");

No explicit tomcat config needed other than what OpenShift sets up for you.

Note You need to log in before you can comment on or make changes to this bug.