代码之家  ›  专栏  ›  技术社区  ›  Dikla

Hibernate |为什么事务“半提交”

  •  0
  • Dikla  · 技术社区  · 8 年前

    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进行事务管理。以下是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>
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   whysoseriousson    8 年前

    我不知道为什么aspect j在pom文件中被显式地添加为依赖项。如果使用spring boot,就可以使aspect开箱即用。当类路径中存在重复jar时,我看到了不可预知的行为。

    我也不明白为什么DB操纵方法是从一个本身被标记为只读事务的方法调用的。

    我觉得这里的设计不对。

        2
  •  0
  •   Dikla    8 年前

    我找到了解决这个问题的办法。

    我不明白这种奇怪的行为的原因,所以我贴了一个单独的问题, here