Timeout Android Connection and SQL Server using JDBC/JTDS

Asked

Viewed 315 times

0

I would like to limit a time for the connection. Every time she doesn’t find the Host to connect, she keeps trying and it takes.

Here’s the code:

@SuppressLint("NewApi")
public Connection CONN() {

/*Properties props = new Properties();
props.setProperty("user", "dbuser");
props.setProperty("password", "dbpassword");
props.setProperty(OracleConnection.CONNECTION_PROPERTY_THIN_NET_CONNECT_TIMEOUT, "2000");*/

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
        .permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL = null;
try {
    Class.forName(classs);
    ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
            + "databaseName=" + db + ";user=" + un + ";password="
            + password + ";namedPipe=true;loginTimeout=5;socketTimeout=1";

/*Properties properties = new Properties();
properties.put("connectTimeout", "2000");*/
    DriverManager.setLoginTimeout(5);
    conn = DriverManager.getConnection(ConnURL);
} catch (SQLException se) {
    Log.e("ERRO", se.getMessage());
} catch (ClassNotFoundException e) {
    Log.e("ERRO", e.getMessage());
} catch (Exception e) {
    Log.e("ERRO", e.getMessage());
}
return conn;
}

1 answer

-1

I don’t know if you’ve found a solution or not for what you wanted, but you could always try this solution given here : https://stackoverflow.com/questions/16952403/setting-a-login-timeout-when-connecting-to-sql-server-with-android

Edit.

I managed to solve my situation by creating a method to make me this check.

public boolean checkConnetion()
{
    boolean success;
    try
    {
        _driver = "net.sourceforge.jtds.jdbc.Driver";
        _conString = "jdbc:jtds:sqlserver://" + GlobalVars.SQLServerInfo.get("server") + ":" + GlobalVars.SQLServerInfo.get("port") + "/" + GlobalVars.SQLServerInfo.get("dbname") +
                ";instance=" + GlobalVars.SQLServerInfo.get("instance") + ";loginTimeout=2";
        Class.forName(_driver);
        _con = DriverManager.getConnection(_conString, GlobalVars.SQLServerInfo.get("user"), GlobalVars.SQLServerInfo.get("pass"));
        _con.close();
        success = true;
    }
    catch (Exception e)
    {
        Log.d("checkConnetion", e.toString());
        success = false;
    }
    return success;
}

I have this inside the Class I have to handle my query and the like I need for SQL, and I can call this function whenever I need to do a check.

It could also be implemented as a private and test within the Class itself, but I preferred to test on the outside, since it has to be all by Asynctask

In the logcat when he can’t make the connection this will appear:

java.sql.Sqlexception: Network error Ioexception: failed to connect to /192.168.1.9 (port 1433) after 2000ms

  • While this link may answer the question, it is best to include the essential parts of the answer here and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed. - Of Revision

  • Yes sry, while I was also looking for a solution by reading http://jtds.sourceforge.net/faq.html I found a solution that worked well for me, by way of a response.

  • Why -1 after adding the answer ?! does what the OP wants -.-

Browser other questions tagged

You are not signed in. Login or sign up in order to post.