• sharding JDBC 不支持批量导入解决方法


    package com.ydmes.service.impl.log;

    import com.ydmes.domain.entity.log.BarTraceBackLog;
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    import org.springframework.stereotype.Component;
    import org.springframework.transaction.TransactionDefinition;
    import org.springframework.transaction.TransactionStatus;
    import org.springframework.transaction.support.DefaultTransactionDefinition;

    import java.util.List;

    @Component
    public class BarTraceBackLogBatchServiceImpl implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public void batchInsertBarTraceBackLogs(List<BarTraceBackLog> barTraceBackLogs) {
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    definition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);

    DataSourceTransactionManager transactionManager = (DataSourceTransactionManager)
    applicationContext.getBean("shardingTransactitonManager");
    TransactionStatus transactionStatus = transactionManager.getTransaction(definition);

    for (BarTraceBackLog barTraceBackLog : barTraceBackLogs) {
    //UserDao有create方法,单条插入
    applicationContext.getBean(BarTraceBackLogServiceImpl.class).insertSelective(barTraceBackLog);
    }
    transactionManager.commit(transactionStatus);
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
    }
    }

  • 相关阅读:
    安卓逆向前置之JAVA学习
    @Controller VS @RestController @RequestBody VS @ResponseBody
    【ArangoDb踩坑】ArangoDb中的大数比较
    我的快排
    记录改造ffmpeg遇到的依赖库问题
    centos7 配置阿里yum源
    记录一个解决GLIBC_2.18 not found的问题
    js 格式化时间 供页面使用
    5G PDU session Establishment
    DPDK performance for USER application
  • 原文地址:https://www.cnblogs.com/xyj179/p/11454562.html
Copyright © 2020-2023  润新知