Java spring batch writer don';我不能毫无差错地工作

Java spring batch writer don';我不能毫无差错地工作,java,spring,oracle11g,spring-batch,Java,Spring,Oracle11g,Spring Batch,我尝试了一个springbatch示例,我使用一个读卡器从数据库读取数据,使用一个写卡器向数据库写入数据。当我运行main类时,它从数据库中打印元素(这意味着从数据库中正确读取),但在数据库中停止写入,并且不显示任何错误。我很困惑,任何帮助都将被感激。我把控制台日志放在这里,如果可以帮你的话。多谢各位 spring上下文: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframewor

我尝试了一个springbatch示例,我使用一个读卡器从数据库读取数据,使用一个写卡器向数据库写入数据。当我运行main类时,它从数据库中打印元素(这意味着从数据库中正确读取),但在数据库中停止写入,并且不显示任何错误。我很困惑,任何帮助都将被感激。我把控制台日志放在这里,如果可以帮你的话。多谢各位

spring上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
                           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
 <import resource="datasource.xml"/>
 <import resource="/job1.xml"/>


 <bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
   <property name="dataSource" ref="dataSource1"></property>
   <property name="transactionManager" ref="transactionManager"></property>
   <property name="databaseType" value="oracle"></property>
 </bean >



    <bean  id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
       <property name="jobRepository" ref="jobRepository"> </property>
     </bean>
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
   <property name="dataSource" ref="dataSource1"></property>
 </bean>
     </beans>
主要类别:

package main;

import java.util.Date;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main
{
  public static void main (String [] args ) 
  {
  ApplicationContext context=new ClassPathXmlApplicationContext("config/spring-context.xml");
  JobLauncher jobLauncher=(JobLauncher) context.getBean("jobLauncher");
  Job job=(Job) context.getBean("personJob");
  try
  {
   JobParameters param=new JobParametersBuilder().addLong("max", (long) 269).toJobParameters();
    //JobParameters param=new JobParametersBuilder().addLong("now", System.currentTimeMillis()).toJobParameters();

   JobExecution jobExecution=jobLauncher.run(job, param);
   System.out.println("job done..."+jobExecution.getStatus());
  }
  catch (JobExecutionException e) {

  }
 }
}
控制台日志:

Oct 19, 2018 2:05:23 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ccabbaa: startup date [Fri Oct 19 14:05:23 EDT 2018]; root of context hierarchy
Oct 19, 2018 2:05:23 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config/spring-context.xml]
Oct 19, 2018 2:05:23 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config/datasource.xml]
Oct 19, 2018 2:05:23 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config/job1.xml]
Oct 19, 2018 2:05:23 PM org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
INFO: Overriding bean definition for bean 'personReader' with a different definition: replacing [Generic bean: class [org.springframework.batch.item.database.JdbcCursorItemReader]; scope=step; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=false; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [config/job1.xml]] with [Root bean: class [org.springframework.aop.scope.ScopedProxyFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in BeanDefinition defined in class path resource [config/job1.xml]]
Oct 19, 2018 2:05:23 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: oracle.jdbc.OracleDriver
Oct 19, 2018 2:05:23 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: oracle.jdbc.OracleDriver
Oct 19, 2018 2:05:23 PM org.springframework.oxm.jaxb.Jaxb2Marshaller createJaxbContextFromClasses
INFO: Creating JAXBContext with classes to be bound [class main.Person1]
Oct 19, 2018 2:05:24 PM org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet
INFO: No TaskExecutor has been set, defaulting to synchronous executor.
Oct 19, 2018 2:05:25 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=personJob]] launched with the following parameters: [{max=269}]
Oct 19, 2018 2:05:27 PM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [stepAAA]
int_id :1   indiv_id :778141667541
.
.
.
int_id :122   indiv_id :547249915381

您正在配置中使用
无资源TransactionManager

<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">

此事务管理器不与任何资源(因此得名)交互,可以将其视为“NoOp”事务管理器(文档中有更多详细信息)

因此,此事务管理器不会提交任何事务,因此,由于没有错误,项目将(无声地)不会写入数据库。您可以在步骤中使用
org.springframework.jdbc.datasource.DataSourceTransactionManager
,当没有错误发生时,您的项目将被写入数据库


希望这有帮助。

您的捕获块是空的。所以,您永远不会知道是否确实抛出了异常。请至少添加
e.printStackTrace()首先在捕捉块内部。另外,您的XML配置文件中有两个bean具有相同的id
personWriter
,您正在从数据库中读取数据并写入数据库,因此请保留与您的问题相关的最少信息(请删除StateEventItemWriter,关于数据源的重复信息(datasource1和datasource2粘贴两次,等等))。提前谢谢。根据评论更新谢谢你的回答。我做了你建议的更改。问题是,只有从表中删除所有要保存数据的数据并且必须手动插入一个元素时,它才起作用,但如果删除所有数据并运行作业,它将执行与以前相同的结果(读取第一个块并在那里停止,没有错误,编译器保持运行状态)。你能帮忙吗?
package main;

public class Person1
{
  Long internal_id;
  Long individual_id;

  public Long getInternal_id()
  {
    return internal_id;
  }
  public void setInternal_id(Long internal_id)
  {
    this.internal_id = internal_id;
  }

  public Long getIndividual_id()
  {
    return individual_id;
  }
  public void setIndividual_id(Long individual_id)
  {
    this.individual_id = individual_id;
  }

}
package main;

import java.util.Date;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main
{
  public static void main (String [] args ) 
  {
  ApplicationContext context=new ClassPathXmlApplicationContext("config/spring-context.xml");
  JobLauncher jobLauncher=(JobLauncher) context.getBean("jobLauncher");
  Job job=(Job) context.getBean("personJob");
  try
  {
   JobParameters param=new JobParametersBuilder().addLong("max", (long) 269).toJobParameters();
    //JobParameters param=new JobParametersBuilder().addLong("now", System.currentTimeMillis()).toJobParameters();

   JobExecution jobExecution=jobLauncher.run(job, param);
   System.out.println("job done..."+jobExecution.getStatus());
  }
  catch (JobExecutionException e) {

  }
 }
}
Oct 19, 2018 2:05:23 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ccabbaa: startup date [Fri Oct 19 14:05:23 EDT 2018]; root of context hierarchy
Oct 19, 2018 2:05:23 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config/spring-context.xml]
Oct 19, 2018 2:05:23 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config/datasource.xml]
Oct 19, 2018 2:05:23 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config/job1.xml]
Oct 19, 2018 2:05:23 PM org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
INFO: Overriding bean definition for bean 'personReader' with a different definition: replacing [Generic bean: class [org.springframework.batch.item.database.JdbcCursorItemReader]; scope=step; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=false; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [config/job1.xml]] with [Root bean: class [org.springframework.aop.scope.ScopedProxyFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in BeanDefinition defined in class path resource [config/job1.xml]]
Oct 19, 2018 2:05:23 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: oracle.jdbc.OracleDriver
Oct 19, 2018 2:05:23 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: oracle.jdbc.OracleDriver
Oct 19, 2018 2:05:23 PM org.springframework.oxm.jaxb.Jaxb2Marshaller createJaxbContextFromClasses
INFO: Creating JAXBContext with classes to be bound [class main.Person1]
Oct 19, 2018 2:05:24 PM org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet
INFO: No TaskExecutor has been set, defaulting to synchronous executor.
Oct 19, 2018 2:05:25 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=personJob]] launched with the following parameters: [{max=269}]
Oct 19, 2018 2:05:27 PM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [stepAAA]
int_id :1   indiv_id :778141667541
.
.
.
int_id :122   indiv_id :547249915381
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">