问题
i'm running mysql 5.5 with mysql 5.1.18 connector. a simple query of style
select * from my_table where column_a in ('aaa','bbb',...) and column b=1;
is executed from within java application. the query returns a resultset of 25k rows, 8 columns in each. while reading the results in while loop
while(rs.next())
{
MyObject c= new MyObject();
c.setA(rs.getString("A"));
c.setB(rs.getString("B"));
c.setC(rs.getString("C"));
...
}
a following exception is thrown, usually during the first loops, but never in the same row:
java.lang.NullPointerException
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5720)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5570)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5610)
i took a look at the source code in ResultSetImpl.java:5720 and i see the following:
switch (metadata.getSQLType())
where metadata is
Field metadata = this.fields[internalColumnIndex];
and getSQLType is a logic-less getter returning an int. what's interesting, is that the same metadata object is called numerous times several lines above with other getters, and throws no exceptions.
btw, there is no problem with the query above while ran directly within mysql. application runs in aws.
any ideas how to solve this? thanks.
回答1:
I ran into this issue, using a Spring Data CrudRepository, retrieving a Stream of results from a MySql database running on AWS RDS, with a moderately convoluted query.
It would also throw at a non-deterministic row, after about 30k rows.
I resolved this issue by annotating the calling method with @Transactional.
Although you're not using JPA, setting up a transaction for your database access may help your issue.
回答2:
From my experience, this error is a result of locked table while trying to read/write simultaneously from the same table. You need to add a pool for every request or some wait time between every operation to the MySQL.
Related answer as well: Getting java.sql.SQLException: Operation not allowed after ResultSet closed
来源:https://stackoverflow.com/questions/10570929/resultsetimpl-throws-nullpointerexception