休眠|为什么交易“半承诺"? [英] Hibernate | Why is the transaction "half-committed"
问题描述
我正在Spring Boot项目中使用Hibernate.
I'm working with Hibernate in a Spring Boot project.
我有以下代码:
public class SomeService {
private Dependency dependency;
@Transactional(readOnly=false)
public void doSomething() {
//some BL code...
dependency.doSomeDbManipualation();
someOperation();
}
public void someOperation() {
//some code that eventually fails
}
}
public class Dependency {
public void doSomeDbManipulation() {
Entity entity = ...; //get the entity from current session by its key
if (entity != null) {
session.delete(entity);
}
OtherEntity oEntity = new OtherEntity();
//set its fields
Long oEntityId = session.save(oEntity);
entity = new Entity();
entity.setForeignKey(oEntityId);
//set other fields
session.persist(entity);
}
}
现在,我在数据库中有了一个带有相关密钥的实体.因此,我希望在调用服务时,查找实体的代码确实可以找到它.但是由于someOperation()
失败,所以我希望数据库中没有任何变化.
Now, I have in the database an entity with the relevant key. So I expect that when calling the service, the code that looks for the entity will indeed find it. But since someOperation()
fails, I expect to see no change in the DB.
实际上,在调用someService.doSomething()
之后(失败),我查看了数据库,发现现有实体已删除!但是没有创建一个新的实体(没关系).
In fact, after calling someService.doSomething()
(and failing), I look in the DB and I see that the existing entity was deleted! But a new entity was not created (which is ok).
为什么此交易半数完成"?
Why is this transaction "half committed"?
显然delete()和save()会立即提交.调试时,我看到在代码中的这一行完成后立即删除了该实体.另外,OtherEntity
也将立即添加到数据库中.
persist()不会立即提交.
Apparently delete() and save() are committed immediately. When I debug, I see that entity is immediately deleted after this line in code is done. Also OtherEntity
is added immediately to the DB.
persist() is not committed immediately.
我将AspectJ用于交易管理.这是我的pom.xml的相关部分:
I use AspectJ for transaction management. Here is the relevant part from my pom.xml:
<project>
...
<dependencies>
...
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.3</version>
</dependency>
</dependencies>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<outxml>true</outxml>
<forceAjcCompile>true</forceAjcCompile>
<source>1.8</source>
<target>1.8</target>
<Xlint>ignore</Xlint>
<complianceLevel>1.8</complianceLevel>
<encoding>UTF-8</encoding>
<verbose>true</verbose>
<preserveAllLocals>true</preserveAllLocals>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<id>AspectJ-Compile</id>
<phase>process-classes</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
</project>
推荐答案
我找到了解决该问题的方法. 我在几个类中有多个@ComponentScan批注.当我将它们全部删除,只剩下其中一个时,一切都按预期运行.
I found the solution to the issue. I had multiple @ComponentScan annotations, in several classes. When I removed them all, and left just one, all was working as expected.
我不明白这种奇怪行为的原因,所以我对此发布了一个单独的问题,
I don't understand the reason to this strange behavior, so I posted a separate question about this, here
这篇关于休眠|为什么交易“半承诺"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!