在几次调用后,我有用于SQL语句的sql的jdbs模板,并使用hikari连接池 我例外,我使用try-with-resources,我的错误在哪里?(Servlet容器-Tomcat)
public class SimpleJdbcTemplate {
private Connection connection;
private DataSource dataSource;
private ResultSet resultSet;
PreparedStatement preparedStatement;
public SimpleJdbcTemplate(Connection connection) {
this.connection = connection;
}
//private DataSourse datasource - из hikariconnectionpool
public SimpleJdbcTemplate(DataSource dataSource) {
this.dataSource = dataSource;
}
public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) {
try (PreparedStatement preparedStatement = dataSource.getConnection().prepareStatement(sql)){
resultSet = null;
List<T> result = new ArrayList<>();
int position = 1;
for (Object arg : args) {
preparedStatement.setObject(position, arg);
position++;
}
if (sql.contains("UPDATE") || sql.contains("update") ||sql.toLowerCase().contains("delete")||sql.toLowerCase().contains("insert")) {
preparedStatement.executeUpdate();
} else{
resultSet = preparedStatement.executeQuery();
if (resultSet == null) {
throw new SQLException("No resultsSet");
}
while (resultSet.next()) {
result.add(rowMapper.mapRow(resultSet));
}
}
log.info(result.toString());
return result;
} catch (SQLException e) {
throw new IllegalStateException(e);
日志:
2020年10月24日13:46:16.633严重[http-nio-8080-exec-3] org.apache.catalina.core.StandardWrapperValve.invoke Servlet的Servlet.service()[servlets.view.MessendjerViewServlet]在路径[/ LabWork_war]的上下文中抛出异常 java.lang.IllegalStateException:java.sql.SQLTransientConnectionException:HikariPool-1-连接不可用,请求在30014ms后超时。 在repository.utill.SimpleJdbcTemplate.query(SimpleJdbcTemplate.java:52) 在repository.jdbc.UserRepositoryImpl.findUserByUUID(UserRepositoryImpl.java:84) 在service.UserService.findUser(UserService.java:71) 在servlets.filter.AuthFilter.doFilter(AuthFilter.java:53) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) 在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) 在org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) 在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) 在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) 在org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:868) 在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1589) 在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 在java.base / java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) 在java.base / java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:630) 在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在java.base / java.lang.Thread.run(Thread.java:832) 原因:java.sql.SQLTransientConnectionException:HikariPool-1-连接不可用,请求在30014ms后超时。 在com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:695) 在com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:197) 在com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:162) 在com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:100) 在repository.utill.SimpleJdbcTemplate.query(SimpleJdbcTemplate.java:27) ... 22更多
答案 0 :(得分:0)
Okey我发现错误,此行无法正常运行(尝试使用资源) (reparedStatement prepareStatement = dataSource.getConnection()。prepareStatement(sql)) 我认为当我尝试关闭连接时,它从datasource(dataSource.getConnection())接受了随机连接,因此,我的连接始终处于打开状态,我将代码重新格式化为
public class SimpleJdbcTemplate {
DataSource dataSource;
//private DataSourse datasource - из hikariconnectionpool
public SimpleJdbcTemplate(DataSource dataSource) {
this.dataSource = dataSource;
}
public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) { //TODO repair connection leak
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = dataSource.getConnection();
preparedStatement = connection.prepareStatement(sql);
resultSet = null;
List<T> result = new ArrayList<>();
int position = 1;
for (Object arg : args) {
preparedStatement.setObject(position, arg);
position++;
}
if (sql.contains("UPDATE") || sql.contains("update") || sql.toLowerCase().contains("delete") || sql.toLowerCase().contains("insert")) {
preparedStatement.executeUpdate();
} else {
resultSet = preparedStatement.executeQuery();
if (resultSet == null) {
throw new SQLException("No resultsSet");
}
while (resultSet.next()) {
result.add(rowMapper.mapRow(resultSet));
}
}
log.info(result.toString());
return result;
} catch (SQLException e) {
throw new IllegalStateException(e);
} finally {
try {
if (resultSet != null) {
resultSet.close();
log.info("resultes finaly closed? - {}", resultSet.isClosed());
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
if (preparedStatement != null) {
try {
preparedStatement.close();
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
try {
if (connection != null) {
connection.close();
connection.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
一切正常工作