【问题标题】:Springboot @Transcational is not getting rolledbackSpring Boot @Transcational 没有回滚
【发布时间】:2020-06-29 08:26:46
【问题描述】:

我是 springboot 新手,一直在做一个项目。我将 Springboot 与 hibernate、JPA 和 hikariCP 一起用于 mysql 数据库操作。目前我有两个数据源,我无法回滚我在服务中编写的事务。我尝试了所有我能找到的东西,但我找不到我错过了什么。任何帮助将不胜感激并原谅我的错误,我非常愿意接受建议。谢谢。

@SpringBootApplication
public class AdminModuleApplication {
public static void main(String[] args) 
    {
        SpringApplication.run(AdminModuleApplication.class, args);
    }   
}
@Configuration
@PropertySource({ "classpath:acc_payDB_properties.properties" })
@EnableJpaRepositories(
    basePackages = "com.pinnacle.accpay.dao", 
    entityManagerFactoryRef = "accpayEntityManager", 
    transactionManagerRef = "accpayTransactionManager"
)
public class acc_payDBConfig 
{
    @Autowired
    private Environment env;

    @Bean
    public LocalContainerEntityManagerFactoryBean accpayEntityManager() 
    {
        String packageList[]= {"com.pinnacle.accpay.model","com.pinnacle.admin.model"};
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(accpayDataSource());
        em.setPackagesToScan(packageList);
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        Properties jpaProperties = new Properties();
        jpaProperties.setProperty("hibernate.hbm2ddl.auto", "none");
        Properties jpaProperties2 = new Properties();
        jpaProperties2.setProperty("connection.provider_class","org.hibernate.hikaricp.internal.HikariCPConnectionProvider");
        em.setJpaProperties(jpaProperties2);
        return em;
    }


    @Bean
    public DataSource accpayDataSource() 
    {

        HikariDataSource dataSource = new HikariDataSource(); 
        dataSource.setDriverClassName(env.getProperty("jdbc.driver-class-name"));
        dataSource.setJdbcUrl(env.getProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.username"));
        dataSource.setPassword(env.getProperty("jdbc.password"));
        dataSource.setCatalog("acc_pay");
        //HikariCP specific properties. Remove if you move to other connection pooling library.
        dataSource.setConnectionTimeout(20000);
        dataSource.setMaximumPoolSize(20);
        dataSource.setMinimumIdle(10);
        dataSource.setIdleTimeout(20000);
        dataSource.setMaxLifetime(40000);
        dataSource.setAutoCommit(false);
        dataSource.addDataSourceProperty("cachePrepStmts", true);
        dataSource.addDataSourceProperty("prepStmtCacheSize", 25000);
        dataSource.addDataSourceProperty("prepStmtCacheSqlLimit", 20048);
        dataSource.addDataSourceProperty("useServerPrepStmts", true);
        dataSource.addDataSourceProperty("initializationFailFast", true);
        dataSource.setPoolName("ACCPAY DB_HIKARICP_CONNECTION_POOL");
        dataSource.addDataSourceProperty("useLocalTransactionState", false);
        return new HikariDataSource(dataSource);

    }

    @Bean
    public PlatformTransactionManager accpayTransactionManager() 
    {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(accpayEntityManager().getObject());
        return transactionManager;
    }
}


@Service
public class TranscationReceptionService {
    @Autowired
    PO_TXN_MasterBeanRepository po_TXN_MasterBeanRepository;

    @Autowired
    GRN_TXN_MasterBeanRepository grn_TXN_MasterBeanRepository;

    @Autowired
    Invoice_TXN_MasterBeanRepository invoice_TXN_MasterBeanRepository;

    @Autowired
    POEventLogRepository poEventLogRepository;

    @Autowired
    InvoiceEventLogRepository invoiceEventLogRepository;

    @Autowired
    GRNEventlogRepository grnEventlogRepository;

    @Autowired
    PO_GRN_INVBeanRepository po_GRN_INVBeanRepository;

    @Autowired
    LinkEventLogBeanRepository linkEventLogBeanRepository;

    @Autowired
    ScheduledJob scheudledJob;

    @Autowired
    acc_payDBConfig acc_paydbConfig;

    @Value("${in_process_status}")
    private String in_process_status;


    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateMatchStatus(Long poId, Long invoiceId, String poStatus, String invoiceStatus)
    {
        try
        {

        po_TXN_MasterBeanRepository.setPOStatusAfterMatching(poStatus, poId, invoiceId);

        POEventLogBean poEventLogBean = new POEventLogBean();
        poEventLogBean.setPo_id(poId); poEventLogBean.setEvent_time(LocalDateTime.now().toString());
        poEventLogBean.setStatus(poStatus); 
        poEventLogRepository.save(poEventLogBean);

        po_GRN_INVBeanRepository.setInvoiceNumber(poId, invoiceId);

        Long linkId = po_GRN_INVBeanRepository.getLinkIdfromPONumber(poId);

        LinkEventLogBean linkEventLogBean=new LinkEventLogBean();
        linkEventLogBean.setLink_id(linkId); 
        linkEventLogBean.setEvent_time(LocalDateTime.now().toString());
        linkEventLogBean.setStatus("");
        linkEventLogBeanRepository.save(linkEventLogBean);

        invoice_TXN_MasterBeanRepository.setInvoiceStatusAfterMatching(poId, invoiceStatus, invoiceId);

        InvoiceEventLogBean invoiceEventLogBean=new InvoiceEventLogBean();
        invoiceEventLogBean.setInv_id(invoiceId); invoiceEventLogBean.setEvent_time(LocalDateTime.now().toString());
        invoiceEventLogBean.setStatus(invoiceStatus);
        invoiceEventLogRepository.save(invoiceEventLogBean);
        }
        catch (Exception e) 
        {

        }
    }

}

@Transactional
@Repository
public interface PO_TXN_MasterBeanRepository extends JpaRepository<PO_TXN_MasterBean, Long> 
{

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query("UPDATE PO_TXN_MasterBean p SET p.status= :status, p.inv_id= :invId WHERE p.po_id = :pId")
    public void setPOStatusAfterMatching(@Param("status") String status, @Param("pId") Long poId, @Param("invId") Long invoiceId);
}

@Transactional
@Repository
public interface POEventLogRepository extends JpaRepository<POEventLogBean, Integer> 
{

}

@Transactional
@Repository
public interface PO_GRN_INVBeanRepository extends JpaRepository<PO_GRN_INVBean, Long>
{

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query("UPDATE PO_GRN_INVBean p SET p.inv_id= :invId WHERE p.po_id = :pId")
    public void setInvoiceNumber(@Param("pId") Long poId, @Param("invId") Long invoiceId);

    @Query("SELECT  p.link_id FROM PO_GRN_INVBean p WHERE p.po_id = :pId")
    public Long getLinkIdfromPONumber(@Param("pId") Long poId);
}


@Transactional
@Repository
public interface LinkEventLogBeanRepository extends JpaRepository<LinkEventLogBean, Long>
{

}

@Repository
@Transactional
public interface Invoice_TXN_MasterBeanRepository extends JpaRepository<Invoice_TXN_MasterBean, Long> 
{
    @Modifying(clearAutomatically = true)
    @Transactional
    @Query("UPDATE Invoice_TXN_MasterBean i SET i.po_id = :pid, i.status =:status WHERE i.inv_id = :inid")
    public void setInvoiceStatusAfterMatching(@Param("pid") Long pid, @Param("status") String status, @Param("inid") Long inId);
}

@Repository
public interface InvoiceEventLogRepository extends JpaRepository<InvoiceEventLogBean, Integer>
{

}

【问题讨论】:

  • 你的意思是要回滚两个数据源上的操作?
  • 是的...但即使我故意抛出异常事务也不会回滚!!
  • 您正在寻找分布式 tx,[Spring @Transactional 与跨多个数据源的事务](stackoverflow.com/questions/48954763/…) 可能会有所帮助
  • TranscationReceptionService.updateMatchStatus() 中删除 try-catch 块。此外,除非另有说明,否则 txn 回滚会针对 RuntimeException 类型的异常发生。
  • 对不起,我的错。到目前为止,我没有在单个事务中使用 2 个数据源。即使我的应用程序中有 2 个数据源。我正在尝试在单个来源中回滚事务

标签: java hibernate spring-boot jpa hikaricp


【解决方案1】:

事务回滚未按预期工作,因为异常是使用 TranscationReceptionService.updateMatchStatus() 中的 try-catch 块处理的。这可以防止 spring 框架意识到引发的异常,因此不会导致回滚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 2018-09-15
    • 2018-11-05
    • 2020-04-28
    • 2016-12-11
    • 2020-03-11
    • 1970-01-01
    相关资源
    最近更新 更多