【问题标题】:I have put @Transactional in method still it commits the transaction before rollback我已将@Transactional 放入方法中,但它仍会在回滚之前提交事务
【发布时间】:2020-02-29 09:18:40
【问题描述】:

Spring如何实现事务管理

我的方法如下

@Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
@Override
public InwardDTO saveEntity(InwardDTO entity) throws Exception {
    try {
        costCalculation(entity);
        InwardDTO dto = super.saveEntity(entity);
        addStock(dto.getDetails(), dto.getId());
        return dto;
    } catch (Exception ex) {
        throw ex;
    }
}

private void addStock(Set<InwardDetailsDTO> argDetailsDTOSet, Long argInwardId) throws RuntimeException {
    String SUBMODULE = getModuleNameForLog() + " [addStock()] ";

    if (1 == 1) {
        throw new RuntimeException("Test Case");
    }
}

日志就像,

2020-02-29 15:01:14.210 TRACE 14504 --- [nio-8080-exec-2] o.s.t.i.TransactionInterceptor           : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
Hibernate: insert into tbl_inward_chemical (date, invoice_number, is_deleted, party_id, po_id, pre_inward_id_id, remark, slip, total_amount, total_weight) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into tbl_inward_details_chemical (inward_id, is_deleted, is_pass, party_moisture_id, party_ph_id, party_price, party_purity_id, party_solubility_id, party_weight, product_name_id, received_moisture_id, received_ph_id, received_price, received_purity_id, received_solubility_id, received_weight, total_price) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into tbl_inward_details_chemical (inward_id, is_deleted, is_pass, party_moisture_id, party_ph_id, party_price, party_purity_id, party_solubility_id, party_weight, product_name_id, received_moisture_id, received_ph_id, received_price, received_purity_id, received_solubility_id, received_weight, total_price) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2020-02-29 15:01:14.216 TRACE 14504 --- [nio-8080-exec-2] o.s.t.i.TransactionInterceptor           : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
2020-02-29 15:01:14.216 TRACE 14504 --- [nio-8080-exec-2] o.s.t.i.TransactionInterceptor           : Completing transaction for [com.alignedorg.chemical.inward.service.InwardService.saveEntity] after exception: java.lang.RuntimeException: Test Case
2020-02-29 15:01:14.216 TRACE 14504 --- [nio-8080-exec-2] o.s.t.i.RuleBasedTransactionAttribute    : Applying rules to determine whether transaction should rollback on java.lang.RuntimeException: Test Case
2020-02-29 15:01:14.216 TRACE 14504 --- [nio-8080-exec-2] o.s.t.i.RuleBasedTransactionAttribute    : Winning rollback rule is: RollbackRuleAttribute with pattern [java.lang.RuntimeException]
2020-02-29 15:01:14.222 ERROR 14504 --- [nio-8080-exec-2] c.a.core.utillity.log.ApplicationLogger  :  [ Inward Controller ]  [SAVE] Test Case

java.lang.RuntimeException: Test Case

在此事务中始终在 addStock 方法回滚之前提交... 在日志中显示事务正在回滚,但条目保存在数据库中......

【问题讨论】:

  • 我没有看到任何@Transactional。你能粘贴更多代码吗?
  • 删除 catch try-catch 块。此外,您正在 rollbackFor = Exception.class 并抛出 RuntimeException。
  • @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) @Override public InwardDTO saveEntity(InwardDTO entity) throws Exception { try { costCalculation(entity); InwardDTO dto = super.saveEntity(entity); addStock(dto.getDetails(), dto.getId());返回 dto; } catch (Exception ex) { throw ex; } }
  • 仍然无法工作@dassum
  • 分享修改后的代码也将propagation = Propagation.SUPPORTS改为propagation = Propagation.REQUIRED

标签: spring hibernate spring-boot jpa


【解决方案1】:

Spring 事务 bean 所需列表:

1.Jdbc模板

@Bean(name = "jdbcTemplate")
public JdbcTemplate creatJdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {
    return new JdbcTemplate(dataSource);
}

2.数据源

@Bean(name = "dataSource")
public DataSource creatDataSource() {
    DriverManagerDataSource ds = new DriverManagerDataSource();
    ds.setDriverClassName(driverClassName);
    ds.setUrl(url);
    ds.setUsername(username);
    ds.setPassword(password);
    return ds;
}

3.事务管理器

@Bean(name = "transactionManager")
public PlatformTransactionManager creatTransactionManager(DataSource  dataSource) {
    return new DataSourceTransactionManager(dataSource);
}

4.@EnableTransactionManagement以上一个@Configuration Class激活TxManager

5.使用DaoImplements中的JdbcTemplate的实例(@Autowired),用query()执行sql || update()方法

【讨论】:

    【解决方案2】:

    如果表存储引擎是MyISAM:这个引擎不支持事务。正确的应该是InnoDB

    如果您使用 Hibernate 自动创建表:我建议您编写自己的迁移并使用像 Flyway 这样的工具来管理这些。这需要更多的工作,但您可以完全控制表格的外观。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多