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
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: OpenShift Online
Classification: Red Hat
Component: Image
Version: 2.x
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: ---
: ---
Assignee: Ben Parees
QA Contact: libra bugs
URL:
Whiteboard:
Depends On:
Blocks:
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:
Environment:
Last Closed: 2014-02-14 23:01:11 UTC


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: 52fd54405973caa238000492-amplode.rhcloud.com:57031

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(NativeConstructorAccessorImpl.java:57)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
	at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:343)
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
	... 86 more
Caused by: java.net.ConnectException: Connection timed out
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:579)
	at java.net.Socket.connect(Socket.java:528)
	at java.net.Socket.<init>(Socket.java:425)
	at java.net.Socket.<init>(Socket.java:241)
	at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:253)
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:292)
	... 87 more

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


How reproducible:


Steps to Reproduce:
1.
2.
3.

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://52fd54405973caa238000492-amplode.rhcloud.com:57031/test?autoReconnect=true"/>
        <property name="username" value="USERNAME"/>
        <property name="password" value="PASSWORD"/>
        <property name="testOnBorrow" value="true" />
        <property name="validationQuery" value="select 1" />
    </bean>

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

<--------mysql.jsp------------->
<%@ page contentType="text/plain" language="java" import="java.sql.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="java.io.*"  %>
<%@ 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));");
statement.executeUpdate("COMMIT;");
ResultSet rscount = statement.executeQuery("SELECT COUNT(*) FROM factory;");
if(rscount.next()) {
    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(rs.next()) {
        out.print("version " + rs.getString(2));
    }
}
statement.close();
connection.close();
%>
<------------------------------>

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:

http://pastebin.test.redhat.com/191576

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?

/A

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.