Skip to content

Commit cbc9478

Browse files
committed
Fix for PooledConnectionRegressionTest.testPacketTooLargeException() failing with high max_allowed_packet.
Change-Id: I6b394c6384e4b2b87f49b4e223701a0ff0258205
1 parent 55f5db9 commit cbc9478

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

src/test/java/testsuite/regression/PooledConnectionRegressionTest.java

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
import com.mysql.cj.jdbc.CommentClientInfoProvider;
5858
import com.mysql.cj.jdbc.ConnectionImpl;
5959
import com.mysql.cj.jdbc.ConnectionWrapper;
60-
import com.mysql.cj.jdbc.DatabaseMetaDataMysqlSchema;
6160
import com.mysql.cj.jdbc.DatabaseMetaDataInformationSchema;
61+
import com.mysql.cj.jdbc.DatabaseMetaDataMysqlSchema;
6262
import com.mysql.cj.jdbc.JdbcConnection;
6363
import com.mysql.cj.jdbc.JdbcPropertySetImpl;
6464
import com.mysql.cj.jdbc.MysqlConnectionPoolDataSource;
@@ -260,37 +260,33 @@ public void testConnectionReclaim() throws Exception {
260260
*/
261261
@Test
262262
public void testPacketTooLargeException() throws Exception {
263-
final ConnectionEventListener conListener = new ConnectionListener();
264-
PooledConnection pc = null;
265-
266-
pc = this.cpds.getPooledConnection();
267-
268-
pc.addConnectionEventListener(conListener);
269-
270-
createTable("testPacketTooLarge", "(field1 LONGBLOB)");
263+
String defaultMaxAllowedPacket = getMysqlVariable("max_allowed_packet");
271264

272-
Connection connFromPool = pc.getConnection();
273-
PreparedStatement pstmtFromPool = ((ConnectionWrapper) connFromPool).clientPrepare("INSERT INTO testPacketTooLarge VALUES (?)");
274-
275-
this.rs = this.stmt.executeQuery("SHOW VARIABLES LIKE 'max_allowed_packet'");
276-
this.rs.next();
277-
278-
int maxAllowedPacket = this.rs.getInt(2);
265+
try {
266+
final int maxPacket = 10240;
267+
final int numChars = (int) (maxPacket * 1.2);
279268

280-
int numChars = (int) (maxAllowedPacket * 1.2);
269+
this.stmt.execute("SET GLOBAL max_allowed_packet=" + maxPacket);
270+
createTable("testPacketTooLarge", "(data LONGBLOB)");
281271

282-
pstmtFromPool.setBinaryStream(1, new BufferedInputStream(new FileInputStream(newTempBinaryFile("testPacketTooLargeException", numChars))), numChars);
272+
PooledConnection pc = this.cpds.getPooledConnection();
273+
pc.addConnectionEventListener(new ConnectionListener());
274+
Connection connFromPool = pc.getConnection();
283275

284-
assertThrows(PacketTooBigException.class, () -> {
285-
pstmtFromPool.executeUpdate();
286-
return null;
287-
});
276+
PreparedStatement pstmtFromPool = ((ConnectionWrapper) connFromPool).clientPrepare("INSERT INTO testPacketTooLarge VALUES (?)");
277+
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(newTempBinaryFile("testPacketTooLargeException", numChars)))) {
278+
pstmtFromPool.setBinaryStream(1, bis, numChars);
279+
assertThrows(PacketTooBigException.class, () -> pstmtFromPool.executeUpdate());
280+
}
288281

289-
// This should still work okay, even though the last query on the same connection didn't...
290-
this.rs = connFromPool.createStatement().executeQuery("SELECT 1");
282+
// Ensure subsequent queries still work after previous failure.
283+
this.rs = connFromPool.createStatement().executeQuery("SELECT 1");
291284

292-
assertEquals(0, this.connectionErrorEventCount);
293-
assertEquals(0, this.closeEventCount);
285+
assertEquals(0, this.connectionErrorEventCount);
286+
assertEquals(0, this.closeEventCount);
287+
} finally {
288+
this.stmt.execute("SET GLOBAL max_allowed_packet = " + defaultMaxAllowedPacket);
289+
}
294290
}
295291

296292
/**
@@ -449,14 +445,14 @@ public void testConnectionWrapperMethods() throws Exception {
449445

450446
assertEquals(this.conn.getAutoCommit(), cw.getAutoCommit());
451447
assertEquals(((JdbcConnection) this.conn).getAutoIncrementIncrement(), cw.getAutoIncrementIncrement());
452-
assertEquals(((JdbcConnection) this.conn).getCatalog(), cw.getCatalog());
448+
assertEquals(this.conn.getCatalog(), cw.getCatalog());
453449
assertEquals(((JdbcConnection) this.conn).getCharacterSetMetadata(), cw.getCharacterSetMetadata());
454-
assertEquals(((JdbcConnection) this.conn).getHoldability(), cw.getHoldability());
450+
assertEquals(this.conn.getHoldability(), cw.getHoldability());
455451
assertEquals(((JdbcConnection) this.conn).getHost(), cw.getHost());
456452
assertEquals(((JdbcConnection) this.conn).getHostPortPair(), cw.getHostPortPair());
457453
assertEquals(Properties.class, cw.getProperties().getClass());
458454
assertEquals(JdbcPropertySetImpl.class, cw.getPropertySet().getClass());
459-
assertEquals(((JdbcConnection) this.conn).getSchema(), cw.getSchema());
455+
assertEquals(this.conn.getSchema(), cw.getSchema());
460456
assertEquals(((JdbcConnection) this.conn).getServerVersion().toString(), cw.getServerVersion().toString());
461457
assertEquals(NativeSession.class, cw.getSession().getClass());
462458
assertEquals(((JdbcConnection) this.conn).getSessionMaxRows(), cw.getSessionMaxRows());
@@ -492,17 +488,18 @@ public void testConnectionWrapperMethods() throws Exception {
492488
assertNotEquals(((JdbcConnection) this.conn).getStatementComment(), cw.getStatementComment());
493489

494490
assertNull(cw.getExceptionInterceptor());
495-
assertEquals(((JdbcConnection) this.conn).getNetworkTimeout(), cw.getNetworkTimeout());
496-
assertEquals(((JdbcConnection) this.conn).getTypeMap(), cw.getTypeMap());
491+
assertEquals(this.conn.getNetworkTimeout(), cw.getNetworkTimeout());
492+
assertEquals(this.conn.getTypeMap(), cw.getTypeMap());
497493
assertNull(cw.getWarnings());
498494

499495
// testsuite is built upon non-SSL default connection with additional useSSL=false&allowPublicKeyRetrieval=true properties
500496
assertFalse(cw.hasSameProperties((JdbcConnection) this.conn));
501497

502498
assertTrue(cw.isSameResource((JdbcConnection) this.conn));
503-
assertEquals(((JdbcConnection) this.conn).nativeSQL("SELECT 1"), cw.nativeSQL("SELECT 1"));
499+
assertEquals(this.conn.nativeSQL("SELECT 1"), cw.nativeSQL("SELECT 1"));
504500

505-
assertEquals(cw.getServerVersion().meetsMinimum(new ServerVersion(8, 0, 3)) ? DatabaseMetaDataInformationSchema.class : DatabaseMetaDataMysqlSchema.class,
501+
assertEquals(
502+
cw.getServerVersion().meetsMinimum(new ServerVersion(8, 0, 3)) ? DatabaseMetaDataInformationSchema.class : DatabaseMetaDataMysqlSchema.class,
506503
cw.getMetaData().getClass());
507504

508505
// TODO find a way to test following methods
@@ -735,7 +732,7 @@ private void checkConnectionReturnedToPool(ConnectionWrapper cw) throws Exceptio
735732
assertEquals(((JdbcConnection) this.conn).getHostPortPair(), cw.getHostPortPair());
736733
assertEquals(Properties.class, cw.getProperties().getClass());
737734
assertEquals(JdbcPropertySetImpl.class, cw.getPropertySet().getClass());
738-
assertEquals(((JdbcConnection) this.conn).getSchema(), cw.getSchema());
735+
assertEquals(this.conn.getSchema(), cw.getSchema());
739736
assertEquals(((JdbcConnection) this.conn).getServerVersion().toString(), cw.getServerVersion().toString());
740737
assertEquals(NativeSession.class, cw.getSession().getClass());
741738
assertEquals(((JdbcConnection) this.conn).getSessionMaxRows(), cw.getSessionMaxRows());
@@ -786,7 +783,7 @@ private void checkConnectionReturnedToPool(ConnectionWrapper cw) throws Exceptio
786783

787784
assertEquals(ConnectionImpl.class, cw.getActiveMySQLConnection().getClass());
788785
assertNull(cw.getExceptionInterceptor());
789-
assertEquals(((JdbcConnection) this.conn).getNetworkTimeout(), cw.getNetworkTimeout());
786+
assertEquals(this.conn.getNetworkTimeout(), cw.getNetworkTimeout());
790787
assertThrows(SQLNonTransientConnectionException.class, "Logical handle no longer valid", () -> {
791788
cw.getTypeMap();
792789
return null;

0 commit comments

Comments
 (0)