Hi, here is my scenario:
Using spring-test & junit for unit test, I added @transactional
for each test cases to make sure each cases is independent from each other. It works well when running tests with single thread.
However this exception occurs after I added parallel execution to speed up unit tests :
"java.lang.IllegalStateException: Cannot start new transaction without ending existing transaction" in TransactionalTestExecutionListener
I really cannot figure it out, I spent a week trying to solve it, but it wasn't successful.
Could you please give me some advice for this?
Here is the details about my code:
each test class will extend this abstract test
No other special, just @Test
for each test cases.
- spring-framwork version: 5.3.23
- junit version: 5.9.1
one kindly remind is that, it seems that the first case for each thread will occurs the exception, for example: junit open 50 thread to run tests, the exception will occurs when each thread running the first test case.
Looking forward for your feedback! Thank you very much!
Comment From: sbrannen
If you would like us to investigate this, please provide a minimal sample application that we can download and run (for example, a ZIP file or a public Git repository).
Comment From: mufasa1
@sbrannen spring-test-junit-demo.zip
hi, apologise for the delay... here is the sample, please use JDK8 to run all tests, and you will see "Cannot start new transaction with parallel execution" error msg.
One kindly reminder, it seems that JDK14 works well, I really cannot figurre it out why JDK8 cannot work. Looking forward to your reply, thanks.
Comment From: mufasa1
@sbrannen Hi, any progress or investigation for this?
Comment From: sbrannen
Hi @mufasa1,
Thanks for providing the ZIP file.
I tried it out with JDK 8 and JDK 17 and could not reproduce the error.
However, since I don't have Redis installed locally, I commented out the stringRedisTemplate.keys("*");
line in SuperTest
.
Can you confirm that the error goes away with that change?
And, if the error persists without that change, can you please provide your setup for Redis -- or preferably automatic Redis setup with Testcontainers?
Cheers,
Sam
Comment From: mufasa1
@sbrannen Hi, thanks for your investigation. You don't need to install redis locally, I have 'MockRedisServer' in the project, it will mock a redis server. And please do not comment out the stringRedisTemplate.keys("*"); line in SuperTest, it is the line which will cause the exception: 'Cannot start new transaction without ending existing transaction'.
It seems that the redis call will add extral threads to run tests, and it is the added threads which will cause the exception.
Is there any other unknown exception with the line 'stringRedisTemplate.keys("*")' in your machine?
Comment From: mufasa1
@sbrannen Hi, did you reproduce the error? or do you need me to provide any other info?
Comment From: sbrannen
Hi @mufasa1,
Thanks for the feedback!
I haven't gotten a chance to look into this again, but I've got it on my radar and should be able to take another look in the next week or so.
Comment From: mufasa1
@sbrannen Hi, thanks for taking time, could you please let me know if you hava any progress?
Comment From: sbrannen
Hi @mufasa1,
I tried your application again but without success.
You don't need to install redis locally, I have 'MockRedisServer' in the project, it will mock a redis server.
Yes, I see that you have a MockRedisServer
, but you never invoke start()
on the RedisServer
that you create. So, there is no server to connect to.
I tried to modify your sample application to start the RedisServer
, but it always throws an exception:
java.lang.RuntimeException: Can't start redis server. Check logs for details.
at redis.embedded.AbstractRedisInstance.awaitRedisServerReady(AbstractRedisInstance.java:61)
at redis.embedded.AbstractRedisInstance.start(AbstractRedisInstance.java:39)
at redis.embedded.RedisServer.start(RedisServer.java:9)
I'm sorry, but I cannot afford to spend any more time on this.
If you can provide an updated version of your sample application that starts and connects to the embedded RedisServer
(without any modification), I would be willing to take another look.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: mufasa1
Hi @mufasa1,
I tried your application again but without success.
You don't need to install redis locally, I have 'MockRedisServer' in the project, it will mock a redis server.
Yes, I see that you have a
MockRedisServer
, but you never invokestart()
on theRedisServer
that you create. So, there is no server to connect to.I tried to modify your sample application to start the
RedisServer
, but it always throws an exception:
java.lang.RuntimeException: Can't start redis server. Check logs for details. at redis.embedded.AbstractRedisInstance.awaitRedisServerReady(AbstractRedisInstance.java:61) at redis.embedded.AbstractRedisInstance.start(AbstractRedisInstance.java:39) at redis.embedded.RedisServer.start(RedisServer.java:9)
I'm sorry, but I cannot afford to spend any more time on this.
If you can provide an updated version of your sample application that starts and connects to the embedded
RedisServer
(without any modification), I would be willing to take another look.
@sbrannen Thanks for taking time, sorry I just took a vacation and didn't reply in time, I am going to work on it in and provide a new demo for you, thank you so much.
Comment From: mufasa1
@sbrannen Hi, I am sorry for mading a mistake on the redis mock. Here is the new demo for you, run all tests on JDK 8 and you can see the exception. however all tests will pass if you run tests on JDK11 or JDK14. Thank you so much for taking time! spring-test-junit-demo.zip
Comment From: mufasa1
@sbrannen Hi, just want to know if the new demo works well ?
Comment From: mufasa1
@sbrannen
Do you still have the energy to pay attention to this issue? I am still continuously tracking it ...
Comment From: sbrannen
Hi @mufasa1,
Sorry for the belated reply. This one fell through the cracks.
I tried your updated example you provided in https://github.com/spring-projects/spring-framework/issues/30972#issuecomment-1759080835.
However, I still get the following error whenever I attempt to run ServiceCTest
-- both within the IDE and via mvn clean test
from the command line.
From the command line, I tried with Java 8, 11, 14, 17, and 21.
Caused by: java.lang.RuntimeException: Can't start redis server. Check logs for details.
at redis.embedded.AbstractRedisInstance.awaitRedisServerReady(AbstractRedisInstance.java:61) ~[embedded-redis-0.6.jar:na]
at redis.embedded.AbstractRedisInstance.start(AbstractRedisInstance.java:39) ~[embedded-redis-0.6.jar:na]
at redis.embedded.RedisServer.start(RedisServer.java:9) ~[embedded-redis-0.6.jar:na]
at com.example.junit5demo.service.MockRedisServer.startMockRedisServer(MockRedisServer.java:26) ~[test-classes/:na]
In light of that, I am closing this issue.
However, as previously mentioned...
If you can provide an updated version of your sample application that starts and connects to the embedded RedisServer
(without any modification), I would be willing to take another look.
Comment From: kajh
We are also experiencing this issue. I would like to attempt creating a demo project to reproduce it. If successful, I will share it here.
It appears that the number of tests failing with this error message is correlated with the number of available cores. I wanted to share this information in case it proves useful for further investigation.
edit: After posting this, I see that you already have stated the same in another way in the following:
one kindly remind is that, it seems that the first case for each thread will occurs the exception, for example: junit open 50 thread to run tests, the exception will occurs when each thread running the first test case.
Comment From: mufasa1
We are also experiencing this issue. I would like to attempt creating a demo project to reproduce it. If successful, I will share it here.
It appears that the number of tests failing with this error message is correlated with the number of available cores. I wanted to share this information in case it proves useful for further investigation.
edit: After posting this, I see that you already have stated the same in another way in the following:
one kindly remind is that, it seems that the first case for each thread will occurs the exception, for example: junit open 50 thread to run tests, the exception will occurs when each thread running the first test case.
Hi, do you have any further solutions, bro?
Comment From: Tradorini
I'm also facing this issue
Comment From: Cenovis
Same here. Unfortunatelly it seams we cannot limit the number of methods called in parallel with junit5 :(
Comment From: sgrimm
https://github.com/spring-projects/spring-framework/issues/33383 was the root cause of this in my application, in case it's useful information for anyone here.