I am trying to write integration test using @trans...
# spring
s
I am trying to write integration test using @transaction annotaion. My integration test uses test containers. Whenever I remove the tranasction annotation, I am able to ser data in test containers, in docker. However if i add transaction annotation, i don't see data in mysql test container. Wondering at what point does data get commited, if tranaction annotation is used at method level in a springboot integration test?
k
I think if you annotate your test with @Transactional then it is automatically rollbacked at the end of the test, therefore you see no data.
s
But i was in debug mode, trying to debug my test. The test had not completed.
k
so? did you check via
read uncommited
? what would you expect to see?
s
Sorry what you mean by read uncommited? I am fairly new to spring. The tranactions gets rolledback at end of test. I was at the point where we saved in repository. When i saved in repository and had @transaction annotation on my test, and the test was not over, why do you say I will not see data in test container mysql image?
n
is that because you’re using kotlin? 🤔
s
Not sure if that is realted. If i remove @transactional annotation and add @commit annotation. I see data at the point where i am trying to save in repository.
Could be possible because using transaction annotation data is commited at end of transaction and it instantly gets rolled back and hence i cannot see anything.
k
look, when you annotate your business method with @Transactional, if there is no previous transaction associated, it wil start new transaction and when this method ends, entitymanager is flushed and transaction is commited
now when you annotate your test method, transaction is started at start of your test, your business method will inherit/join this transaction so when your business method ends nothing is commited, and if its flushed you can read it via some kind of DB explorer if you change isolation level to read uncommited. otherwise there is no data flushed or commited and when your test method ends the transaction is being rollbacked
therefore you cannot see any data
and yes this is most probably not related to kotlin
s
@kqr thank you for replying. I had a question for you, when I am fetching from repository and doing my assertions, the transaction has not yet commited. So in repositiry, the query was able to read uncommited data? Is there some hibernate cache involved here. Wondering how query found data , when it was not commited.
I did try using saveAndFlush, when i was trying to debug my test line by line. But did not sed data in DB.
k
you can see data within same transaction
you would see data in DB if you switch your reading connection to read uncommited
in Intellijs DB viewer you can switch it when viewing table in selectbox above table