Spring Batch中H2 db的元表

时间:2015-01-29 09:40:30

标签: java spring batch-processing spring-batch h2

为了配置h2 db
的弹簧批,我遇到了一些麻烦 我的配置如下所示

@Configuration
@EnableBatchProcessing
public class BatchConfiguration implements BatchConfigurer {

    @Bean
    public DataSource dataSource() {
       EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(Driver.class.getName());
        String h2Url = MessageFormat.format("jdbc:h2:file:{0}note;MODE=Oracle", System.getProperty("java.io.tmpdir"));           
        LOGGER.info("Using H2 with URL : {}", h2Url);
        dataSource.setUrl(h2Url);
        dataSource.setUsername("sa");
        dataSource.setPassword("");
        return dataSource;

    }

    @Bean
    public DefaultPersistenceUnitManager persistenceUnitManager() {
        DefaultPersistenceUnitManager defaultPersistenceUnitManager = new DefaultPersistenceUnitManager();
        defaultPersistenceUnitManager.setPersistenceXmlLocation("classpath*:/META-INF/persistence.xml");
        defaultPersistenceUnitManager.setDefaultDataSource(dataSource());
        return defaultPersistenceUnitManager;
    }

    @Bean
    public HibernateJpaVendorAdapter jpaAdapter() {
        HibernateJpaVendorAdapter jpaAdapter = new HibernateJpaVendorAdapter();
        jpaAdapter.setDatabasePlatform(H2Dialect.class.getName());
        jpaAdapter.setGenerateDdl(true);
        jpaAdapter.setShowSql(true);
        return jpaAdapter;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean myEmf() {
        LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        localContainerEntityManagerFactoryBean.setDataSource(dataSource());
        localContainerEntityManagerFactoryBean.setPersistenceUnitManager(persistenceUnitManager());
        localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaAdapter());
        localContainerEntityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        return localContainerEntityManagerFactoryBean;
    }

    public JobRepository getJobRepository() throws Exception {
        JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
        factory.setDataSource(dataSource());
        factory.setTablePrefix("BATCH_");
        factory.setTransactionManager(getTransactionManager());
        factory.afterPropertiesSet();
        return (JobRepository) factory.getObject();
    }

    public JobLauncher getJobLauncher() throws Exception {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(getJobRepository());
        jobLauncher.afterPropertiesSet();
        return jobLauncher;
    }

    public JobExplorer getJobExplorer() throws Exception {
        JobExplorerFactoryBean factory = new JobExplorerFactoryBean();
        factory.setDataSource(dataSource());
        factory.setTablePrefix("BATCH_");
        factory.afterPropertiesSet();
        return factory.getObject();
    }

    public PlatformTransactionManager getTransactionManager() {
        return new JpaTransactionManager(myEmf().getObject());
    }

}

当我开始工作时,它无法连接到数据库,因为配置无法检索元数据(在检查spring批处理核心时.jar,我可以找到h2 db模式)所以我得到了异常:< / p>

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]; nested exception is org.h2.jdbc.JdbcSQLException: Table "BATCH_JOB_INSTANCE" not found; 

任何帮助将不胜感激 非常感谢你

1 个答案:

答案 0 :(得分:1)

一些事情:

  1. 该异常不是因为无法连接到数据库,而是因为尚未创建表(Exception表示您可以连接到数据库)。我没有看到你运行初始化脚本。
  2. 你的所有布线都太过分了。根据您的工作,您不需要实施BatchConfigurer。您需要提供的只是DataSource bean。应该为你提供其余的......