Quartz调度程序 - 每次都有新的jdbc连接

时间:2015-06-19 08:47:53

标签: java spring hibernate quartz-scheduler

我遇到Quartz Scheduler和数据库配置问题。每次调度程序检查是否存在新作业都会创建新的JDBC连接。如何避免创建新连接?

2015-06-19 10:42:05,522 DEBUG DriverManagerDataSource:142 - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/db?characterEncoding=UTF-8]
2015-06-19 10:42:05,544 DEBUG LocalDataSourceJobStore:3182 - Found 0 triggers that missed their scheduled fire-time.
2015-06-19 10:42:05,545 DEBUG DataSourceUtils:327 - Returning JDBC Connection to DataSource
2015-06-19 10:42:07,522 DEBUG LocalDataSourceJobStore:3933 - MisfireHandler: scanning for misfires...
2015-06-19 10:42:07,522 DEBUG DriverManagerDataSource:142 - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/db?characterEncoding=UTF-8]
2015-06-19 10:42:07,539 DEBUG LocalDataSourceJobStore:3182 - Found 0 triggers that missed their scheduled fire-time.
2015-06-19 10:42:07,539 DEBUG DataSourceUtils:327 - Returning JDBC Connection to DataSource

配置

<bean id="scheduler" name="scheduler"
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
    scope="singleton">
    <property name="quartzProperties">
        <props>

            <prop key="org.quartz.scheduler.instanceId">AUTO</prop>
            <prop key="org.quartz.scheduler.instanceName">USER_JOBS</prop>
            <prop key="org.quartz.jobStore.class">org.quartz.impl.jdbcjobstore.JobStoreTX</prop>
            <prop key="org.quartz.jobStore.driverDelegateClass">
                org.quartz.impl.jdbcjobstore.StdJDBCDelegate
            </prop>
            <prop key="org.quartz.jobStore.tablePrefix">QRTZ_</prop>
            <prop key="org.quartz.jobStore.isClustered">false</prop>
            <prop key="org.quartz.jobStore.clusterCheckinInterval">20000</prop>
            <prop key="org.quartz.jobStore.misfireThreshold">2000</prop>
        </props>
    </property>

    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
</bean>

和datasource,对于hibernate和quartz scheduler来说是一样的

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${db.driver}" />
    <property name="url" value="${db.url}" />
    <property name="username" value="${db.username}" />
    <property name="password" value="${db.password}" />
</bean>

1 个答案:

答案 0 :(得分:4)

据我所知,您使用org.springframework.jdbc.datasource.DriverManagerDataSource类作为数据源。根据javadoc,每次有人调用getConnection时都会创建jdbc连接。我确信Quartz会在内部调用此方法。

要解决问题,您应该使用池化的DataSource。例如,c3p0(查看com.mchange.v2.c3p0.ComboPooledDataSource