1

I have some troubles when I recieve the json data from my application in app engine, some cases I recieve the json empty and in the log viewer of my application appears the following

    2014-07-03 16:46:21.193 /service/concentraciondeptos/ 400 43ms 0kb Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 module=default version=2
181.48.97.68 - - [03/Jul/2014:14:46:21 -0700] "GET /service/concentraciondeptos/ HTTP/1.1" 400 146 - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36" "Instance ID" ms=44 cpu_ms=80 cpm_usd=0.000016 app_engine_release=1.9.6 trace_id=e2164f3bbeb2302c384164569b22b8dd 

and here's my code

package com.servinfo.services.controller;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.servinfo.database.Manager;
import com.servinfo.services.AbstractController;
import com.servinfo.services.entity.ResponseObject;
import com.servinfo.services.entity.afe.ConcentracionDeptos;

@Controller
@RequestMapping("service/concentraciondeptos/*")
public class ConcentracionDeptosController {

    // private static final Logger log = Logger.getLogger("AreaTController");

    protected final static String RESPONSE_FORMAT = "application/json";

    private final static String GET = "";

    private static String STATE = "";

    private static String MESSAGE = "";


    @RequestMapping(value = GET, method = RequestMethod.GET, produces = RESPONSE_FORMAT)
    public @ResponseBody
    ResponseEntity<ResponseObject> getPoint() {

        if (getData().isEmpty()) {
            STATE = "ERROR";
            MESSAGE = ResponseObject.MSG_EMPTY;
        }

        // log.warning("Servicio areat");
        ResponseObject response = new ResponseObject(STATE, MESSAGE, getData());

        if (response.getStatus().equals("OK")) {
            return new ResponseEntity<ResponseObject>(response,
                    AbstractController.getHeader(), HttpStatus.OK);
        }

        return new ResponseEntity<ResponseObject>(response,
                AbstractController.getHeader(), HttpStatus.BAD_REQUEST);

    }

    public List<ConcentracionDeptos> getData() {
        STATE = "ERROR";
        MESSAGE = ResponseObject.MSG_ERROR;
        int intContador = 0;

        List<ConcentracionDeptos> result = new ArrayList<ConcentracionDeptos>();
        try {
            String sql = "select depto.coddane "
                    + ""
                    + "from departamentos as depto, proyecto as p, aporte_depto as apdepto,"
                    + "aporte_muni as apomuni, ciudades as ciu"
                    + " "
                    + "where apdepto.nit = p.nit and apdepto.id_proyecto = p.id "
                    + "and apdepto.coddane_depto = depto.coddane and apomuni.nit = p.nit "
                    + "and apomuni.id_proyecto = p.id and apomuni.coddane_ciudad = ciu.coddane";

            Manager mng = new Manager();
            ResultSet rs = mng.executeQuery(sql);
            if (rs.first()) {
                rs.beforeFirst();
                STATE = "OK";
                MESSAGE = ResponseObject.MSG_SUCCESS;
            } else {
                Thread.sleep(200);
                rs = mng.executeQuery(sql);
                if (rs.first()) {
                    rs.beforeFirst();
                    STATE = "OK";
                    MESSAGE = ResponseObject.MSG_SUCCESS;
                } else {
                    STATE = "ERROR";
                    MESSAGE = ResponseObject.MSG_ERROR;
                }
            }
            ConcentracionDeptos obEntity = new ConcentracionDeptos();
            while (rs.next()) {
                intContador++;
                obEntity.setCoddane(rs.getString("coddane"));
            }
            obEntity.setCantidadP(Integer.toString(intContador));
            result.add(obEntity);
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

when I execute the sql command in mysql I retrieve the correct information. and here's th connection code

import java.sql.*;
import com.google.appengine.api.utils.SystemProperty;

public class Manager {

    public ResultSet executeQuery(String sql){

        try{
            String url = null;
            if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
                Class.forName("com.mysql.jdbc.GoogleDriver");
                url = "jdbc:google:mysql://ID:App ID/DB?user=root";
            } else {
                Class.forName("com.mysql.jdbc.Driver");
                url = "jdbc:mysql://IP:3306/afe_services?user=root&password=****";
            }

            Connection conn = DriverManager.getConnection(url);
            ResultSet rs = conn.createStatement().executeQuery(sql);
            return rs;

        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
}

what I'm doing bad?

Thanks.

  • Can you advise if you can access that URL directly or if the error comes up? Also can you put in the code that is making the call to the url and also processing the response. – Aeseir Jul 03 '14 at 22:58
  • I'm test the connection but in a few intents the connection fails but if I attempt again the connection is established correctly I post the connection code and thanks for the response :) – user3803423 Jul 04 '14 at 13:08
  • Could you paste the stacktrace ? – Pracede Jul 04 '14 at 13:14
  • I got the nex message 2014-07-04 09:35:42.404 com.servinfo.services.controller.AreaTController getData: 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. – user3803423 Jul 04 '14 at 14:36

1 Answers1

0

EDIT 2:

After lengthy discussion the issue here is that there is no database connection and maintenance of session.

Thus when you call the database, it fails, thus the no connection error.

You need to setup your connection something like described in the spring.io/guides page. The web tutorial gives indepthy database connection and maintenance explanation.

for quick explanation or dirty version setup you can check out http://spring.io/guides/gs/relational-data-access/

Aeseir
  • 7,754
  • 10
  • 58
  • 107
  • Well I tried this but witout any change in the response, I get the next message com.servinfo.services.controller.AreaTController getData: 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. – user3803423 Jul 04 '14 at 15:22
  • So its your connection with the database that is failing. In the code i don't see you opening and controlling the session with DB. – Aeseir Jul 05 '14 at 05:06
  • Well I don't know how to control the connection, in the code above I get the connection with a connection manager but I can't close that connection after the query request to the database when I use the command "conn.Close();" The stack give me the next error: "Uncaught exception from servlet java.lang.NullPointerException" – user3803423 Jul 05 '14 at 21:15
  • How do you connect to your db and manage sessions then? Show me you database setup – Aeseir Jul 06 '14 at 07:15
  • ]I describe this code above, I use a class called Manager, and in the method "executeQuery" I give the sql sentence from other class I simply invoque the method and the metod establish the connectiond and execute the query and retrieve all the data requested – user3803423 Jul 07 '14 at 13:17
  • Yes i seen the call to new Manager() but that doesn't say how you connect to your database and maintain transactions/sessions/connection. You need to share that part. – Aeseir Jul 07 '14 at 13:18
  • well I don't know how to maintain the transactions/sessions/connection I only use all the code described above, :( – user3803423 Jul 07 '14 at 13:58
  • Bingo. We found your problem. I'll re-edit the answer. – Aeseir Jul 07 '14 at 14:15
  • No worries, make sure you accept it, took a while to get there would be a shame to waste those rep points. – Aeseir Jul 07 '14 at 14:33