java – Spring @Transactional Timeout无法按预期工作
发布时间:2020-12-15 02:08:41 所属栏目:Java 来源:网络整理
导读:我有一个JDBC批量更新操作可能需要很长时间,因此我使用事务超时来处理这个问题. @Override@Transactional(propagation=Propagation.REQUIRES_NEW,timeout=10)public void saveAllUsingBatch(ListKillPrintModel list){ PreparedStatmentMapper ps= new Hiber
我有一个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(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读