我正在尝试使用@ Transactional
within @Async
方法追踪文件转换过程如下:
@Async
@Transactional
public void convertFile(String documentId) {
CustomLog customLog = new CustomLog();
customLog.setStatus("IN_PROGRESS");
mySpringDataRepo.save(customLog);
try {
doConvertFunction(documentId);
} catch (Exception e) {
customLog.setStatus("FAIL");
mySpringDataRepo.save(customLog);
return;
}
customLog.setStatus("SUCCESS");
mySpringDataRepo.save(customLog);
}
我正在使用以下技术:
- Spring Boot 2.7.18
- Hibernate Core 5.6.15.Final
- Websphere ND 9.0.5.13
- Java 1.8
我的客户实体组合:
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
DataSource dataSource) {
LocalContainerEntityManagerFactoryBean em = builder.dataSource(dataSource).packages("com.myapp").persistenceUnit("MyEntityManagerFactory").properties(jpaProperties()).build();
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
return em;
}
protected Map<String, Object> jpaProperties() {
Map<String, Object> props = new HashMap<>();
props.put("hibernate.physical_naming_strategy", CamelCaseToUnderscoresNamingStrategy.class.getName());
props.put("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class.getName());
return props;
}
当操作上述《汤姆斯法典》时 9 它完全是罚款的,但在试图在网上进行交易时,交易是公开的,储蓄方法根本就没有执行!
I made several tries with no luck as follows:
使用
@ Transactional(propagation = Propagation。 REQUIRES_NEW
将储蓄方法推至新的单独服务方法,具体如下:
@Service public class MyService { @Autowired private MySpringDataRepo mySpringDataRepo; @Transactional(propagation = Propagation.REQUIRES_NEW) public CustomLog save(CustomLog customLog) { mySpringDataRepo.save(customLog); return customLog; } }
UPDATE:
<>只有>解决办法在网上开展工作,即从实体管理基金会创建实体管理:
@Service
public class CustomLogService {
@Autowired
private EntityManagerFactory entityManagerFactory;
public Long save(CustomLog customLog) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(customLog);
entityManager.flush();
entityManager.getTransaction().commit();
return customLog.getId();
}
public CustomLog find(Long id) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
CustomLog customLog = entityManager.find(CustomLog.class, id);
entityManager.getTransaction().commit();
return customLog;
}
public void update(CustomLog customLog) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.merge(customLog);
entityManager.getTransaction().commit();
}
}
更改我的<代码>@Async 守则如下:
@Async
public void convertFile(String documentId) {
CustomLog customLog = new CustomLog();
customLog.setStatus("IN_PROGRESS");
customLogService.save(customLog);
try {
doConvertFunction(documentId);
} catch (Exception e) {
customLog = customLogService.find(customLog.getId());
customLog.setStatus("FAIL");
customLogService.update(customLog);
return;
}
customLog = customLogService.find(customLog.getId());
customLog.setStatus("SUCCESS");
customLogService.update(customLog);
}
我试图注入实体 经理,但只有创建实体才能工作。 实体经理:
@PersistenceContext(name = "MyEntityManagerFactory")
private EntityManager entityManager;
Are there any disadvantages/things to consider in this solution because I will be heavily using this method and it returns a new EntityManager instance each time it is invoked?