0

我有两个 Quartz (1.8.3) 作业,通过 Spring (2.5.6) 配置,其中一个写入(发送)到数据库,一个从中读取(检查)。

<bean id="scheduleFactory"
      class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="Check"/>
            <ref bean="Send"/>
        </list>
    </property>
</bean>

<bean id="Send" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail">
        <bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="StatusMonitor" />
            <property name="targetMethod" value="sendMessage" />
        </bean>
    </property>
    <property name="cronExpression" value="0 0/1 * * * ?" />
</bean>

<bean id="Check" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail">
        <bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="StatusMonitor" />
            <property name="targetMethod" value="checkAndUpdateStatus" />
        </bean>
    </property>
    <property name="cronExpression" value="30 0/1 * * * ?" />
</bean>

事务管理器设置:

<tx:annotation-driven transaction-manager="TransactionManager"/>

在这两个作业中,我明确地在事务中运行读/写操作,如下所示:

@Override
    public synchronized void sendMessage() {
        try {
            TransactionTemplate tt = new TransactionTemplate(ptm);
            tt.execute(new TransactionCallbackWithoutResult() {
                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    ...
                    statusDAO.update(status);
                    ...
                }
            });
            log.info("Status was updated");
        } catch (Exception e) {
            ...
        }
    }

其中 ptm 是一个 TransactionManager bean,通过 Spring 注入。我在日志中看到“状态已更新”记录,但是当我从事务读取方法中读取此记录时,它有时已过时。此外,当我使用 SQL 编辑器读取此记录时,它也已过时。我不明白,为什么交易在这种情况下不起作用,你有什么想法吗?谢谢。

4

1 回答 1

1

对于任何可能感兴趣的人。这对我有用

<bean name="applicationDataCollectorControllerJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass" value="org.mypckage.controller.jobs.ApplicationDataCollectorController" />
    <property name="jobDataAsMap">
        <map>
            <!--<entry key="timeout" value="1" />-->
            <entry key="genericService" value-ref="genericService" />


            <entry key="applicationDataCollectorService" value-ref="applicationDataCollectorService" />

            <entry key="transactionManager" value-ref="transactionManager" />

        </map>
    </property>



</bean>

--- 在调度程序 bean 中---

@Override
protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {

getApplicationDataCollectorService().collectData(transactionManager);


}

----在applicationDataCollectorService bean中-----

public void collectData( org.springframework.transaction.jta.JtaTransactionManager transactionManager) {



        try {
            this.transactionManager = transactionManager;
            testTransactionalSave();

        } catch (Exception e) {
            BUSY = false;
            e.printStackTrace();
        }

    }

}

private void testTransactionalSave() throws Exception {

    TransactionTemplate tt = new TransactionTemplate(transactionManager);
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus ts) {
            try {
                ApplicationParameter appPara = null;
                List<ApplicationParameter> appParaList = genericService.getListFromHQL("select o from ApplicationParameter as o order by o.id desc", false);
                if (appParaList != null) {
                    if (appParaList.size() > 0) {
                        appPara = (ApplicationParameter) appParaList.get(0);
                        appPara.setLastBankStatementMailNum(appPara.getLastBankStatementMailNum() + 10);

                        appPara = (ApplicationParameter) genericService.mergeObject(appPara);

                        System.out.println(" num is now = " + appPara.getLastBankStatementMailNum());
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();

            }
        }
    });

}

注意:不要忘记将 transactionManager 声明为两个带有公共 setter 和 getter 的 bean 中的私有属性,以便 spring 将其连接起来。任何问题?yemiosigbesan@gmail.com

于 2014-02-24T09:37:06.443 回答