Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DatabaseType EnterpriseDb not found in spring batch 5.1 #4681

Open
chetan1993-prog opened this issue Oct 14, 2024 · 4 comments
Open

DatabaseType EnterpriseDb not found in spring batch 5.1 #4681

chetan1993-prog opened this issue Oct 14, 2024 · 4 comments
Labels
status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter type: bug

Comments

@chetan1993-prog
Copy link

chetan1993-prog commented Oct 14, 2024

Hi Team,

We have migrated our project to Spring Boot 3.2.5. As part of this migration, Spring Batch has also been updated. However, we are noticing that Spring Batch is failing with the driver "com.edb.Driver." I have set the databaseType to "POSTGRES," but it is still failing. I have tried using a custom CustomJobRepositoryFactoryBean, but the issue persists. Is there any workaround to fix this issue in version 5.1?

**Error**:
Caused by: java.lang.IllegalArgumentException: **DatabaseType not found for product name: [EnterpriseDB]**\n\tat org.springframework.batch.support.DatabaseType.fromProductName(DatabaseType.java:71)\n\tat org.springframework.batch.support.DatabaseType.fromMetaData(DatabaseType.java:109)\n\tat

Below is the workaround I tried.

  @Bean
  public JobRepository jobRepository(EntityManagerFactory entityManagerFactory) throws Exception {
    log.info(
        "Creating job repository instance------------------------------------------------------------------");
    JobRepositoryFactoryBean fb = new JobRepositoryFactoryBean();
    fb.setTransactionManager(transactionManager(entityManagerFactory));
    fb.setIsolationLevelForCreate(ISOLATION_LEVEL);
    fb.setTablePrefix("batch_schema.BATCH_");
    try {
       fb.setDatabaseType(DatabaseType.POSTGRES.name());
      fb.afterPropertiesSet();
      return fb.getObject();
    } catch (Exception e) {
      throw new BatchConfigurationException("Unable to configure the default job repo", e);
    }
  }
  1. Using CustomJobRepositoryFactoryBean
  public class CustomJobRepositoryFactoryBean extends JobRepositoryFactoryBean {

  @Override
  public void setDataSource(DataSource dataSource) {
    super.setDataSource(dataSource);

    try {
      String productName = dataSource.getConnection().getMetaData().getDatabaseProductName();
      if ("EnterpriseDB".equalsIgnoreCase(productName)) {
        super.setDatabaseType("POSTGRES"); //PostgreSQL
      } else {
        super.setDatabaseType(DatabaseType.fromProductName(productName).getProductName());
      }
    } catch (SQLException e) {
      throw new RuntimeException("Failed to determine database type", e);
    }
  }
}
@chetan1993-prog chetan1993-prog added status: waiting-for-triage Issues that we did not analyse yet type: bug labels Oct 14, 2024
@chetan1993-prog
Copy link
Author

Hi @fmbenhassine Could you please assist me here?

@fmbenhassine
Copy link
Contributor

I think you should use fb.setDatabaseType(DatabaseType.POSTGRES.getProductName()); instead of fb.setDatabaseType(DatabaseType.POSTGRES.name()); (which returns the constant name and not the product name). Can you please try that and let me know?

@fmbenhassine fmbenhassine added status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter and removed status: waiting-for-triage Issues that we did not analyse yet labels Oct 15, 2024
@chetan1993-prog
Copy link
Author

chetan1993-prog commented Oct 15, 2024

Hi @fmbenhassine , Thank you for quick response.
I have tried using fb.setDatabaseType(DatabaseType.POSTGRES.getProductName());, but the issue still persists

Below is my BatchConfigurer for your reference.

@slf4j
@component
public class BatchConfigure extends DefaultBatchConfiguration {

@Autowired DataSource dataSource;

@Autowired private EntityManagerFactory entityManagerFactory;

static final String ISOLATION_LEVEL = "ISOLATION_DEFAULT";

@bean
public JobRepository jobRepository() {
JobRepositoryFactoryBean fb = new JobRepositoryFactoryBean();
fb.setDatabaseType(DatabaseType.POSTGRES.getProductName());

fb.setDataSource(dataSource);
fb.setTransactionManager(getTransactionManager());
fb.setTablePrefix("batch_schema.BATCH_");
fb.setIsolationLevelForCreate(ISOLATION_LEVEL);
try {
    fb.afterPropertiesSet();
  return Objects.requireNonNull(fb.getObject());
} catch (Exception e) {
  throw new RuntimeException(e);
}

}

@bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
log.debug("Datasource, {}", dataSource);
transactionManager.setEntityManagerFactory(entityManagerFactory);
transactionManager.setDataSource(dataSource);
return transactionManager;
}

@bean
public JobLauncher jobLauncher() {
TaskExecutorJobLauncher jobLauncher = new TaskExecutorJobLauncher();
jobLauncher.setJobRepository(this.jobRepository());
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
try {
jobLauncher.afterPropertiesSet();
} catch (Exception e) {
throw new RuntimeException(e);
}
return jobLauncher;
}

@bean
public JobExplorer jobExplorer() {
JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
jobExplorerFactoryBean.setDataSource(dataSource);
jobExplorerFactoryBean.setTransactionManager(getTransactionManager());
try {
jobExplorerFactoryBean.afterPropertiesSet();
jobExplorerFactoryBean.setTransactionManager(getTransactionManager());
return Objects.requireNonNull(jobExplorerFactoryBean.getObject());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

@chetan1993-prog
Copy link
Author

chetan1993-prog commented Oct 15, 2024

I have tried using the @EnableBatchProcessing annotation as well however, after adding it, I am seeing the following in the PCF logs. According to this, it is not considering the JobRepository bean, which is created explicitly, even after adding the allow-bean-definition-overriding: true property in application.yml

Bean jobRepository already defined in the application context, skipping the registration of a jobRepository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter type: bug
Projects
None yet
Development

No branches or pull requests

2 participants