使用@Transactional注释时延迟执行

时间:2018-05-24 08:32:20

标签: java spring hibernate jpa-2.0 spring-transactions

我在方法上添加 @Transactional 注释时遇到了问题。 在启动方法之前的某个时间,它延迟启动15分钟(在我的日志中为928787毫秒),然后在几毫秒内完成执行。

并不总是复制。我在100000个方法执行中发现了这个问题。

当TaskDispatcher试图调用save()方法时,代理中出现了问题。

TaskDispatcher.java

public class TaskDispatcher extends Thread {
    private DBProcessor dbProcessor;
    private ThreadPoolTaskExecutor myTaskExecutor;
    ...
    @Override
    public void run() {
        this.myTaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                dbProcessor.processData(data);
            }
        });
    }

}

DBProcessor.java

public class DBProcessor extends ADataProcessor {
    private DBService dbService;
    ...
    @Override
    public boolean processData(Data data) throws DataProcessorException {
        long startTime = System.currentTimeMillis();

        success = dbService.save(tradeReportID, translatedMessages, result);

        logger.info("processing time : {} ms.", tradeReportID, action, (System.currentTimeMillis() - startTime));

        return success;
    }

}

DBServiceImpl.java

public class DBServiceImpl extends CustomRepository implements DBService {
    ...
    @Transactional
    public boolean save(String tradeOrderId, List<TranslatedMessage> translatedMessages, DBDealServiceResult result) throws DealProcessorException, DealProcessorExceptionNoRollback {
        logger.info("start DB processing");

        ... // do some db operation

        logger.info("finished DB processing");
        return success;
    }
}

spring.xml

...
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

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

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="connectionPoolDataSource" />
    <property name="packagesToScan" value="com.mycompany.myapp.jpa.entity" />
    <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />

    <property name="jpaPropertyMap">  
        <map>
            <entry key="hibernate.generate_statistics" value="${hibernate.generate_statistics}"/>
            <entry key="hibernate.format_sql" value="${hibernate.format_sql}"/>
        </map>
    </property>
</bean>

<bean id="hibernateJpaVendorAdapter"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true" />
    <property name="databasePlatform" value="org.hibernate.dialect.Oracle12cDialect" /> 
</bean>

日志显示执行的时间

2018/05/17 01:58:36.903|INFO |[myTaskExecutor-4] c.m.m.a.d.DBServiceImpl start DB processing
2018/05/17 01:58:36.915|INFO |[myTaskExecutor-4] c.m.m.a.p.DBServiceImpl finished DB processing
2018/05/17 01:58:36.920|INFO |[myTaskExecutor-4] c.m.m.a.d.DBProcessor processing time : 17 ms.
2018/05/17 01:58:36.931|INFO |[myTaskExecutor-16] c.m.m.a.d.DBServiceImpl start DB processing
2018/05/17 01:58:37.745|INFO |[myTaskExecutor-16] c.m.m.a.p.DBServiceImpl finished DB processing
2018/05/17 01:58:37.745|INFO |[myTaskExecutor-16] c.m.m.a.d.DBProcessor processing time : 928787 ms.

版本详细信息:

spring-framework : 5.0.5.RELEASE
spring-tx : 5.0.5.RELEASE
spring-jdbc : 5.0.5.RELEASE
spring-orm : 5.0.5.RELEASE
hibernate-jpa-2.1-api : 1.0.0.Final
hibernate-core : 5.2.7.Final
tomcat-jdbc : 7.0.70
oracle driver : ojdbc7 12.1.0.2

0 个答案:

没有答案