Bug #13444 driver returns general error ; illegalargumentexception when using nio
Submitted: 23 Sep 2005 17:43 Modified: 23 Sep 2005 18:11
Reporter: Steve Ragle Email Updates:
Status: Won't fix Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:3.1.10 OS:Windows (WIndows)
Assigned to: Mark Matthews CPU Architecture:Any

[23 Sep 2005 17:43] Steve Ragle
Description:
com.mysql.jdbc.ChannelBuffer.java's readString method tries to set the buffer position past the limit at this line:

	final String readString() {

		int len = 0;
		int maxLen = getBufLength();
		int oldPosition = getPosition();

		while ((getPosition() < maxLen) && (this.directBuffer.get() != 0)) {
			len++;
		}

		setPosition(oldPosition);

		String s = new String(bufferToArray(), getPosition(), len);

>>>         this.directBuffer.position(getPosition() + len + 1); // update cursor

causing an IllegalArgumentException to be thrown from Sun's nio Buffer class:

    public final Buffer position(int newPosition) {
	if ((newPosition > limit) || (newPosition < 0))
>>>	    throw new IllegalArgumentException();

In my case, it was trying to set position 11 and limit was 10.

Here is the head of the stack trace:

java.sql.SQLException: General error: java.lang.IllegalArgumentException
	at com.mysql.jdbc.MysqlIO.buildResultSetWithUpdates(MysqlIO.java:2045)
	at com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:1939)
	at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:1384)
	at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1133)
	at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:670)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1159)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1076)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1061)
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:101)
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:101)

How to repeat:
here is the url form I am using:

jdbc:mysql://${sql.host}/${sql.database}?autoReconnectForPools=true&prepStmtCacheSize=100&cachePrepStmts=true&elideSetAutoCommits=true&useLocalSessionState=true&useNewIO=true

if I change to this, the error goes away:

jdbc:mysql://${sql.host}/${sql.database}?autoReconnectForPools=true&prepStmtCacheSize=100&cachePrepStmts=true&elideSetAutoCommits=true&useLocalSessionState=true

I am using an auto-increment column and the preparedstatement uses Statement.RETURN_GENERATED_KEYS in this particular case, not sure if that matters.  It fails whether doing an insert of a blank row (insert into foo () values();) just to get the next id, and when inserting a row with some values set.  This is failing on 10 different tables.

Java 1.5.0_05
MySql running on Windows XP, version 4.1.10-nt-log
[23 Sep 2005 18:11] Mark Matthews
We're removing NIO support from future versions of the driver, as it's turned out to have no benefit for client-side applications that use blocking I/O (besides the fact that not all platforms support it correctly).

I'd suggest removing your dependence on this feature if possible.
[23 Sep 2005 18:38] Steve Ragle
Sounds like a plan.  Thanks for the quick reply.
[1 Mar 2012 1:40] mike marchywka
Anything change in the last few years? LOL.
I'm looking at threading models for a java server and just spent a few minutes watching a set of conns to dead DB block and thought polling would be nice for that and other reasons as we work with netty etc.
Thanks.