gpt4 book ai didi

Spring & Hibernate - 使 native 查询与@Transactional 在同一事务中运行

转载 作者:行者123 更新时间:2023-12-01 14:38:38 25 4
gpt4 key购买 nike

是否可以创建使用通过@Transactional 创建的现有事务的 native 查询?这里的大多数问题似乎都与进行 native 查询有关。此外,答案如来自 Spring + Hibernate Transaction -- Native SQL rollback failure 的答案建议这可能是不可能的。

我测试隔离的方法是运行一些删除并添加一个断点来调查数据库。

到目前为止,这是我尝试过的:

@Transactional(value = "someManager")
public class SpringJpaSomeDao implements SomeDao {

@PersistenceContext(unitName = "someUnit")
@Qualifier("entityManagerFactorySome")
private EntityManager em;


@Resource
@Qualifier("someManager")
private PlatformTransactionManager transactionManager;

@Override
@Transactional(value = "someManager", propagation = Propagation.SUPPORTS)
public void runNative(String sql){
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {

@Override

protected void doInTransactionWithoutResult(TransactionStatus status) {

em.createNativeQuery(sql).executeUpdate();

}

});





}

persistence.xml 的一部分

<persistence-unit name="someUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:comp/env/jdbc/someDS</non-jta-data-source>
<!-- some classes here -->
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- some dialect -->
</properties>
</persistence-unit>

代码是从一些 Controller 调用的,该 Controller 也有 @Transactional 批注,将删除传递给 Dao。

@Transactional(value = "someManager", propagation = Propagation.SUPPORTS)
public void deleteEntireDatabase() {

List<String> deletions = new ArrayList<>();
deletions.add("DELETE FROM something;");


for (String currentDeletion : deletions) {
someDao.runNative(currentDeletion);
}


}

@Override
@Transactional(value = "someManager", propagation = Propagation.REQUIRES_NEW, rollbackFor = {Exception.class})
public void deleteAndFill(JobExecutionProgress progress) {
deleteEntireDatabase();
// more code
}

摘自spring-dao.xml:

<tx:annotation-driven  />

<bean id="entityManagerFactorySome" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="someDataSource" />
<property name="persistenceUnitName" value="someUnit" />
<property name="jpaProperties">
<props>
<!-- some dialect -->
</props>
</property>
</bean>

<bean id="someDao" class="xyz.model.controller.SpringJpaSomeDao"/>

<bean id="someManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactorySome" />
<qualifier value="someManager"></qualifier>
</bean>


<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

当然,我也尝试了一些变体,比如不同的Propagations,以及使用从别处获取的entityManager:

    EntityManagerFactory emf = ((JpaTransactionManager) transactionManager).getEntityManagerFactory();

EntityManager em2 = EntityManagerFactoryUtils.getTransactionalEntityManager(emf);

现在,如果其他一切都失败了,我会做的是手动事务管理。

这是否适用于您的某个应用程序,还是不知道这是否可能是设置问题?

最佳答案

实际上,事实证明评论中提到的 alter table 语句似乎是问题所在。不确定如何重置 auto_increment 可以清空表,但情况似乎是这样。

关于Spring & Hibernate - 使 native 查询与@Transactional 在同一事务中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42293354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com