Skip to content

Inconsistence in Spring @Transactional(Propagation.NESTED) + Exposed #1264

@stengvac

Description

@stengvac

Hello again me :),

exposed: 0.32.1, SB: 2.5.0, DB driver: org.testcontainers.jdbc.ContainerDatabaseDriver delegating to org.postgresql.Driver

this problem with @Transactional(Propagation.NESTED) is simmilar to Exposed/issues/1263. Problem is that, functions annotated with @Transactional(Propagation.NESTED) does not behave correctly or no error is produced when they are in code.

After some investigation I have found out, that AbstractPlatformTransactionManager define variable nestedTransactionAllowedwhich by dafults is left with value true - filled in constructor. Then spring does not throw NestedTransactionNotSupportedException when there are @Transactional(Propagation.NESTED) in code. On the other hand SpringTransactionManager behave like nested transactions are disabled because Database instance has useNestedTransactions set to false. Thus inconsistence.

Here is my playground with nested transacions: spring boot app there are some comments on flow maybe it can be used for future impl. I have forcefully set db.useNestedTransactions = true for those tests.

I have also tried nested tx with exposed DSL but it did not work for me. What is wrong with this example? This code is part of spring boot app I would expect data with id val exposedId = UUID.randomUUID() to be present in DB after those calls.

        val exposedId = UUID.randomUUID()
        transaction {
            println("useNestedTransactions allowed ${db.useNestedTransactions}")
            someRepo.insertWithoutTransaction(exposedId)

            //if i understand correctly this code should be repeated 3x and fails should not abort outer tx
            transaction {
                try {
                    someRepo.insertWithoutTransaction(exposedId)
                } catch (e: ExposedSQLException) {
                    e.printStackTrace()
                }
            }
        }

        val r = transaction {
            someRepo.findById(exposedId)
        }
        //for some reason this is null
        println("insert with exposed tx present ${r != null}")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions