Issue
If the database connection gets lost, it took more than one minute to reconnect. How can I speed up that reconnect?
class MssqlConnectorHelper (val view: View){
private var isConnected = false
lateinit var mssqlConnector :MssqlConnector
var quitTask = false
inner class SyncData : AsyncTask<String, String, String>() {
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 && 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")
}
}
I get an Network error IOException: Connection refused and a ConnectException: Connection refused. After that the reconnect works fine.
Solution
I found a solution by myself and it's quite easy, just adding the timeout to the jtds URL:
"jdbc:jtds:sqlserver://$ip:$port;instance=MSSQLSERVER;databaseName=$db;user=$username;password=$password;loginTimeout=5"
Answered By - neobatman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.