问题描述
我有以下代码将两个不同的entities
保留到我的MysqL
数据库中。
这可以按预期工作,但是,如果一个表存在问题,而不是另一个表存在问题,则将填充一个表,而另一个则不会。
注意-我正在以EAR
服务器内的jboss EAP
文件的形式运行应用程序。
我想确保同时填充两个表或无。
我该怎么办?
Persistence.xml
<persistence-unit name="entitystore" transaction-type="JTA">
<jta-data-source>java:/jdbc/datasources/global</jta-data-source>
Java服务类:
public void createCompanyStatuses(String client,CompanyStatusPostDTO companyStatusPostDTO) {
EntityManager entityManager = null;
try {
CompanyStatus companyStatus = new CompanyStatus();
companyStatus.setCompanyLabel(candidateMaskedStatusPostDTO.getCompanyLabel());
entityManager = entityManagement.createEntityManager(client);
entityManager.persist(companyStatus);
for(Integer employeeStatusId: companyStatusPostDTO.getEmployeeStatuses()){
CompanyStatusEmployeeStatus companyStatusEmployeeStatus = new CompanyStatusEmployeeStatus();
companyStatusEmployeeStatus.setEmployeeId(employeeStatusId);
companyStatusEmployeeStatus.setCompanyId(companyStatus.getCompanyId()); //todo - how will get this?
entityManager.persist(CompanyStatusEmployeeStatus);
}
} catch(Exception e){
log.error("An exception has occurred in inserting data into the table" + e.getMessage(),e);
} finally {
entityManagement.closeEntityManager(client,entityManager);
}
}
编辑:
我尝试添加:
@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
但是,问题仍然存在,成功的人坚持工作,不成功的人坚持工作,而不是全部或全部坚持下去。
解决方法
仅使用交易即可。
在spring上使用@Transactional注释。
没有spring框架,您可以做
else if (r>=80 && r<=84)
请参阅:https://vladmihalcea.com/high-performance-java-persistence-github-repository/