Mashrabbek Akbarov
Mashrabbek Akbarov

Reputation: 336

Failed to obtain DB connection from data source

I'm creating a in memory quartz scheduler in Spring Boot, but getting a connection error. I have a connected database 'oracle'. once i run I get following error. Can anybody help

ERROR [http-nio-80-exec-1] org.apache.juli.logging.DirectJDKLog: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.quartz.JobPersistenceException: Failed to obtain DB connection from data source 'myDS': java.sql.SQLException: Connections could not be acquired from the underlying database!

this is my quartz.properties file

#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.makeSchedulerThreadDaemon = true

#============================================================================
# Configure ThreadPool
#============================================================================

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.makeThreadsDaemons = true
#org.quartz.threadPool.threadCount= 20
#org.quartz.threadPool.threadPriority= 5

#============================================================================
# Configure JobStore
#============================================================================
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
org.quartz.jobStore.useProperties=true
org.quartz.jobStore.misfireThreshold=25000
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.jobStore.isClustered=false
org.quartz.jobStore.dataSource=myDS

#============================================================================
# Configure Datasources
#============================================================================

org.quartz.dataSource.myDS.driver = oracle.jdbc.pool.OracleDataSource
org.quartz.dataSource.myDS.URL = jdbc:oracle:thin:@x.x.x.x:1521:fdf
org.quartz.dataSource.myDS.user = username
org.quartz.dataSource.myDS.password = password
org.quartz.dataSource.myDS.maxConnections = 5
org.quartz.dataSource.myDS.validationQuery = select 1

this is how I created scheduler in Controller class

@RequestMapping("/start")
  public String start() throws SchedulerException {
  Scheduler scheduler;
  SchedulerFactory schedulerFactory  = new StdSchedulerFactory(quartzProperties());
scheduler = schedulerFactory.getScheduler();
    JobDetail jobDetail =
                JobBuilder.newJob(Job1.class)
                    .usingJobData("job1", "description")
                    .storeDurably(true)
                    .requestRecovery(true)
                    .build();
            scheduler.scheduleJob(jobDetail, Trigger1.fire());
scheduler.start();
return "redirect:/";
}
  @Bean
  public Properties quartzProperties() throws IOException {
    PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
    propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
    propertiesFactoryBean.afterPropertiesSet();
    return propertiesFactoryBean.getObject();
  }

and Job1.class

@Slf4j
public class Job1 implements Job {

  @Override
  public void execute(JobExecutionContext jobContext) {
    JobDetail jobDetail = jobContext.getJobDetail();
    log.info(
        "\nname: {}, \nstart: {}, \nnext: {}",
        jobDetail.getJobDataMap().getString("job 1"),
        jobContext.getFireTime(),
        jobContext.getNextFireTime());
  }
}

and this is Trigger1 class

public class Trigger1 {

  public static Trigger trigger;

  public static Trigger fire() {
    trigger =
        TriggerBuilder.newTrigger()
            .withIdentity("trigger 1")
            .startAt(new Date())
            .withSchedule(CronScheduleBuilder.cronSchedule("/1 * * ? * * *"))
            .build();
    return trigger;
  }
}

Furthermore I run this sql scripts in my db tables_oracle.sql

Upvotes: 2

Views: 8335

Answers (1)

Mashrabbek Akbarov
Mashrabbek Akbarov

Reputation: 336

I had to add jdbc maven dependancy.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

and creating SchedulerFactoryBean and setting dataSource to it solved the issue

Upvotes: 1

Related Questions