gpt4 book ai didi

java - Hibernateflush()影响事务

转载 作者:行者123 更新时间:2023-12-01 17:51:38 24 4
gpt4 key购买 nike

我有以下测试用例:

@Test
fun givenEntity_whenSaved_thenFound() {
doInHibernate<Unit>(({ this.sessionFactory() }), { session ->
val entity = getSampleEntity()

session.persist(entity)
session.flush()

val entityFound = session.find(Entity::class.java, entity.id)

session.refresh(entityFound )
assertTrue(entityFound == entity)

session.transaction.rollback()
})
}

所以我认为 session.flush() 不会影响我的事务,以便它可以回滚。但是它确实会影响它,我收到以下错误:

Transaction not successfully started
java.lang.IllegalStateException: Transaction not successfully started
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:95)
at org.hibernate.testing.transaction.TransactionUtil.doInHibernate(TransactionUtil.java:301)
at data.base.EntityTest.givenEntity_whenSaved_thenFound(EntityTest.kt:20)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:45)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:288)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:282)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.lang.Thread.run(Thread.java:834)

针对这种特定情况的解决方法是这样,但这仍然无法解释为什么交易受到影响。

doInHibernate<Unit>(({ this.sessionFactory() }), { session ->
val entity = getSampleEntity()

session.persist(entity)
assertTrue(session.contains(entity))

})

最佳答案

所以我对此不确定,但我认为我的问题是我启用了自动提交。所以一个可能的解决办法是:

hibernate.connection.autommit=false

关于java - Hibernateflush()影响事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60785483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com