Search code examples
db2ibm-cloud

Bluemix, Connection refused after few succeeded connections


I developing a RESTful application in Bluemix. When I start my application and send @GET request to JAX-RS service it works ok few first requests, but after a while it begins returning an error. After I seen the logs, I found this error:

com.ibm.db2.jcc.am.io: DB2 SQL Error: SQLCODE=-438, SQLSTATE=42502, SQLERRMC=Connection refused, DRIVER=3.57.82

This is code which I use to get the connection, and of course I close it after I've done my job.

            Gson gson = new Gson();
            JsonObject vcap_services_obj = null;
            Connection conn = null;
            String driver = "com.ibm.db2.jcc.DB2Driver";
            try {
                Class.forName(driver);
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            String icap_services_string = System.getenv("VCAP_SERVICES");
            if(icap_services_string == null){
                icap_services_string = FileProvider.readFile("db.ini");
            }
            if (null != icap_services_string && icap_services_string.length() > 0) {
                 vcap_services_obj = gson.fromJson(icap_services_string, JsonObject.class);
            } 
            if (null != vcap_services_obj) {
                JsonArray vcap_services_array = vcap_services_obj.getAsJsonArray("sqldb");

                JsonObject first_db2 = vcap_services_array.get(0).getAsJsonObject();
                JsonObject first_credential = first_db2.get("credentials").getAsJsonObject();       

                String host = first_credential.get("host").getAsString();
                String port = first_credential.get("port").getAsString();
                String uid = first_credential.get("username").getAsString();
                String pwd = first_credential.get("password").getAsString();
                String dbname = first_credential.get("db").getAsString();

                String dburl = "jdbc:db2:"+"//" + host + ":" + port + "/" + dbname;
                try {
                    conn = DriverManager.getConnection(dburl, uid, pwd);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            return conn;

What can cause to this behavior? Thank you.

UPDATE The problem was that I was opening connections from another methods and haven't closed them. Thank you to Jeeva T for him help.


Solution

  • Which plan are you using? I don't see the close connection in the snippet. Btw, did you know that you can get your entire connection string from your vcap(no need to construct it)?