Search code examples
javaloopsjdbcstack-overflowout-of-memory

Getting out of memory and stackoverflow exception in java app that loops indefinitely


I have a simple app that listens for UDP messages. The app needs to run indefinitely. It instantiates a single jdbc database connection by instantiating the MySQLConnect object I created It looks like this:

 public MySQLConnect() {
    this.instantiateConnection();
}

//Open the database connection.  Done iniitally in 
//the main class and only called again if the connection 
//is closed due to an error in processing a message
public Connection instantiateConnection() {

    try {

        Class.forName("com.mysql.jdbc.Driver");

        connection = DriverManager
                .getConnection("jdbc:mysql://localhost:3306/mydb?"
                        + "user=user&password=pwd");
    } catch (Exception e) {
        e.printStackTrace();
    }

    return connection;
}

The MySQLConnect class constructor is called from the UDPReceiver class once one the app is started up. It is then only called again if there is an error processing and the database connection is closed. This class looks like:

 public class UDPReceiver {

private static int port = 2140;
private static int byteSize = 1024;
private static int timeOut = 5000;
@SuppressWarnings("unused")
private static int count;
static MySQLConnect dbConnect;


   public static void main(String[] args) {

    recvUDPMessage();
}

public static String recvUDPMessage() {
    DataTransferService dts = new DataTransferServiceImp();
    dbConnect = new MySQLConnect();

    try {

        DatagramSocket dsocket = null;
        if (dsocket == null) {
            dsocket = new DatagramSocket(port);
            dsocket.setBroadcast(true);
            dsocket.setReuseAddress(false);
        }
        byte[] inbuf = new byte[byteSize];
        byte[] rcvMsg;
        InetAddress fromIP;

        DatagramPacket receivepacket = new DatagramPacket(inbuf,
                inbuf.length);
        dsocket.setSoTimeout(timeOut);

        //Infinitely loop and listen for UDP messages

        count = 0;
        boolean loopRecv = true;
        while (loopRecv) {
            try {
                count++;
                dsocket.receive(receivepacket);
                // temp = receivepacket.getAddress().toString();
                fromIP = receivepacket.getAddress();
                String fromIPString = fromIP.toString();
                rcvMsg = receivepacket.getData();
                String rcvString = new String(rcvMsg, 0, rcvMsg.length);
                String rcvMessage = "Message Received from:  "
                        + fromIPString + " Message:  " + rcvString + "\n";
                System.out.println(rcvMessage);



                ArrayList<String> al = getMessageElements(rcvString);



                //Send array of message elements to service layer
                dts.saveUDPMessage(dbConnect, al, Utils.getTimeStamp());

                loopRecv = true;
            } catch (IOException e) {
                System.out.println("Listening . . .");



                loopRecv = true;
            }
        }
    } catch (SocketException e) {
        System.err.println("Sockets Exception: " + e.getMessage());
    } catch (Exception e) {
        System.err.println(" Exception: " + e.getMessage());

    } finally {
        System.out.println(". . . Close DB");
        dts.closeDBConnection(dbConnect);


                    // I added the creation MySQLConnect object after I was getting an error that the database was closed when trying to insert.
        dbConnect = new MySQLConnect();
    }
    return "end of routine";
}

//Extract comma delimited message elements into an array
private static ArrayList<String> getMessageElements(String rcvString) {
    StringTokenizer st = new StringTokenizer(rcvString, ",");

    ArrayList<String> al = new ArrayList<String>();

    while (st.hasMoreElements()) {
        String messageElement = (String) st.nextElement();
        al.add(messageElement);

    }



    return al;
}

}

This runs for about 8 hours and then I get the following error:

Exception in thread main java.lang.stackoverflowerror

Exception: java.lang.outofmemoryerror thrown from uncaughtexceptionhandler in thread main

Previously, I did not reinstantiate the MySQLConnect object after the database was closed. The problem is that I received an error that the database connection was closed, but my program was still trying to do a jdbc insert. The jdbc insert first checks to see if there is a connection instantiated and if not instantiates it. This code from the MySQLConnect class looks like:

 PreparedStatement prep = null;

    if (connection == null) {
        connection = this.instantiateConnection();
    }

    try {

        prep = connection
                .prepareStatement("insert into MyTable (UDPMessage)"
                        + "values (?);");

        prep.setString(1, udpMessage);




        prep.addBatch();

        prep.executeBatch();

How can architect this process to correctly to handle UDP messages coming in indefinitely and writing to the database even when errors occur?

Also how can I resolve getting the out of memory error when I re-instantiate the MySQLConnect class when there is an exception in processing the data?

If it is incorrect to instantiate this class after an exception, how can I re-instantiate the connection to the database to continue processing data?

Thanks for your help!


Solution

  • You haven't set boolean loopRecv to false anywhere in your code, which leading to infinite loop (while condition always resolves to true). Infinite/recursive loops keep on populate stack, but don't remove any stack frames and leads to StackOVerflowError.