嵌套交易理论上可用,但未使用

时间:2015-01-28 13:02:09

标签: java hibernate postgresql spring-data-jpa

我正在尝试阻止在多个线程中的多个服务器节点上运行的导入服务中创建多个对象。我这样做的唯一机会似乎是在数据库上使用一个独特的约束,这是唯一的一点,一切都在一起。

我的服务现在尝试创建对象,并接收DataIntegrityViolationException。之后,应查询并使用已由另一个进程创建的对象。到目前为止,非常好。

但Postgres拒绝查询此对象,错误消息为SQL state [25P02]; error code [0],意味着当前事务已中断。

简而言之,我的代码如下:

@Transactional
public void connectToCollections(Product product) {
    for (Collection coll : product.getCollections()) {
        Set<CollectionMaster> collCandidates = new HashSet<CollectionMaster>();
        CollectionMaster oldMaster = coll.getMaster();

        newMaster = collectionMasterRepository.findByTitleAndSubtitle(product, coll);

        if (newMaster == null) {
            newMaster = createNewMaster(product,coll);
            if(newMaster == null) {
                // Maybe already created by another process; search again!
                newMaster = collectionMasterRepository.findByTitleAndSubtitle(product, coll);
            }
        }

        // Do something with newMaster
    }
}

private CollectionMaster createNewMaster(Product product, Collection coll) {
    CollectionMaster oldMaster = coll.getMaster();
    CollectionMaster newMaster = null;

    oldMaster.setCreatedBy(product.getCreatedBy());
    oldMaster.setLastModifiedBy(product.getCreatedBy());

    try {
        newMaster = create(oldMaster);
    }
    catch(DataIntegrityViolationException e) {
        return null;
    }
    coll.setMaster(newMaster);

    return newMaster;
}


@Transactional(propagation=Propagation.NESTED)
public CollectionMaster create(CollectionMaster collMaster) {
    CollectionMaster result = null;
    result = collectionMasterRepository.saveAndFlush(collMaster);
    return result;
}

入口点是connectToCollections。查询CollectionMaster,如果没有找到,则尝试创建,如果不成功,则再次查询。 似乎忽略了传播为NESTED(或REQUIRES_NEW)的@Transactional属性,并且没有设置或使用数据库上的保存点,通常使用的技术堆栈应该能够这样做。我错过了什么?

pom.xml的相关部分如下所示:

<hibernate.version>4.1.7.Final</hibernate.version>
<postgresql.version>9.3-1102-jdbc41</postgresql.version>
<commons-dbcp2.version>2.0.1</commons-dbcp2.version>
<spring.version>3.2.9.RELEASE</spring.version>
<spring-data-jpa.version>1.6.0.RELEASE</spring-data-jpa.version>
<aspectj.version>1.8.0</aspectj.version>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>${spring-data-jpa.version}</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
    <version>${commons-dbcp2.version}</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>${hibernate.version}</version>
    <exclusions>
        <exclusion>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
        </exclusion>
        <exclusion>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </exclusion>
        <exclusion>
            <groupId>asm</groupId>
            <artifactId>asm</artifactId>
        </exclusion>
        <exclusion>
            <groupId>asm</groupId>
            <artifactId>asm-attrs</artifactId>
        </exclusion>
        <exclusion>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>${hibernate.version}</version>
    <exclusions>
        <exclusion>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
        </exclusion>
        <exclusion>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>javax.transaction</groupId>
    <artifactId>transaction-api</artifactId>
    <version>1.1</version>
</dependency>
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>${postgresql.version}</version>
</dependency>

最后是Spring应用程序上下文的相关部分:

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${database.driverClassName}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" />
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="database" value="${database.dialect}" />
        </bean>
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="javax.persistence.validation.factory" value-ref="validator" />
            <entry key="hibernate.show_sql" value="${hibernate.show_sql}"/>
            <entry key="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl}"/>
            <entry key="org.hibernate.envers.track_entities_changed_in_revision" value="true"/>
        </map>
    </property>
    <property name="persistenceUnitName" value="mvb.vlx.model" />
    <property name="persistenceUnitManager" ref="persistenceUnitManager" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="nestedTransactionAllowed" value="true"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj" />

Java版本是jdk1.7.0_40

1 个答案:

答案 0 :(得分:3)

免责声明:我从未使用过Postgres。数据库供应商和驱动程序之间的嵌套事务支持可能会有很大差异。

但是,我认为问题可能是这样的:

try {
    ...
} catch(DataIntegrityViolationException e) {
    return null;
}

在创建失败时,数据库将事务标记为失败,但您在Java中忽略了这一点。所以我怀疑当你尝试提交它时会失败。

我认为您的选择是失败并重试整个块(connectToCollections()),或者在新的/嵌套事务中执行createNewMaster()。

如果你使用后者,请记住@Transactional不适用于私有方法,因为代理的创建方式。你仍然可以通过编程方式完成,Spring的TransactionTemplate和RetryTemplate是很好的工具。

另外我不确定我是否理解正确,但是如果你的意思是把'NESTED'放在connectToCollections()上,那么这当然没有区别(你想要嵌套createNewMaster())。 / p>