title: How to reconnect to Database quick in AsyncTask?
body: <p>If the database connection gets lost, it took more than one minute to reconnect. How can I speed up that reconnect? </p>
  
  <pre><code>class MssqlConnectorHelper (val view: View){
  
  private var isConnected = false
  lateinit var  mssqlConnector :MssqlConnector
  var quitTask = false
  
  inner class SyncData : AsyncTask&lt;String, String, String&gt;() {
  
      override fun onPreExecute() {
          mssqlConnector = MssqlConnector()
      }
  
      override fun doInBackground(vararg params: String?): String {
          var conn : Connection? = null
  
          while (!quitTask) {
              if (conn == null || !isConnected) {
                  conn = mssqlConnector?.dbConn()
                  isConnected = conn != null
              }
  
              if (conn != null &amp;&amp; isConnected) {
                  try {
                      val preparedStatement = conn.prepareStatement(params[0])
                      val cursor = preparedStatement.executeQuery()
                      if (cursor != null) {
                          while (cursor.next()) {
                              Log.i("SQL Result: ", cursor.getInt(1).toString() + " "
                                      + cursor.getInt(2).toString() + " "
                                      + cursor.getInt(3).toString() + " "
                                      + cursor.getInt(4).toString())
                          }
                      }
                  }catch (e : Exception) {
                      isConnected = false
                      conn = null
                      Log.e("SQL Error", "Database Exception: "+e.message)
                  }
              }else {
                  Log.e("SQL Error", "Connection Error")
              }
              Thread.sleep(3000)
          }
          return "end background"
      }
  }
  
  fun getPickUpOrder() {
      SyncData().execute("SELECT * FROM pickUpOrder WHERE iStatus = 4")
  }
  }
  </code></pre>
  
  <p>I get an Network error IOException: Connection refused and a ConnectException: Connection refused. After that the reconnect works fine.</p>