Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

scalikejdbc - Cannot start a new transaction error

177 views
Skip to first unread message

ms101

unread,
Apr 5, 2022, 9:20:48 AM4/5/22
to ScalikeJDBC Users Group
Team,

Trying to get recommendation on below error "Cannot start a new transaction". 

current settings
HikariCP 4.0.3
scalikejdbc 3.5.0

/conf file
//used default settings from documentation plus below line
scalikejdbc.global.jtaDataSourceCompatible.enabled = false

for dbconnections
val a: DataSource = {
val b = new HikariDataSource
//used default settings from documentation plus below line
b.setAutoCommit(false)

for processing,
db: NamedDB = NamedDB(ConnectionPool.DEFAULT_NAME)

db.localTx { implicit session => 
   //do something
db.autoClose(true)  //closing the connection after performing operation.


Error: Cannot start a new transaction with stacktrace
scalikejdbc.DBConnection.newTx


Appreciate your thoughts. Thanks!

ms101

unread,
Apr 12, 2022, 1:28:25 PM4/12/22
to ScalikeJDBC Users Group

Team, Here is the latest after trying out few things.

Turned on the flag,
/conf file
scalikejdbc.global.jtaDataSourceCompatible.enabled = true

If I do that I sometimes encounter the same error because isTxAlreadyStarted was TRUE and it is failing for below check on newTx from DBConnection.scala 

if (!jtaDataSourceCompatible && (isTxNotActive || isTxAlreadyStarted)) {throw new IllegalStateException(ErrorMessage.CANNOT_START_A_NEW_TRANSACTION)}

Revised version of code for processing,
- removed closing connection db.autoClose(true)

db: NamedDB = NamedDB(ConnectionPool.DEFAULT_NAME)

db.autoClose(false)

try {
db.localTx { implicit session => 
   //do something 
   try { loadTables(value) } catch { //exception }

} finally { //log things}


loadTables(data:Seq[TableA])(implicit session: DBSession = AutoSession) {
  // insert schema
}

Appreciate your recommendations. Thanks!
Reply all
Reply to author
Forward
0 new messages