|
57 | 57 | import com.mysql.cj.jdbc.CommentClientInfoProvider; |
58 | 58 | import com.mysql.cj.jdbc.ConnectionImpl; |
59 | 59 | import com.mysql.cj.jdbc.ConnectionWrapper; |
60 | | -import com.mysql.cj.jdbc.DatabaseMetaDataMysqlSchema; |
61 | 60 | import com.mysql.cj.jdbc.DatabaseMetaDataInformationSchema; |
| 61 | +import com.mysql.cj.jdbc.DatabaseMetaDataMysqlSchema; |
62 | 62 | import com.mysql.cj.jdbc.JdbcConnection; |
63 | 63 | import com.mysql.cj.jdbc.JdbcPropertySetImpl; |
64 | 64 | import com.mysql.cj.jdbc.MysqlConnectionPoolDataSource; |
@@ -260,37 +260,33 @@ public void testConnectionReclaim() throws Exception { |
260 | 260 | */ |
261 | 261 | @Test |
262 | 262 | 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"); |
271 | 264 |
|
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); |
279 | 268 |
|
280 | | - int numChars = (int) (maxAllowedPacket * 1.2); |
| 269 | + this.stmt.execute("SET GLOBAL max_allowed_packet=" + maxPacket); |
| 270 | + createTable("testPacketTooLarge", "(data LONGBLOB)"); |
281 | 271 |
|
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(); |
283 | 275 |
|
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 | + } |
288 | 281 |
|
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"); |
291 | 284 |
|
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 | + } |
294 | 290 | } |
295 | 291 |
|
296 | 292 | /** |
@@ -449,14 +445,14 @@ public void testConnectionWrapperMethods() throws Exception { |
449 | 445 |
|
450 | 446 | assertEquals(this.conn.getAutoCommit(), cw.getAutoCommit()); |
451 | 447 | assertEquals(((JdbcConnection) this.conn).getAutoIncrementIncrement(), cw.getAutoIncrementIncrement()); |
452 | | - assertEquals(((JdbcConnection) this.conn).getCatalog(), cw.getCatalog()); |
| 448 | + assertEquals(this.conn.getCatalog(), cw.getCatalog()); |
453 | 449 | assertEquals(((JdbcConnection) this.conn).getCharacterSetMetadata(), cw.getCharacterSetMetadata()); |
454 | | - assertEquals(((JdbcConnection) this.conn).getHoldability(), cw.getHoldability()); |
| 450 | + assertEquals(this.conn.getHoldability(), cw.getHoldability()); |
455 | 451 | assertEquals(((JdbcConnection) this.conn).getHost(), cw.getHost()); |
456 | 452 | assertEquals(((JdbcConnection) this.conn).getHostPortPair(), cw.getHostPortPair()); |
457 | 453 | assertEquals(Properties.class, cw.getProperties().getClass()); |
458 | 454 | assertEquals(JdbcPropertySetImpl.class, cw.getPropertySet().getClass()); |
459 | | - assertEquals(((JdbcConnection) this.conn).getSchema(), cw.getSchema()); |
| 455 | + assertEquals(this.conn.getSchema(), cw.getSchema()); |
460 | 456 | assertEquals(((JdbcConnection) this.conn).getServerVersion().toString(), cw.getServerVersion().toString()); |
461 | 457 | assertEquals(NativeSession.class, cw.getSession().getClass()); |
462 | 458 | assertEquals(((JdbcConnection) this.conn).getSessionMaxRows(), cw.getSessionMaxRows()); |
@@ -492,17 +488,18 @@ public void testConnectionWrapperMethods() throws Exception { |
492 | 488 | assertNotEquals(((JdbcConnection) this.conn).getStatementComment(), cw.getStatementComment()); |
493 | 489 |
|
494 | 490 | 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()); |
497 | 493 | assertNull(cw.getWarnings()); |
498 | 494 |
|
499 | 495 | // testsuite is built upon non-SSL default connection with additional useSSL=false&allowPublicKeyRetrieval=true properties |
500 | 496 | assertFalse(cw.hasSameProperties((JdbcConnection) this.conn)); |
501 | 497 |
|
502 | 498 | 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")); |
504 | 500 |
|
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, |
506 | 503 | cw.getMetaData().getClass()); |
507 | 504 |
|
508 | 505 | // TODO find a way to test following methods |
@@ -735,7 +732,7 @@ private void checkConnectionReturnedToPool(ConnectionWrapper cw) throws Exceptio |
735 | 732 | assertEquals(((JdbcConnection) this.conn).getHostPortPair(), cw.getHostPortPair()); |
736 | 733 | assertEquals(Properties.class, cw.getProperties().getClass()); |
737 | 734 | assertEquals(JdbcPropertySetImpl.class, cw.getPropertySet().getClass()); |
738 | | - assertEquals(((JdbcConnection) this.conn).getSchema(), cw.getSchema()); |
| 735 | + assertEquals(this.conn.getSchema(), cw.getSchema()); |
739 | 736 | assertEquals(((JdbcConnection) this.conn).getServerVersion().toString(), cw.getServerVersion().toString()); |
740 | 737 | assertEquals(NativeSession.class, cw.getSession().getClass()); |
741 | 738 | assertEquals(((JdbcConnection) this.conn).getSessionMaxRows(), cw.getSessionMaxRows()); |
@@ -786,7 +783,7 @@ private void checkConnectionReturnedToPool(ConnectionWrapper cw) throws Exceptio |
786 | 783 |
|
787 | 784 | assertEquals(ConnectionImpl.class, cw.getActiveMySQLConnection().getClass()); |
788 | 785 | assertNull(cw.getExceptionInterceptor()); |
789 | | - assertEquals(((JdbcConnection) this.conn).getNetworkTimeout(), cw.getNetworkTimeout()); |
| 786 | + assertEquals(this.conn.getNetworkTimeout(), cw.getNetworkTimeout()); |
790 | 787 | assertThrows(SQLNonTransientConnectionException.class, "Logical handle no longer valid", () -> { |
791 | 788 | cw.getTypeMap(); |
792 | 789 | return null; |
|
0 commit comments