簡體   English   中英

休眠| 為什么交易“半承諾”

[英]Hibernate | Why is the transaction “half-committed”

我正在Spring Boot項目中與Hibernate合作。

我有以下代碼:

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()失敗,所以我希望數據庫中沒有任何變化。

實際上,在調用someService.doSomething() (並失敗)之后,我查看了數據庫,發現現有實體已刪除! 但是沒有創建新的實體(可以)。

為什么此交易是“半數承諾”?

編輯:顯然delete()和save()會立即提交。 調試時,我看到在代碼中的這一行完成后立即刪除了該實體。 另外, OtherEntity也將立即添加到數據庫中。 persist()不會立即提交。

我使用AspectJ進行事務管理。 這是我的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>

我不知道為什么將方面j顯式添加為pom文件中的依賴項。如果使用spring boot進行安裝,您會發現方面的問題。當類路徑中存在重復的jar時,我看到了無法預料的行為。

我也無法理解為什么從本身被標記為只讀事務的方法中調用數據庫操作方法。

我覺得這里的設計不正確。

我找到了解決問題的方法。 在幾個類中,我有多個@ComponentScan批注。 當我將它們全部刪除,只剩下其中一個時,它們都按預期工作。

我不明白這種奇怪行為的原因,因此我在此處發布了一個與此相關的單獨問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM