Spring 批处理 - HibernateItemWriter:确保 SQL 服务器的实例在主机上 运行 并在端口上接受 TCP/IP 连接
Spring Batch - HibernateItemWriter : Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port
我有一个 Spring 批处理应用程序,它使用 HibernateItemWriter 来更新 Azure SQL 服务器数据库。
批处理作业执行了 20 分钟后失败并出现以下错误
Could not open Hibernate Session for transaction; nested exception is
org.hibernate.exception.JDBCConnectionException: Unable to acquire
JDBC connection
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP
connection to the host
sqlsrv-01.database.windows.net, port 1433 has
failed. Error: "sqlsrv-01.database.windows.net:
Temporary failure in name resolution. Verify the connection
properties. Make sure that an instance of SQL Server is running on the
host and accepting TCP/IP connections at the port. Make sure that TCP
connections 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
为每个块创建一个新的 HibernateItemWriter
,这效率不高并且可能是您问题的根本原因(即为每个块创建一个新会话,并且在某些情况下点,不可能再获取新会话,因此出现错误 Could not open Hibernate Session for transaction
)。委托编写器应该重构为一个字段,类似于:
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()));
}
}
我有一个 Spring 批处理应用程序,它使用 HibernateItemWriter 来更新 Azure SQL 服务器数据库。
批处理作业执行了 20 分钟后失败并出现以下错误
Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC connection
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host sqlsrv-01.database.windows.net, port 1433 has failed. Error: "sqlsrv-01.database.windows.net: Temporary failure in name resolution. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections 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
为每个块创建一个新的 HibernateItemWriter
,这效率不高并且可能是您问题的根本原因(即为每个块创建一个新会话,并且在某些情况下点,不可能再获取新会话,因此出现错误 Could not open Hibernate Session for transaction
)。委托编写器应该重构为一个字段,类似于:
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()));
}
}