开发者

Using JobStoreCMT in Quartz - preventing automatic commit

I'm trying to use a JDBC Job Store in Quartz with the following code:

DateTime dt = new DateTime().plusHours(2);

JobDetail jobDetail = new JobDetail(identifier, "group", TestJob.class);
SimpleTrigger trigger = new SimpleTrigger(identifier, dt.toDate());

trigger.setJobName(identifier);
trigger.setJobGroup("group");

quartzScheduler.addJob(jobDetail, true);
quartzScheduler.scheduleJob(trigger);

And am configuring the scheduler as follows:

<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
<property name="autoStartup" value="true" />
<property name="waitForJobsToCompleteOnShutdown" value="false" />
<property name="dataSource" ref="schedulerDataSource" />
<property name="nonTransactionalDataSource" ref="nonTXdataSource" />
<property name="quartzProperties">
    <props>
        <!--Job Store -->
        <prop key="org.quartz.jobStore.driverDelegateClass">
            org.quartz.impl开发者_运维知识库.jdbcjobstore.StdJDBCDelegate
        </prop>

        <prop key="org.quartz.jobStore.class">
            org.quartz.impl.jdbcjobstore.JobStoreCMT
        </prop>
        <prop key="org.quartz.jobStore.tablePrefix">QRTZ_</prop>
    </props>
</property>
</bean>

The schedulerDataSource is a standard JNDI data source, the nonTXdataSource is configured via a simple org.springframework.jdbc.datasource.DriverManagerDataSource I have specified the job store class to be: org.quartz.impl.jdbcjobstore.JobStoreCMT and was hoping that the code:

quartzScheduler.addJob(jobDetail, true);
quartzScheduler.scheduleJob(trigger);

would not commit the job to the database when the each method is called. Basically when I call addJob the job is immediately saved to the database, the scheduleJob method causes the trigger information to be immediately saved in the database as well, but this tends to happen over two separate transactions already.

There is a fair bit of subsequent logic in the code that needs to be committed to the database together with the scheduled jobs in one transactions, however no matter what I try the jobs are committed by the scheduler to the database as soon as they methods are called. I tried in various environments Testing/Tomcat/Glassfish and various configurations of data sources but to no avail.

Can somebody point me into the direction of where I am going wrong?

Thank you.


Having thought this over a bit, now i believe you can achieve this providing your own wrapping DataSource but you should not do this. I think Quartz maintains some internal state in memory that must be in sync with the database (or at least it can do so). If you rollback a transaction or otherwise modify database state not notifying Quartz about this fact, it may not work as expected.

On the other hand you can use Quartz's pausing of the jobs to achieve similar effect: you simply create new job and pause it before adding any triggers. Then, you resume it only after you commit your transaction.

---------------------- my original answer ----------------------

I think, but I'm not sure, not tried this, that you can try the following:

You need a transaction around a code that uses DataSource.getConnection internally. To achieve that you have to use data source that'd be aware of global transaction state. I suppose that JBoss application server gives you just that (even with plain data source).

JBoss comes with a transaction manager (Arjuna) and data sources wrappers (JBoss app server internal) that are at least aware of global transaction state.

Other options include Atomikos and a XA data source, but i have less experience here.

Edit: if Quartz uses explicit COMMIT or setAutocommit(true) internally, both my suggestions would not work.


When you set datasource on SchedulerFactoryBean, spring uses below class as JobStore ( extension to Quartz's JobStoreCMT )

LocalDataSourceJobStore This supports both transactional and non-transactional DataSource access.

Please try following

  1. Remove property org.quartz.jobStore.class [Edit : Its ignored ,anyways]

  2. Make sure the method which does addJob / ScheduleJob is in spring managed transaction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜