嵌套事务理论上可用,但未使用
Nested Transactions theoretically available, but not used
我试图阻止在多个线程中的多个服务器节点上运行的导入服务中创建多个对象。我这样做的唯一机会似乎是在数据库上使用唯一约束,这是唯一的单点,所有内容再次聚集在一起。
我的服务现在尝试创建对象,并收到 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
免责声明:我从未使用过 Postgres。嵌套事务支持在数据库供应商和驱动程序之间可能有很大差异。
但是,我认为问题可能是这样的:
try {
...
} catch(DataIntegrityViolationException e) {
return null;
}
在创建失败时,数据库将事务标记为失败,但您在 Java 中忽略了这一点。所以我怀疑稍后当你尝试提交它时它会失败。
我认为您的选择是要么失败并重试整个块 (connectToCollections()),要么在 new/nested 事务中执行 createNewMaster()。
如果您选择后者,请记住@Transactional 不适用于私有方法,因为代理的创建方式。您仍然可以通过编程方式执行此操作,Spring 的 TransactionTemplate 和 RetryTemplate 是这方面的绝佳工具。
另外我不确定我是否理解正确,但如果你的意思是将 'NESTED' 放在 connectToCollections() 上,那当然不会有什么不同(你想嵌套 createNewMaster( )).
我试图阻止在多个线程中的多个服务器节点上运行的导入服务中创建多个对象。我这样做的唯一机会似乎是在数据库上使用唯一约束,这是唯一的单点,所有内容再次聚集在一起。
我的服务现在尝试创建对象,并收到 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
免责声明:我从未使用过 Postgres。嵌套事务支持在数据库供应商和驱动程序之间可能有很大差异。
但是,我认为问题可能是这样的:
try {
...
} catch(DataIntegrityViolationException e) {
return null;
}
在创建失败时,数据库将事务标记为失败,但您在 Java 中忽略了这一点。所以我怀疑稍后当你尝试提交它时它会失败。
我认为您的选择是要么失败并重试整个块 (connectToCollections()),要么在 new/nested 事务中执行 createNewMaster()。
如果您选择后者,请记住@Transactional 不适用于私有方法,因为代理的创建方式。您仍然可以通过编程方式执行此操作,Spring 的 TransactionTemplate 和 RetryTemplate 是这方面的绝佳工具。
另外我不确定我是否理解正确,但如果你的意思是将 'NESTED' 放在 connectToCollections() 上,那当然不会有什么不同(你想嵌套 createNewMaster( )).