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 3317454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:35:22+00:00 2026-05-17T22:35:22+00:00

How to set a Timer, say for 2 minutes, to try to connect to

  • 0

How to set a Timer, say for 2 minutes, to try to connect to a Database then throw exception if there is any issue in connection?

  • 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-05-17T22:35:23+00:00Added an answer on May 17, 2026 at 10:35 pm

    So the first part of the answer is how to do what the subject asks as this was how I initially interpreted it and a few people seemed to find helpful. The question was since clarified and I’ve extended the answer to address that.

    Setting a timer

    First you need to create a Timer (I’m using the java.util version here):

    import java.util.Timer;
    

    ..

    Timer timer = new Timer();
    

    To run the task once you would do:

    timer.schedule(new TimerTask() {
      @Override
      public void run() {
        // Your database code here
      }
    }, 2*60*1000);
    // Since Java-8
    timer.schedule(() -> /* your database code here */, 2*60*1000);
    

    To have the task repeat after the duration you would do:

    timer.scheduleAtFixedRate(new TimerTask() {
      @Override
      public void run() {
        // Your database code here
      }
    }, 2*60*1000, 2*60*1000);
    
    // Since Java-8
    timer.scheduleAtFixedRate(() -> /* your database code here */, 2*60*1000, 2*60*1000);
    

    Making a task timeout

    To specifically do what the clarified question asks, that is attempting to perform a task for a given period of time, you could do the following:

    ExecutorService service = Executors.newSingleThreadExecutor();
    
    try {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                // Database task
            }
        };
    
        Future<?> f = service.submit(r);
    
        f.get(2, TimeUnit.MINUTES);     // attempt the task for two minutes
    }
    catch (final InterruptedException e) {
        // The thread was interrupted during sleep, wait or join
    }
    catch (final TimeoutException e) {
        // Took too long!
    }
    catch (final ExecutionException e) {
        // An exception from within the Runnable task
    }
    finally {
        service.shutdown();
    }
    

    This will execute normally with exceptions if the task completes within 2 minutes. If it runs longer than that, the TimeoutException will be throw.

    One issue is that although you’ll get a TimeoutException after the two minutes, the task will actually continue to run, although presumably a database or network connection will eventually time out and throw an exception in the thread. But be aware it could consume resources until that happens.

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

Sidebar

Related Questions

Say, if I created a waitable timer using CreateWaitableTimer API and then set it
is there any way to set timer for the progress bar? for example wait
How to set a timer for a fade effect? Say text1 will fade out
Let's say there is a set of Watcher s that need to be refreshed
I want to set timer in a thread and kill that thread when I
i like to set timer for calculating execution time in c# for particular process
If I set a timer to execute a code every 3 seconds. If the
I've got a timer set up to detect if the mouse is over a
How can I use variables to set my timer? I want to control delay
When a timer's autostart is set to true, does it execute the elapsed event

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.