admin管理员组文章数量:1200978
I have code like this:
@Transactional
@RestController
public class TestController {
@Transactional(propagate = NEVER)
public void testTransactionLeak(){
repository.findAll();
Thread.sleep(40_000);
}
public void otherCrudEndpoint(){
repository.doSomething();
}
}
When calling the testTransactionLeak() method, hikariCP is complaining about a connection leak after 30 seconds. I dont understand this as i would have expected that propagate = NEVER is telling spring to not create a transaction in this case and this would mean there is nothing to leak.
PS: If i remove all the transactional annotations, it works as expected, but I prefer to have an @Transactional annotation on the controller as i want to have the behaviour that any error will rollback all the database changes (CRUD app) and i dont want that devs forget to set the annoation themselves
I have code like this:
@Transactional
@RestController
public class TestController {
@Transactional(propagate = NEVER)
public void testTransactionLeak(){
repository.findAll();
Thread.sleep(40_000);
}
public void otherCrudEndpoint(){
repository.doSomething();
}
}
When calling the testTransactionLeak() method, hikariCP is complaining about a connection leak after 30 seconds. I dont understand this as i would have expected that propagate = NEVER is telling spring to not create a transaction in this case and this would mean there is nothing to leak.
PS: If i remove all the transactional annotations, it works as expected, but I prefer to have an @Transactional annotation on the controller as i want to have the behaviour that any error will rollback all the database changes (CRUD app) and i dont want that devs forget to set the annoation themselves
Share Improve this question asked Jan 22 at 10:07 KP_EVKP_EV 33 bronze badges 3 |1 Answer
Reset to default 0Based on this article: https://medium.com/@dulanjayasandaruwan1998/understanding-transactional-propagation-in-spring-boot-ec958521794d
Also based on the spring documentation, this method should always throw an exception if there is currently a transaction executing.
Assuming you have open-in-view=false
for spring.jpa
in your properties, you probably want NOT_SUPPORTED
instead of NEVER
NOT_SUPPORTED executes the method without a transaction. If a transaction is present, it will be suspended during the method execution.
Use NOT_SUPPORTED when you want to ensure that a method does not run within a transaction context, such as for non-transactional operations like reading configuration data .
本文标签: hibernatepropagateNEVER still creates a transaction in springStack Overflow
版权声明:本文标题:hibernate - propagate = NEVER still creates a transaction in spring - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738572523a2100683.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
spring.jpa.open-in-view=false
as that would quite eagerly open a connection before anything else. – M. Deinum Commented Jan 22 at 12:37