Skip to content

Commit 98c0795

Browse files
Sharing the connection string among databases
1 parent 0d5e9a9 commit 98c0795

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

src/main/kotlin/EndpointToCheck.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
data class EndpointToCheck(val dbname: String, val endpoint: String)
1+
data class EndpointToCheck(val dbname: String, val dblabel: String)

src/main/kotlin/MonitorService.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import org.omg.CORBA.Environment
1313
import sun.misc.ObjectInputFilter
1414
import java.io.File
1515
import java.net.URL
16+
import java.sql.Connection
1617
import java.sql.DriverManager
18+
import java.sql.Statement
1719
import 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
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
slackbridge = ${?SLACK_BRIDGE}
22
errormessage = "Alert from db monitor:"
3-
3+
endpoint = ${?TRANSITDATA_PUBTRANS_CONN_STRING}
44
databases = [
55
{
6-
dbname = "OMM DB"
7-
endpoint = ${?OMM_DB_ENDPOINT}
6+
dblabel = "OMM DB"
7+
dbname = "OMM_Community"
88
},
99
{
10-
dbname = "LIJ DB"
11-
endpoint = ${?LIJ_DB_ENDPOINT}
10+
dblabel = "ROI DB"
11+
dbname = "ptROI_Community"
12+
},
13+
{
14+
dblabel = "DOI DB"
15+
dbname = "ptDOI4_Community"
1216
}
1317
]

0 commit comments

Comments
 (0)