gpt4 book ai didi

java - Spring Batch 中 H2 数据库的元表

转载 作者:行者123 更新时间:2023-12-01 11:58:01 24 4
gpt4 key购买 nike

我在使用 h2 db 配置 Spring Batch 时遇到一些麻烦
我的配置如下所示

@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 Batch core.jar 时,我可以找到 h2 数据库模式),所以我得到了异常:

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. 该异常不是因为无法连接到数据库,而是因为尚未创建表(该异常表明您可以连接到数据库)。我没有看到您运行初始化脚本。
  2. 你的接线过度了。根据您正在做的事情,您不需要实现 BatchConfigurer。您需要提供的只是DataSource bean。剩下的应该为你提供......

关于java - Spring Batch 中 H2 数据库的元表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28211334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com