gpt4 book ai didi

hibernate - Spring 批处理-HibernateItemWriter : Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port

转载 作者:行者123 更新时间:2023-12-04 13:25:04 25 4
gpt4 key购买 nike

我有一个使用 HibernateItemWriter 更新 Azure SQL Server 数据库的 Spring Batch 应用程序。

批处理作业执行了 20 分钟并因以下错误而失败

Could not open Hibernate Session for transaction; nested exception isorg.hibernate.exception.JDBCConnectionException: Unable to acquireJDBC connection

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IPconnection to the hostsqlsrv-01.database.windows.net, port 1433 hasfailed. Error: "sqlsrv-01.database.windows.net:Temporary failure in name resolution. Verify the connectionproperties. Make sure that an instance of SQL Server is running on thehost and accepting TCP/IP connections at the port. Make sure that TCPconnections to the port are not blocked by a firewall.".

下面是我的实现

public class StoreWriter implements ItemWriter<List<Store>> {

Logger logger = Logger.getLogger(StoreWriter.class);

@Autowired
private SessionFactory sessionFactory;

@Override
public void write(List<? extends List<Store>> items) throws Exception {
HibernateItemWriter<Store> hibernateItemWriter = new HibernateItemWriter<>();
hibernateItemWriter.setSessionFactory(sessionFactory);

for (List<Store> Store : items) {

hibernateItemWriter.write(Store);

}
hibernateItemWriter.afterPropertiesSet();
logger.info(String.format("Store Processing Completed %s", new LocalDateTime()));
}}

我想这是一个临时问题,但我想知道我是否可以实现重试逻辑来处理这个问题?

最佳答案

StoreWriter 不会在 HibernateItemWriter 上添加任何值。您可以在步骤中直接使用 HibernateItemWriter

StoreWriter 为每个 block 创建一个新的 HibernateItemWriter,这效率不高并且可能是问题的根本原因(即为每个 block 创建一个新 session ,并且在某些时候,不可能再获取新 session ,因此出现错误 Could not open Hibernate Session for transaction)。委托(delegate)编写器应该被重构为一个字段,例如:

public class StoreWriter implements ItemWriter<List<Store>> {

Logger logger = Logger.getLogger(StoreWriter.class);

private HibernateItemWriter<Store> hibernateItemWriter;

public StoreWriter(HibernateItemWriter<Store> hibernateItemWriter) {
this.hibernateItemWriter = hibernateItemWriter;
}

@Override
public void write(List<? extends List<Store>> items) throws Exception {
for (List<Store> Store : items) {
hibernateItemWriter.write(Store);
}
logger.info(String.format("Store Processing Completed %s", new LocalDateTime()));
}
}

关于hibernate - Spring 批处理-HibernateItemWriter : Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68922956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com