0

I have a project in which i connect to my Local Network DB. I Insert,Update and Select from DB, and DB type is SQL Server 2005. I made a class named ConnectionHelper and everywhere i want to connect to DB, i make an Instance of it and then call its getConnection Method. This class has a Constructor like this :

public ConnectionHelper() {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    setConnection(connection);

    try {

        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        setIpAddress("X.X.X.X:1433");
        setDb("XXX");
        setUserName("XX");
        setPassword("XXXX");

        setConnectionURL("jdbc:jtds:sqlserver://" + getIpAddress() + ";"
                + "databaseName=" + getDb() + ";user=" + getUserName()
                + ";password=" + getPassword() + ";");
       setConnection(DriverManager.getConnection(getConnectionURL()));
    } catch (SQLException se) {
        Log.e("ERRO", se.getMessage());
    } catch (ClassNotFoundException e) {
        Log.e("ERRO", e.getMessage());
    } catch (Exception e) {
        Log.e("ERRO", e.getMessage());
    }
}

And this is getConnection Method :

public Connection getConnection() {
    return connection;
}

And here is the sample of using this with Callable Statement( Call an Update Stored Procedure) :

ConnectionHelper connectionHelper = new ConnectionHelper();
CallableStatement callableStatement;
try {
    callableStatement = connectionHelper.getConnection().prepareCall("{call MySP(?, ?, ?)}");
     callableStatement.setInt(1, X);
     callableStatement.setInt(2, X));
     callableStatement.setInt(3, X);
     callableStatement.executeUpdate();

} catch (SQLException e) {
    e.printStackTrace();
}

Well, it works good, But Android Devices connect to Local Network via WIFI, and sometimes they could not connect to DB due to Network Problems and then App close it by itself with unfortunately Error. I want to handle if it could not connect to DB, retry again or display an Toast message to user and do not close the App. How can i should handle the Exception?

Jim Rhodes
  • 5,021
  • 4
  • 25
  • 38
Alireza
  • 179
  • 1
  • 2
  • 11

2 Answers2

0

Looks like you need an architecture that includes a "internal error" response to the nodes calling your services.

Must not catch the exception in the device, use web api's or webservices to comunicate with DBs.

Now, if your problem is error while processing because the local network on android is down plase look: How to check internet conection on android

Community
  • 1
  • 1
Pharsat
  • 1
  • 2
0

I think you are already catching an exception - but SQLException - so you might find that the exception for connection issue is a Timeout or some other. In this case you need to change the part of your code into something like this:

ConnectionHelper connectionHelper = new ConnectionHelper();
                    CallableStatement callableStatement;
             try {
                    callableStatement = connectionHelper.getConnection().prepareCall("{call MySP(?, ?, ?)}");
                    callableStatement.setInt(1, X);
                    callableStatement.setInt(2, X));
                    callableStatement.setInt(3, X);
                    callableStatement.executeUpdate();

                    } catch (SQLException e) {
                      //this is a specific SQL exception
                      e.printStackTrace();

                   }
                  catch (Exception e) {
                      //all other exceptions except the one above
                      //here you do your toast message

                   }

Catching the general Exception is good when you do not know all possible exceptions that could be thrown. Please give it a try and let us know if this helps.

ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • Thx, As you did and @Jim said i will do this, but a question, It is runTime Error, is it possible to retry calling my connection and SP ? – Alireza May 06 '16 at 14:48
  • Hi, yes, please try it. In terms of your question - you could come up with your own "retry policy" - something like an "exponential backoff" might work well. – ishmaelMakitla May 06 '16 at 14:54
  • Could you please update your answer and make it two part for the second question i asked in cm? – Alireza May 06 '16 at 14:56
  • Instead, let's know if your "App crashing" problem is solved. Accept the answer so your question is marked as "answered". Then you may ask a different question. This is better, I think. – ishmaelMakitla May 06 '16 at 15:03