我在Web应用程序的后端有这段代码。它有时会在while(rs.next())处抛出NullPointerException,有时会抛出SQLException在ResultSet也在while(rs.next())关闭后不允许运行,但有时它不会抛出任何错误。谁知道问题是什么?
public HashSet<LocalDate> getAvailableDates(long listingNumber) throws SQLException{
Statement statement = null;
String getDatesAvail = "SELECT AVAILDATE FROM AVAILABILITY WHERE LISTINGNUM = " +listingNumber +";";
HashSet<LocalDate> avail = new HashSet<LocalDate>();
ResultSet rs = null;
try {
connect(); // Open dbConnection
statement = dbConnection.createStatement();
rs = statement.executeQuery(getDatesAvail);
System.out.println(getDatesAvail);
if (rs == null) {
System.out.println("Result set is null for avail");
}
while(rs.next()) {
avail.add(LocalDate.parse(rs.getString("AVAILDATE"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}
} catch (SQLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
throw new SQLException();
} finally {
if (rs != null) {
rs.close();
}
if (statement != null) {
statement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
return avail;
}
我的connect()方法:
private void connect() {
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection( DB_CONNECTION, DB_USER, DB_PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
}
这是NullPointerException的堆栈跟踪:
java.lang.NullPointerException
at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6309)
at car_service.JDBCConnector.getAvailableDates(JDBCConnector.java:1170)
at car_service.JDBCConnector.getListing(JDBCConnector.java:974)
at car_service.ListingController.getListing(ListingController.java:347)
at car_service.ListingController.lambda$6(ListingController.java:94)
at spark.RouteImpl$1.handle(RouteImpl.java:72)
at spark.http.matching.Routes.execute(Routes.java:61)
at spark.http.matching.MatcherFilter.doFilter(MatcherFilter.java:130)
at spark.embeddedserver.jetty.JettyHandler.doHandle(JettyHandler.java:50)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1568)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:530)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:347)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:256)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)
at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:382)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:708)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:626)
at java.lang.Thread.run(Unknown Source)
以下是SQLException的堆栈跟踪:
java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861)
at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:743)
at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6288)
at car_service.JDBCConnector.getAvailableDates(JDBCConnector.java:1170)
at car_service.JDBCConnector.getListing(JDBCConnector.java:974)
at car_service.ListingController.getListing(ListingController.java:347)
at car_service.ListingController.lambda$6(ListingController.java:94)
at spark.RouteImpl$1.handle(RouteImpl.java:72)
at spark.http.matching.Routes.execute(Routes.java:61)
at spark.http.matching.MatcherFilter.doFilter(MatcherFilter.java:130)
at spark.embeddedserver.jetty.JettyHandler.doHandle(JettyHandler.java:50)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1568)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:530)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:347)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:256)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)
at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:382)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:708)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:626)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:0)
您的代码不是线程安全的。您错误地将node <-
df %>%
distinct(author_id, vendor) %>%
rename(id = author_id) %>%
mutate(vendor = as.numeric(vendor)) %>%
mutate(id = as.character(id))
edge <-
df %>%
group_by(topic_id) %>%
expand(topic_id, from = author_id, to = author_id) %>%
filter(from < to) %>%
select(to, from, topic_id) %>%
mutate_at(vars(to, from), as.character)
tidy_net <- tbl_graph(nodes = node, edges = edge, directed = F)
plot(tidy_net)
对象存储为成员变量。它应该是一个局部变量,并在该方法中打开和关闭,其下面有一个连接池。