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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:07:53+00:00 2026-05-14T22:07:53+00:00

I have a single thread trying to connect to a database using JDBCTemplate as

  • 0

I have a single thread trying to connect to a database using JDBCTemplate as follows:

JDBCTemplate jdbcTemplate =  new JdbcTemplate(dataSource); 

try{
    jdbcTemplate.execute(new CallableStatementCreator() {
        @Override
        public CallableStatement createCallableStatement(Connection con)
        throws SQLException {
            return con.prepareCall(query);
        }
    }, new CallableStatementCallback() {
        @Override
        public Object doInCallableStatement(CallableStatement cs)
        throws SQLException {
            cs.setString(1, subscriberID);
            cs.execute();
            return null;
        }
    });
 } catch (DataAccessException dae) {
     throw new CougarFrameworkException(
             "Problem removing subscriber from events queue: "
             + subscriberID, dae);
 }

I want to make sure that if the above code throws DataAccessException or SQLException, the thread waits a few seconds and tries to re-connect, say 5 more times and then gives up. How can I achieve this? Also, if during execution the database goes down and comes up again, how can i ensure that my program recovers from this and continues running instead of throwing an exception and exiting?

Thanks in advance.

  • 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-14T22:07:53+00:00Added an answer on May 14, 2026 at 10:07 pm

    Try this. My considerations are : run a loop until the statements executed successfully. If there is a failure, tolerate the failure for 5 times and each time it will wait for 2 seconds for the next execution.

    JDBCTemplate jdbcTemplate =  new JdbcTemplate(dataSource); 
    boolean successfullyExecuted = false;
    int failCount = 0;
    while (!successfullyExecuted){
     try{
        jdbcTemplate.execute(new CallableStatementCreator() {
            @Override
            public CallableStatement createCallableStatement(Connection con)
            throws SQLException {
                return con.prepareCall(query);
            }
        }, new CallableStatementCallback() {
            @Override
            public Object doInCallableStatement(CallableStatement cs)
            throws SQLException {
                cs.setString(1, subscriberID);
                cs.execute();
                return null;
            }
        });
        successfullyExecuted = true;
     } catch (DataAccessException dae) {
         if (failedCount < 5){
            failedCount ++;
            try{java.lang.Thread.sleep(2 * 1000L); // Wait for 2 seconds
            }catch(java.lang.Exception e){}
         }else{
         throw new CougarFrameworkException(
                 "Problem removing subscriber from events queue: "
                 + subscriberID, dae);
         }
     } catch (java.sql.SQLException sqle){
         if (failedCount < 5){
            failedCount ++;
         }else{
         try{java.lang.Thread.sleep(2 * 1000L); // Wait for 2 seconds
         }catch(java.lang.Exception e){}
         throw new CougarFrameworkException(
                 "Problem removing subscriber from events queue: "
                 + subscriberID, dae);
         }
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a single xml file and every new thread of the program (BHO)
I have a WCF service using NHibernate, I am trying to connect this WCF
I am trying to add a parallel processing component to a single thread single
I'm trying to run multiple SQL queries in a single SQL server thread through
I have a single threaded ActiveMQ consumer written in Java. All I'm trying to
I'm new to WinDbg and have started using it this week to diagnose memory
I have several apps that I'm trying to merge into a single suite: 2
I'm trying to list available interfaces using the WSAIoctl function. I have to pass
I am using Qt and trying to achieve a single instance application by applying
I have a thread that creates COM objects that require a single threaded apartment.

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.