[英]Hibernate flush() affects transaction
I have the following Test-Case:我有以下测试用例:
@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()
})
}
So I thought that the session.flush()
would not affect my transaction so that it could rollback.所以我认为
session.flush()
不会影响我的事务,以便它可以回滚。 However it does affect it, I get the following error:但是它确实影响它,我收到以下错误:
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)
A workaround for this specific case would be this, but that still does not explain why the transaction is affected.这种特定情况的解决方法是这样,但这仍然不能解释为什么交易会受到影响。
doInHibernate<Unit>(({ this.sessionFactory() }), { session ->
val entity = getSampleEntity()
session.persist(entity)
assertTrue(session.contains(entity))
})
So I'm not sure about that, but I think my problem was that I had Autocommit enabled.所以我不确定,但我认为我的问题是我启用了自动提交。 So a possible fix would be:
所以一个可能的解决方法是:
hibernate.connection.autommit=false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.