3

I am dealing with a strange problem connecting to my MySQL database on Amazon RDS using JDBC. I have two different Android phones on the same network running the same code. One connects without any problems. The other is giving me the following exception:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1014)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2547)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at java.sql.DriverManager.getConnection(DriverManager.java:175)
    at java.sql.DriverManager.getConnection(DriverManager.java:209)
    at edu.ucsb.cs.epsilon.ucsb360.DatabaseManager.Connect(DatabaseManager.java:32)
    at edu.ucsb.cs.epsilon.ucsb360.Global.applicationResumed(Global.java:31)
    at edu.ucsb.cs.epsilon.ucsb360.MainActivity.onResume(MainActivity.java:54)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1236)
    at android.app.Activity.performResume(Activity.java:4625)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2834)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2889)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:156)
    at android.app.ActivityThread.main(ActivityThread.java:5045)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1190)
    at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:245)
    at java.net.InetAddress.getAllByName(InetAddress.java:220)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:249)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)

The database only shows 1 active connection, so I know it is not because of too many open connections. I have tried rebooting the server, but it didn't help. I am using the MySQL JDBC Connector/J version 5.1.23. The phone that works is running Android version 2.3.5 and the one that doesn't work is running Android version 4.0.4. Does anyone have any ideas as to what might be wrong?

Thanks!

1 Answer 1

2
Caused by: android.os.NetworkOnMainThreadException

You can't do network IO on the main thread. Move it to an AsyncTask or thread.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, that makes sense. I was too focused on the MySQL/JDBC side of things and I didn't even consider it as an Android issue. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.