سؤال

I am building an app that keeps querying a database once every minute. I used the TimerTask class to query the database. The method worked for some time and it stopped completely. What do I do? Here's the Code below

public class DateMgr extends TimerTask{
        static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
        static final String DB_URL = "jdbc:mysql://localhost:3306/reminder";
        static final String USER = "username";
        static final String PASS = "password";
        @Override
        public void run(){
            Statement stmt = null;
            Connection conn = null;

            try{

                DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                //get current date time with Date()
                Date d = new Date();

                String m = dateFormat.format(d);
                String s = m + ":00.0";
                System.out.println(dateFormat.format(s));
                //STEP 2: Register JDBC driver
                Class.forName("com.mysql.jdbc.Driver");

                //STEP 3: Open a connection
                System.out.println("Connecting to a selected database...");
                conn = DriverManager.getConnection(DB_URL, USER, PASS);
                System.out.println("Connected database successfully...");

                //STEP 4: Execute a query
                System.out.println("Creating statement...");

                stmt = conn.createStatement();
                String sql = "SELECT * FROM  reminder.list";
                ResultSet rs = stmt.executeQuery(sql);
                //STEP 5: Extract data from result set
                while(rs.next()){
                    //Retrieve by column name
                    int id  = rs.getInt("id");
                    String message = rs.getString("Message");
                    String when = rs.getString("When");                                               
                    System.out.println( "Time for " + message + " At " + when);
                }
                rs.close();                                        
            }catch (Exception e){                
            }
        }        
    }

and this is the Timer class with the main() Method

public class Reminder {   
    Timer timer;

    public Reminder(int seconds) {
        timer = new Timer();
        timer.schedule(new DateMgr(), 0, seconds*1000);
    }
    public static void main (String args[]){
        new Reminder(20);
    }          
}
هل كانت مفيدة؟

المحلول

You:

  1. "handle" all exceptions with an empty block;
  2. have no finally where you release the database resources;
  3. have no line of code whatsoever which should close the database connection, even in a "happy day" scenario.

Your program stops working because each time the task runs, one database connection is left hanging. Soon enough there are no more connections to acquire, but you don't get informed of that because you decided not to log the exception.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top