@@ -13,7 +13,9 @@ import org.omg.CORBA.Environment
1313import sun.misc.ObjectInputFilter
1414import java.io.File
1515import java.net.URL
16+ import java.sql.Connection
1617import java.sql.DriverManager
18+ import java.sql.Statement
1719import kotlin.properties.Delegates
1820
1921
@@ -24,8 +26,10 @@ object MonitorService{
2426
2527 fun start (config : Config )= runBlocking {
2628 try {
27- config.getConfigList(" databases" ).forEach {
28- endpointToCheck -> checkIfDbIfReachable(endpointToCheck.getString(" dbname" ), endpointToCheck.getString(" endpoint" ))
29+ DriverManager .getConnection(config.getString(" endpoint" )).use {
30+ config.getConfigList(" databases" ).forEach {
31+ endpointToCheck -> checkIfDbIfReachable(endpointToCheck.getString(" dblabel" ), endpointToCheck.getString(" dbname" ), it)
32+ }
2933 }
3034 }
3135 catch (e: Exception ){
@@ -47,14 +51,15 @@ object MonitorService{
4751 /* *
4852 * Throws an exception if can't reach the db
4953 */
50- private suspend fun checkIfDbIfReachable (dbname : String , url : String ){
54+ private suspend fun checkIfDbIfReachable (dblabel : String , dbname : String , connection : Connection ){
5155 withContext(Dispatchers .IO ) {
5256 try {
53- val connection = DriverManager .getConnection(url)
57+ val stmt: Statement = connection.createStatement()
58+ stmt.execute(" use [$dbname ]" )
59+ stmt.close()
5460 assert (connection.isValid(5000 ))
55- connection.close()
5661 } catch (e: Exception ) {
57- throw Exception (" Connection to DB $dbname failed" )
62+ throw Exception (" Connection to DB $dblabel failed" )
5863 }
5964 }
6065 }
0 commit comments