Skip to content

Commit 892e709

Browse files
committed
#654 Fix db_version not created when postgresql autocommit=false.
1 parent d9d3aa4 commit 892e709

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

pramen/core/src/main/scala/za/co/absa/pramen/core/rdb/PramenDb.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import za.co.absa.pramen.core.reader.JdbcUrlSelector
2727
import za.co.absa.pramen.core.reader.model.JdbcConfig
2828

2929
import java.sql.Connection
30+
import scala.util.Try
3031
import scala.util.control.NonFatal
3132

3233
class PramenDb(val jdbcConfig: JdbcConfig,
@@ -43,6 +44,11 @@ class PramenDb(val jdbcConfig: JdbcConfig,
4344
def db: Database = slickDb
4445

4546
def setupDatabase(): Unit = {
47+
// Explicitly set auto-commit to true, overriding any user JDBC settings or PostgreSQL defaults
48+
Try(jdbcConnection.setAutoCommit(true)).recover {
49+
case NonFatal(e) => log.warn(s"Unable to set autoCommit=true fdr the bookkeeping database tha uses the driver: ${jdbcConfig.driver}.")
50+
}
51+
4652
val dbVersion = rdb.getVersion()
4753
if (dbVersion < MODEL_VERSION) {
4854
initDatabase(dbVersion)

pramen/core/src/main/scala/za/co/absa/pramen/core/rdb/RdbJdbc.scala

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@
1616

1717
package za.co.absa.pramen.core.rdb
1818

19-
import java.sql.Connection
20-
19+
import org.slf4j.LoggerFactory
2120
import za.co.absa.pramen.core.rdb.RdbJdbc.dbVersionTableName
2221

22+
import java.sql.{Connection, SQLException}
2323
import scala.util.control.NonFatal
2424

2525
object RdbJdbc {
2626
val dbVersionTableName = "db_version"
2727
}
2828

2929
class RdbJdbc(connection: Connection) extends Rdb{
30+
private val log = LoggerFactory.getLogger(this.getClass)
31+
3032
override def getVersion(): Int = {
3133
getDbVersion()
3234
}
@@ -37,18 +39,24 @@ class RdbJdbc(connection: Connection) extends Rdb{
3739
executeDDL(s"INSERT INTO $dbVersionTableName (version) VALUES (0)")
3840
}
3941

40-
executeDDL(s"UPDATE db_version SET version = $version")
42+
executeDDL(s"UPDATE $dbVersionTableName SET version = $version")
4143
}
4244

4345
override def doesTableExists(tableName: String): Boolean = {
4446
val meta = connection.getMetaData
45-
val res = meta.getTables(null, null, tableName, Array[String]("TABLE"))
46-
if (res.next) {
47-
res.close()
48-
true
49-
} else {
50-
res.close()
51-
false
47+
try {
48+
val res = meta.getTables(null, null, tableName, Array[String]("TABLE"))
49+
if (res.next) {
50+
res.close()
51+
true
52+
} else {
53+
res.close()
54+
false
55+
}
56+
} catch {
57+
case ex: SQLException =>
58+
log.warn(s"Error while checking existence of $tableName.", ex)
59+
false
5260
}
5361
}
5462

0 commit comments

Comments
 (0)