为了配置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;
任何帮助将不胜感激 非常感谢你
答案 0 :(得分:1)
一些事情:
BatchConfigurer
。您需要提供的只是DataSource
bean。应该为你提供其余的......