无法将csv批量加载到mysql中:javax.persistence.TransactionRequiredException:执行更新/删除查询

时间:2019-08-27 13:11:58

标签: mysql hibernate spring-boot spring-data-jpa

我无法从Spring执行“ LOAD DATA LOCAL INFILE”查询。 我在各处都添加了@ org.springframework.transaction.annotation.Transactional,仍然有同样的问题。 当我在mysql上执行查询以及在单个DB上执行查询时,查询效果最佳。 现在我正在使用多个数据库。 我要在其中执行查询的主要数据库是mydb DB

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "mydbEntityManagerFactory",
    basePackages = {"com.job.sync.job.ptoz.mydbdb.dao"},
    transactionManagerRef = "mydbTransactionManager")
public class MydbDbConfig {



  @Bean(name = "mydbDataSource")
  @ConfigurationProperties(prefix = "spring.mydb.datasource")
  public DataSource dataSource() {
    return DataSourceBuilder.create().build();
  }


  @Bean(name = "mydbEntityManagerFactory")
  public LocalContainerEntityManagerFactoryBean entityManagerFactory(
      EntityManagerFactoryBuilder builder, @Qualifier("mydbDataSource") DataSource dataSource) {
    return builder.dataSource(dataSource).packages("com.job.sync.job.ptoz.mydbdb.entities").

        build();
  }

  @Primary
  @Bean(name = "mydbTransactionManager")
  public PlatformTransactionManager transactionManager(
      @Qualifier("mydbEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
    return new JpaTransactionManager(entityManagerFactory);
  }

}

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "fibertoolSyncEntityManagerFactory",
    basePackages = {"com.job.sync.job.ptoz.fibertoolsyncdb.dao"},
    transactionManagerRef = "fibertoolSyncTransactionManager")
public class FibertToolDbConfig {

  @Primary
  @Bean(name = "fibertoolSyncDataSource")
  @ConfigurationProperties(prefix = "spring.fibertoolsync.datasource")
  public DataSource dataSource() {
    return DataSourceBuilder.create().build();
  }

  @Primary
  @Bean(name = "fibertoolSyncEntityManagerFactory")
  public LocalContainerEntityManagerFactoryBean entityManagerFactory(
      EntityManagerFactoryBuilder builder,
      @Qualifier("fibertoolSyncDataSource") DataSource dataSource) {
    return builder.dataSource(dataSource).packages("com.job.sync.job.ptoz.fibertoolsyncdb.entities")
        .

        build();
  }


  @Bean(name = "fibertoolSyncTransactionManager")
  public PlatformTransactionManager transactionManager(
      @Qualifier("fibertoolSyncEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
    return new JpaTransactionManager(entityManagerFactory);
  }


}
public interface TempoLoadPtoCustom {



  public void bulkLoadData(String inputDirectory);

  public void callProcedureLoadPtoZmd();

}
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.StoredProcedureQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Transactional
public class TempoLoadPtoCustomImpl implements TempoLoadPtoCustom {


  private static final Logger logger = LoggerFactory.getLogger(TempoLoadPtoCustomImpl.class);

  @PersistenceContext
  private EntityManager em;


  public void setEntityManager(EntityManager em) {
    this.em = em;
  }


  @Modifying
  @Transactional(rollbackFor = Exception.class)
  @Override
  public void bulkLoadData(String inputDirectory) {

    logger.info("path before executing the query:  " + inputDirectory);

    String query = "LOAD DATA LOCAL INFILE '" + inputDirectory
        + "' INTO TABLE tempo_load_pto FIELDS TERMINATED BY ';' LINES TERMINATED BY '\\n' IGNORE 1 LINES;";

    logger.info("Execution of the query  :" + query);


    Query q = em.createNativeQuery(query);
    int rows = q.executeUpdate();

    if (rows > 1) {

      logger.info("Data has been loaded successfuly!!");

    } else {

      logger.warn("0 row inserted !!");
    }


  }

  @Override
  public void callProcedureLoadPtoZmd() {


    StoredProcedureQuery query = em.createStoredProcedureQuery("load_pto_zmd");
    query.execute();

  }



}
@Repository
@Transactional
public interface TempoLoadPtoDAO extends JpaRepository<TempoLoadPto, String>, TempoLoadPtoCustom {


  @Modifying
  @Query(value = "truncate table tempo_load_pto", nativeQuery = true)
  public void truncateTempoLoadPtoTable();



}
@Service
public class PTOZ {

  private static final Logger logger = LoggerFactory.getLogger(PTOZ.class);


  @Autowired
  TempoLoadPtoService tempoLoadPmtzService;

  @Autowired
  TempoLoadPtoDAO tempoLoadPmtzDAO;

@Transactional
  public void csvProcessing(File csv, String inputDirectory) {
      String csvName = csv.getName().replace(".csv", "");
      String csvNameAfterTalend = csvName + "_control";
      String csvNameAfterTalendCsv = csvName + "_control.csv";

      String inputDirectoryAfterTalend = inputDirectory.replace(csvName, csvNameAfterTalend);

 tempoLoadPmtzService.csvBulkInsertService(inputDirectoryAfterTalend);

}




}


以下是日志:

org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:402)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:144)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$ExposeRepositoryInvocationInterceptor.invoke(CrudMethodMetadataPostProcessor.java:364)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy104.bulkLoadData(Unknown Source)
    at com.job.sync.job.ptoz.service.TempoLoadPtoService.csvBulkInsertService(TempoLoadPtoService.java:25)
    at com.job.sync.job.ptoz.service.TempoLoadPtoService$$FastClassBySpringCGLIB$$54a8c059.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at com.job.sync.job.ptoz.service.TempoLoadPtoService$$EnhancerBySpringCGLIB$$f28b7807.csvBulkInsertService(<generated>)
    at com.job.sync.job.ptoz.service.PTOZ.csvProcessing(PTOZ.java:252)
    at com.job.sync.job.ptoz.service.PTOZ.execute(PTOZ.java:86)
    at com.job.sync.job.ptoz.service.PTOZ$$FastClassBySpringCGLIB$$d1b01301.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684)
    at com.job.sync.job.ptoz.service.PTOZ$$EnhancerBySpringCGLIB$$f5d658ff.execute(<generated>)
    at com.job.sync.job.ptoz.service.RunnerService.runJar(RunnerService.java:69)
    at com.job.sync.job.ptoz.LoadPtozmdCommandApplication.main(LoadPtozmdCommandApplication.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
    at org.hibernate.internal.AbstractSharedSessionContract.checkTransactionNeededForUpdateOperation(AbstractSharedSessionContract.java:398)
    at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1580)
    at com.job.sync.job.ptoz.mydbdb.dao.TempoLoadPtoCustomImpl.bulkLoadData(TempoLoadPtoCustomImpl.java:49)
    at com.job.sync.job.ptoz.mydbdb.dao.TempoLoadPtoCustomImpl$$FastClassBySpringCGLIB$$88b2e904.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at com.job.sync.job.ptoz.mydbdb.dao.TempoLoadPtoCustomImpl$$EnhancerBySpringCGLIB$$cb2734e8.bulkLoadData(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359)
    at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
    ... 37 more

0 个答案:

没有答案