Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8389833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:49:39+00:00 2026-06-09T18:49:39+00:00

I have a simple app that listens for UDP messages. The app needs to

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:49:41+00:00Added an answer on June 9, 2026 at 6:49 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple app that listens to a socket connection. Whenever certain chunks
I have a simple PHP app that prints 'hello world'. When I run it
I have a simple app that use org.springframework.jdbc.datasource.DataSourceTransactionManager to manage the transactions. My spring
I have a very simple OSGI app that just listens on register/unregister and sends
This is a real newbie question. I have simple app that selects a picture
I have one simple app that suppose to get information from user and send
I have a simple app that will allow a user to tag a location.
Right so I have a simple app that consists of a calendar a Set
I'm now testing GKPeerPickerController. I have a simple app that just have a button
I have a simple catalogue app that lets users download product data from an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.