繁体   English   中英

Spring @Transactional Timeout无法按预期工作

[英]Spring @Transactional Timeout not working as expected

我有一个JDBC批量更新操作可能需要很长时间,因此我使用事务超时来处理这个问题。

@Override
@Transactional(propagation=Propagation.REQUIRES_NEW,timeout=10)
public void saveAllUsingBatch(List<KillPrintModel> list){
    PreparedStatmentMapper ps=  new HibernateDao.PreparedStatmentMapper<KillPrintModel>() {

        @Override
        public void prepareStatement(PreparedStatement ps, KillPrintModel t)
                throws SQLException {
            ps.setString(1, t.getOffice());
            ps.setString(2, t.getAccount());
            ps.setDate(3, new java.sql.Date(t.getUpdatedOn().getTime()));
        }
    };
    String sql = String.format("INSERT INTO dbo.%s (%s,%s,%s) VALUES (?,?,?)",KillPrintModel.TABLE_NAME,KillPrintModel.FIELD_Office,KillPrintModel.FIELD_Account,KillPrintModel.FIELD_UpdatedOn);
    this.jdbcBatchOperation(list, sql, ps);
}

即使我的事务时间超过10秒,此方法也会持续一分钟以上(并成功返回)。 当超时为0时,它工作正常。


是因为我的线程一旦开始执行就一直处于运行状态?

如果在跟踪模式下调试没有帮助,只需在以下hibernate类中放置一个断点,它们最终会在@Transactional Annotation的preparedstatement.setQueryTimeout(...)中设置一个超时。

org.hibernate.engine.jdbc.internal.StatementPreparerImpl
private void setStatementTimeout(PreparedStatement preparedStatement) throws SQLException {
            final int remainingTransactionTimeOutPeriod = jdbcCoordinator.determineRemainingTransactionTimeOutPeriod();
            if ( remainingTransactionTimeOutPeriod > 0 ) {
                preparedStatement.setQueryTimeout( remainingTransactionTimeOutPeriod );
            }
        }

或者甚至更好,早在事务管理器和步骤直到你点击statement.setQueryTimout(..)。

org.springframework.orm.hibernate4.HibernateTransactionManager
int timeout = determineTimeout(definition);
            if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
                // Use Hibernate's own transaction timeout mechanism on Hibernate 3.1+
                // Applies to all statements, also to inserts, updates and deletes!
                hibTx = session.getTransaction();
                hibTx.setTimeout(timeout);
                hibTx.begin();
            }
            else {
                // Open a plain Hibernate transaction without specified timeout.
                hibTx = session.beginTransaction();
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM