2
Copyright (C) 2002-2007 MySQL AB
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of version 2 of the GNU General Public License as
6
published by the Free Software Foundation.
8
There are special exceptions to the terms and conditions of the GPL
9
as it is applied to this software. View the full text of the
10
exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11
software distribution.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
package testsuite.regression;
27
import java.io.ByteArrayOutputStream;
28
import java.io.PrintStream;
29
import java.lang.reflect.Field;
30
import java.lang.reflect.InvocationTargetException;
31
import java.lang.reflect.Method;
32
import java.sql.Connection;
33
import java.sql.DriverManager;
34
import java.sql.DriverPropertyInfo;
35
import java.sql.ResultSet;
36
import java.sql.SQLException;
37
import java.sql.Statement;
38
import java.util.HashMap;
39
import java.util.Iterator;
40
import java.util.Locale;
42
import java.util.Properties;
43
import java.util.StringTokenizer;
45
import testsuite.BaseTestCase;
47
import com.mysql.jdbc.Driver;
48
import com.mysql.jdbc.NonRegisteringDriver;
49
import com.mysql.jdbc.ReplicationConnection;
50
import com.mysql.jdbc.ReplicationDriver;
51
import com.mysql.jdbc.log.StandardLogger;
54
* Regression tests for Connections
56
* @author Mark Matthews
57
* @version $Id: ConnectionRegressionTest.java,v 1.1.2.1 2005/05/13 18:58:38
60
public class ConnectionRegressionTest extends BaseTestCase {
65
* the name of the testcase
67
public ConnectionRegressionTest(String name) {
72
* Runs all test cases in this test suite
76
public static void main(String[] args) {
77
junit.textui.TestRunner.run(ConnectionRegressionTest.class);
86
public void testBug1914() throws Exception {
87
System.out.println(this.conn
88
.nativeSQL("{fn convert(foo(a,b,c), BIGINT)}"));
89
System.out.println(this.conn
90
.nativeSQL("{fn convert(foo(a,b,c), BINARY)}"));
92
.println(this.conn.nativeSQL("{fn convert(foo(a,b,c), BIT)}"));
93
System.out.println(this.conn
94
.nativeSQL("{fn convert(foo(a,b,c), CHAR)}"));
95
System.out.println(this.conn
96
.nativeSQL("{fn convert(foo(a,b,c), DATE)}"));
97
System.out.println(this.conn
98
.nativeSQL("{fn convert(foo(a,b,c), DECIMAL)}"));
99
System.out.println(this.conn
100
.nativeSQL("{fn convert(foo(a,b,c), DOUBLE)}"));
101
System.out.println(this.conn
102
.nativeSQL("{fn convert(foo(a,b,c), FLOAT)}"));
103
System.out.println(this.conn
104
.nativeSQL("{fn convert(foo(a,b,c), INTEGER)}"));
105
System.out.println(this.conn
106
.nativeSQL("{fn convert(foo(a,b,c), LONGVARBINARY)}"));
107
System.out.println(this.conn
108
.nativeSQL("{fn convert(foo(a,b,c), LONGVARCHAR)}"));
109
System.out.println(this.conn
110
.nativeSQL("{fn convert(foo(a,b,c), TIME)}"));
111
System.out.println(this.conn
112
.nativeSQL("{fn convert(foo(a,b,c), TIMESTAMP)}"));
113
System.out.println(this.conn
114
.nativeSQL("{fn convert(foo(a,b,c), TINYINT)}"));
115
System.out.println(this.conn
116
.nativeSQL("{fn convert(foo(a,b,c), VARBINARY)}"));
117
System.out.println(this.conn
118
.nativeSQL("{fn convert(foo(a,b,c), VARCHAR)}"));
122
* Tests fix for BUG#3554 - Not specifying database in URL causes
123
* MalformedURL exception.
126
* if an error ocurrs.
128
public void testBug3554() throws Exception {
130
new NonRegisteringDriver().connect(
131
"jdbc:mysql://localhost:3306/?user=root&password=root",
133
} catch (SQLException sqlEx) {
134
assertTrue(sqlEx.getMessage().indexOf("Malformed") == -1);
144
public void testBug3790() throws Exception {
145
String field2OldValue = "foo";
146
String field2NewValue = "bar";
147
int field1OldValue = 1;
149
Connection conn1 = null;
150
Connection conn2 = null;
151
Statement stmt1 = null;
152
Statement stmt2 = null;
153
ResultSet rs2 = null;
155
Properties props = new Properties();
158
this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3790");
160
.executeUpdate("CREATE TABLE testBug3790 (field1 INT NOT NULL PRIMARY KEY, field2 VARCHAR(32)) TYPE=InnoDB");
161
this.stmt.executeUpdate("INSERT INTO testBug3790 VALUES ("
162
+ field1OldValue + ", '" + field2OldValue + "')");
164
conn1 = getConnectionWithProps(props); // creates a new connection
165
conn2 = getConnectionWithProps(props); // creates another new
167
conn1.setAutoCommit(false);
168
conn2.setAutoCommit(false);
170
stmt1 = conn1.createStatement();
171
stmt1.executeUpdate("UPDATE testBug3790 SET field2 = '"
172
+ field2NewValue + "' WHERE field1=" + field1OldValue);
175
stmt2 = conn2.createStatement();
177
rs2 = stmt2.executeQuery("SELECT field1, field2 FROM testBug3790");
179
assertTrue(rs2.next());
180
assertTrue(rs2.getInt(1) == field1OldValue);
181
assertTrue(rs2.getString(2).equals(field2NewValue));
183
this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3790");
208
* Tests if the driver configures character sets correctly for 4.1.x
209
* servers. Requires that the 'admin connection' is configured, as this test
210
* needs to create/drop databases.
215
public void testCollation41() throws Exception {
216
if (versionMeetsMinimum(4, 1) && isAdminConnectionConfigured()) {
217
Map charsetsAndCollations = getCharacterSetsAndCollations();
218
charsetsAndCollations.remove("latin7"); // Maps to multiple Java
220
charsetsAndCollations.remove("ucs2"); // can't be used as a
221
// connection charset
223
Iterator charsets = charsetsAndCollations.keySet().iterator();
225
while (charsets.hasNext()) {
226
Connection charsetConn = null;
227
Statement charsetStmt = null;
230
String charsetName = charsets.next().toString();
231
String collationName = charsetsAndCollations.get(
232
charsetName).toString();
233
Properties props = new Properties();
234
props.put("characterEncoding", charsetName);
236
System.out.println("Testing character set " + charsetName);
238
charsetConn = getAdminConnectionWithProps(props);
240
charsetStmt = charsetConn.createStatement();
243
.executeUpdate("DROP DATABASE IF EXISTS testCollation41");
245
.executeUpdate("DROP TABLE IF EXISTS testCollation41");
248
.executeUpdate("CREATE DATABASE testCollation41 DEFAULT CHARACTER SET "
250
charsetConn.setCatalog("testCollation41");
252
// We've switched catalogs, so we need to recreate the
253
// statement to pick this up...
254
charsetStmt = charsetConn.createStatement();
256
StringBuffer createTableCommand = new StringBuffer(
257
"CREATE TABLE testCollation41"
258
+ "(field1 VARCHAR(255), field2 INT)");
260
charsetStmt.executeUpdate(createTableCommand.toString());
263
.executeUpdate("INSERT INTO testCollation41 VALUES ('abc', 0)");
265
int updateCount = charsetStmt
266
.executeUpdate("UPDATE testCollation41 SET field2=1 WHERE field1='abc'");
267
assertTrue(updateCount == 1);
269
if (charsetStmt != null) {
271
.executeUpdate("DROP TABLE IF EXISTS testCollation41");
273
.executeUpdate("DROP DATABASE IF EXISTS testCollation41");
277
if (charsetConn != null) {
286
* Tests setReadOnly() being reset during failover
289
* if an error occurs.
291
public void testSetReadOnly() throws Exception {
292
Properties props = new Properties();
293
props.put("autoReconnect", "true");
295
String sepChar = "?";
297
if (BaseTestCase.dbUrl.indexOf("?") != -1) {
301
Connection reconnectableConn = DriverManager.getConnection(
302
BaseTestCase.dbUrl + sepChar + "autoReconnect=true", props);
304
this.rs = reconnectableConn.createStatement().executeQuery(
305
"SELECT CONNECTION_ID()");
308
String connectionId = this.rs.getString(1);
310
reconnectableConn.setReadOnly(true);
312
boolean isReadOnly = reconnectableConn.isReadOnly();
314
Connection killConn = getConnectionWithProps(null);
316
killConn.createStatement().executeUpdate("KILL " + connectionId);
319
SQLException caughtException = null;
323
while (caughtException == null && numLoops > 0) {
327
reconnectableConn.createStatement().executeQuery("SELECT 1");
328
} catch (SQLException sqlEx) {
329
caughtException = sqlEx;
334
.println("Executing statement on reconnectable connection...");
336
this.rs = reconnectableConn.createStatement().executeQuery(
337
"SELECT CONNECTION_ID()");
339
assertTrue("Connection is not a reconnected-connection", !connectionId
340
.equals(this.rs.getString(1)));
343
reconnectableConn.createStatement().executeQuery("SELECT 1");
344
} catch (SQLException sqlEx) {
348
reconnectableConn.createStatement().executeQuery("SELECT 1");
350
assertTrue(reconnectableConn.isReadOnly() == isReadOnly);
353
private Map getCharacterSetsAndCollations() throws Exception {
354
Map charsetsToLoad = new HashMap();
357
this.rs = this.stmt.executeQuery("SHOW character set");
359
while (this.rs.next()) {
360
charsetsToLoad.put(this.rs.getString("Charset"), this.rs
361
.getString("Default collation"));
365
// These don't have mappings in Java...
367
charsetsToLoad.remove("swe7");
368
charsetsToLoad.remove("hp8");
369
charsetsToLoad.remove("dec8");
370
charsetsToLoad.remove("koi8u");
371
charsetsToLoad.remove("keybcs2");
372
charsetsToLoad.remove("geostd8");
373
charsetsToLoad.remove("armscii8");
375
if (this.rs != null) {
380
return charsetsToLoad;
384
* Tests fix for BUG#4334, port #'s not being picked up for
385
* failover/autoreconnect.
388
* if an error occurs.
390
public void testBug4334() throws Exception {
391
if (isAdminConnectionConfigured()) {
392
Connection adminConnection = null;
395
adminConnection = getAdminConnection();
397
int bogusPortNumber = 65534;
399
NonRegisteringDriver driver = new NonRegisteringDriver();
401
Properties oldProps = driver.parseURL(BaseTestCase.dbUrl, null);
403
String host = driver.host(oldProps);
404
int port = driver.port(oldProps);
405
String database = oldProps
406
.getProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY);
407
String user = oldProps
408
.getProperty(NonRegisteringDriver.USER_PROPERTY_KEY);
409
String password = oldProps
410
.getProperty(NonRegisteringDriver.PASSWORD_PROPERTY_KEY);
412
StringBuffer newUrlToTestPortNum = new StringBuffer(
416
newUrlToTestPortNum.append(host);
419
newUrlToTestPortNum.append(":").append(port);
420
newUrlToTestPortNum.append(",");
423
newUrlToTestPortNum.append(host);
426
newUrlToTestPortNum.append(":").append(bogusPortNumber);
427
newUrlToTestPortNum.append("/");
429
if (database != null) {
430
newUrlToTestPortNum.append(database);
433
if ((user != null) || (password != null)) {
434
newUrlToTestPortNum.append("?");
437
newUrlToTestPortNum.append("user=").append(user);
439
if (password != null) {
440
newUrlToTestPortNum.append("&");
444
if (password != null) {
445
newUrlToTestPortNum.append("password=")
450
Properties autoReconnectProps = new Properties();
451
autoReconnectProps.put("autoReconnect", "true");
453
System.out.println(newUrlToTestPortNum);
456
// First test that port #'s are being correctly picked up
458
// We do this by looking at the error message that is returned
460
Connection portNumConn = DriverManager.getConnection(
461
newUrlToTestPortNum.toString(), autoReconnectProps);
462
Statement portNumStmt = portNumConn.createStatement();
463
this.rs = portNumStmt.executeQuery("SELECT connection_id()");
466
killConnection(adminConnection, this.rs.getString(1));
469
portNumStmt.executeQuery("SELECT connection_id()");
470
} catch (SQLException sqlEx) {
471
// we expect this one
475
portNumStmt.executeQuery("SELECT connection_id()");
476
} catch (SQLException sqlEx) {
477
assertTrue(sqlEx.getMessage().toLowerCase().indexOf(
478
"connection refused") != -1);
482
// Now make sure failover works
484
StringBuffer newUrlToTestFailover = new StringBuffer(
488
newUrlToTestFailover.append(host);
491
newUrlToTestFailover.append(":").append(port);
492
newUrlToTestFailover.append(",");
495
newUrlToTestFailover.append(host);
498
newUrlToTestFailover.append(":").append(bogusPortNumber);
499
newUrlToTestFailover.append("/");
501
if (database != null) {
502
newUrlToTestFailover.append(database);
505
if ((user != null) || (password != null)) {
506
newUrlToTestFailover.append("?");
509
newUrlToTestFailover.append("user=").append(user);
511
if (password != null) {
512
newUrlToTestFailover.append("&");
516
if (password != null) {
517
newUrlToTestFailover.append("password=").append(
522
Connection failoverConn = DriverManager.getConnection(
523
newUrlToTestFailover.toString(), autoReconnectProps);
524
Statement failoverStmt = portNumConn.createStatement();
525
this.rs = failoverStmt.executeQuery("SELECT connection_id()");
528
killConnection(adminConnection, this.rs.getString(1));
531
failoverStmt.executeQuery("SELECT connection_id()");
532
} catch (SQLException sqlEx) {
533
// we expect this one
536
failoverStmt.executeQuery("SELECT connection_id()");
538
if (adminConnection != null) {
539
adminConnection.close();
545
private static void killConnection(Connection adminConn, String threadId)
546
throws SQLException {
547
adminConn.createStatement().execute("KILL " + threadId);
551
* Tests fix for BUG#6966, connections starting up failed-over (due to down
552
* master) never retry master.
555
* if the test fails...Note, test is timing-dependent, but
556
* should work in most cases.
558
public void testBug6966() throws Exception {
559
Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
560
props.setProperty("autoReconnect", "true");
562
// Re-build the connection information
563
int firstIndexOfHost = BaseTestCase.dbUrl.indexOf("//") + 2;
564
int lastIndexOfHost = BaseTestCase.dbUrl.indexOf("/", firstIndexOfHost);
566
String hostPortPair = BaseTestCase.dbUrl.substring(firstIndexOfHost,
569
StringTokenizer st = new StringTokenizer(hostPortPair, ":");
574
if (st.hasMoreTokens()) {
575
String possibleHostOrPort = st.nextToken();
577
if (Character.isDigit(possibleHostOrPort.charAt(0)) &&
578
(possibleHostOrPort.indexOf(".") == -1 /* IPV4 */) &&
579
(possibleHostOrPort.indexOf("::") == -1 /* IPV6 */)) {
580
port = possibleHostOrPort;
583
host = possibleHostOrPort;
587
if (st.hasMoreTokens()) {
588
port = st.nextToken();
599
StringBuffer newHostBuf = new StringBuffer();
600
newHostBuf.append(host);
601
newHostBuf.append(":65532"); // make sure the master fails
602
newHostBuf.append(",");
603
newHostBuf.append(host);
605
newHostBuf.append(":");
606
newHostBuf.append(port);
609
props.remove("PORT");
611
props.setProperty("HOST", newHostBuf.toString());
612
props.setProperty("queriesBeforeRetryMaster", "50");
613
props.setProperty("maxReconnects", "1");
615
Connection failoverConnection = null;
618
failoverConnection = getConnectionWithProps("jdbc:mysql://"
619
+ newHostBuf.toString() + "/", props);
620
failoverConnection.setAutoCommit(false);
622
String originalConnectionId = getSingleIndexedValueWithQuery(
623
failoverConnection, 1, "SELECT CONNECTION_ID()").toString();
625
for (int i = 0; i < 49; i++) {
626
failoverConnection.createStatement().executeQuery("SELECT 1");
629
((com.mysql.jdbc.Connection)failoverConnection).clearHasTriedMaster();
631
failoverConnection.setAutoCommit(true);
633
String newConnectionId = getSingleIndexedValueWithQuery(
634
failoverConnection, 1, "SELECT CONNECTION_ID()").toString();
636
assertTrue(((com.mysql.jdbc.Connection)failoverConnection).hasTriedMaster());
638
assertTrue(!newConnectionId.equals(originalConnectionId));
640
failoverConnection.createStatement().executeQuery("SELECT 1");
642
if (failoverConnection != null) {
643
failoverConnection.close();
649
* Test fix for BUG#7952 -- Infinite recursion when 'falling back' to master
650
* in failover configuration.
653
* if the tests fails.
655
public void testBug7952() throws Exception {
656
Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
657
props.setProperty("autoReconnect", "true");
659
// Re-build the connection information
660
int firstIndexOfHost = BaseTestCase.dbUrl.indexOf("//") + 2;
661
int lastIndexOfHost = BaseTestCase.dbUrl.indexOf("/", firstIndexOfHost);
663
String hostPortPair = BaseTestCase.dbUrl.substring(firstIndexOfHost,
666
StringTokenizer st = new StringTokenizer(hostPortPair, ":");
671
if (st.hasMoreTokens()) {
672
String possibleHostOrPort = st.nextToken();
674
if (possibleHostOrPort.indexOf(".") == -1
675
&& Character.isDigit(possibleHostOrPort.charAt(0))) {
676
port = possibleHostOrPort;
679
host = possibleHostOrPort;
683
if (st.hasMoreTokens()) {
684
port = st.nextToken();
695
StringBuffer newHostBuf = new StringBuffer();
696
newHostBuf.append(host);
697
newHostBuf.append(":");
698
newHostBuf.append(port);
699
newHostBuf.append(",");
700
newHostBuf.append(host);
702
newHostBuf.append(":");
703
newHostBuf.append(port);
706
props.remove("PORT");
708
props.setProperty("HOST", newHostBuf.toString());
709
props.setProperty("queriesBeforeRetryMaster", "10");
710
props.setProperty("maxReconnects", "1");
712
Connection failoverConnection = null;
713
Connection killerConnection = getConnectionWithProps(null);
716
failoverConnection = getConnectionWithProps("jdbc:mysql://"
717
+ newHostBuf + "/", props);
718
((com.mysql.jdbc.Connection) failoverConnection)
719
.setPreferSlaveDuringFailover(true);
720
failoverConnection.setAutoCommit(false);
722
String failoverConnectionId = getSingleIndexedValueWithQuery(
723
failoverConnection, 1, "SELECT CONNECTION_ID()").toString();
725
System.out.println("Connection id: " + failoverConnectionId);
727
killConnection(killerConnection, failoverConnectionId);
729
Thread.sleep(3000); // This can take some time....
732
failoverConnection.createStatement().executeQuery("SELECT 1");
733
} catch (SQLException sqlEx) {
734
assertTrue("08S01".equals(sqlEx.getSQLState()));
737
((com.mysql.jdbc.Connection) failoverConnection)
738
.setPreferSlaveDuringFailover(false);
739
((com.mysql.jdbc.Connection) failoverConnection)
740
.setFailedOver(true);
742
failoverConnection.setAutoCommit(true);
744
String failedConnectionId = getSingleIndexedValueWithQuery(
745
failoverConnection, 1, "SELECT CONNECTION_ID()").toString();
746
System.out.println("Failed over connection id: "
747
+ failedConnectionId);
749
((com.mysql.jdbc.Connection) failoverConnection)
750
.setPreferSlaveDuringFailover(false);
751
((com.mysql.jdbc.Connection) failoverConnection)
752
.setFailedOver(true);
754
for (int i = 0; i < 30; i++) {
755
failoverConnection.setAutoCommit(true);
756
System.out.println(getSingleIndexedValueWithQuery(
757
failoverConnection, 1, "SELECT CONNECTION_ID()"));
758
// failoverConnection.createStatement().executeQuery("SELECT
760
failoverConnection.setAutoCommit(true);
763
String fallbackConnectionId = getSingleIndexedValueWithQuery(
764
failoverConnection, 1, "SELECT CONNECTION_ID()").toString();
765
System.out.println("fallback connection id: "
766
+ fallbackConnectionId);
769
* long begin = System.currentTimeMillis();
771
* failoverConnection.setAutoCommit(true);
773
* long end = System.currentTimeMillis();
775
* assertTrue("Probably didn't try failing back to the
776
* master....check test", (end - begin) > 500);
778
* failoverConnection.createStatement().executeQuery("SELECT 1");
781
if (failoverConnection != null) {
782
failoverConnection.close();
788
* Tests fix for BUG#7607 - MS932, SHIFT_JIS and Windows_31J not recog. as
794
public void testBug7607() throws Exception {
795
if (versionMeetsMinimum(4, 1)) {
796
Connection ms932Conn = null, cp943Conn = null, shiftJisConn = null, windows31JConn = null;
799
Properties props = new Properties();
800
props.setProperty("characterEncoding", "MS932");
802
ms932Conn = getConnectionWithProps(props);
804
this.rs = ms932Conn.createStatement().executeQuery(
805
"SHOW VARIABLES LIKE 'character_set_client'");
806
assertTrue(this.rs.next());
807
String encoding = this.rs.getString(2);
808
if (!versionMeetsMinimum(5, 0, 3)
809
&& !versionMeetsMinimum(4, 1, 11)) {
810
assertEquals("sjis", encoding.toLowerCase(Locale.ENGLISH));
812
assertEquals("cp932", encoding.toLowerCase(Locale.ENGLISH));
815
this.rs = ms932Conn.createStatement().executeQuery(
817
assertTrue(this.rs.next());
819
String charsetToCheck = "ms932";
821
if (versionMeetsMinimum(5, 0, 3)
822
|| versionMeetsMinimum(4, 1, 11)) {
823
charsetToCheck = "windows-31j";
826
assertEquals(charsetToCheck,
827
((com.mysql.jdbc.ResultSetMetaData) this.rs
828
.getMetaData()).getColumnCharacterSet(1)
829
.toLowerCase(Locale.ENGLISH));
832
ms932Conn.createStatement().executeUpdate(
833
"drop table if exists testBug7607");
837
"create table testBug7607 (sortCol int, col1 varchar(100) ) character set sjis");
838
ms932Conn.createStatement().executeUpdate(
839
"insert into testBug7607 values(1, 0x835C)"); // standard
841
ms932Conn.createStatement().executeUpdate(
842
"insert into testBug7607 values(2, 0x878A)"); // NEC
848
"SELECT col1 FROM testBug7607 ORDER BY sortCol ASC");
849
assertTrue(this.rs.next());
850
String asString = this.rs.getString(1);
851
assertTrue("\u30bd".equals(asString));
853
// Can't be fixed unless server is fixed,
854
// this is fixed in 4.1.7.
856
assertTrue(this.rs.next());
857
asString = this.rs.getString(1);
858
assertEquals("\u3231", asString);
860
ms932Conn.createStatement().executeUpdate(
861
"drop table if exists testBug7607");
864
props = new Properties();
865
props.setProperty("characterEncoding", "SHIFT_JIS");
867
shiftJisConn = getConnectionWithProps(props);
869
this.rs = shiftJisConn.createStatement().executeQuery(
870
"SHOW VARIABLES LIKE 'character_set_client'");
871
assertTrue(this.rs.next());
872
encoding = this.rs.getString(2);
873
assertTrue("sjis".equalsIgnoreCase(encoding));
875
this.rs = shiftJisConn.createStatement().executeQuery(
877
assertTrue(this.rs.next());
879
String charSetUC = ((com.mysql.jdbc.ResultSetMetaData) this.rs
880
.getMetaData()).getColumnCharacterSet(1).toUpperCase(
883
if (isRunningOnJdk131()) {
884
assertEquals("WINDOWS-31J", charSetUC);
886
// assertEquals("SHIFT_JIS", charSetUC);
889
props = new Properties();
890
props.setProperty("characterEncoding", "WINDOWS-31J");
892
windows31JConn = getConnectionWithProps(props);
894
this.rs = windows31JConn.createStatement().executeQuery(
895
"SHOW VARIABLES LIKE 'character_set_client'");
896
assertTrue(this.rs.next());
897
encoding = this.rs.getString(2);
899
if (!versionMeetsMinimum(5, 0, 3)
900
&& !versionMeetsMinimum(4, 1, 11)) {
901
assertEquals("sjis", encoding.toLowerCase(Locale.ENGLISH));
903
assertEquals("cp932", encoding.toLowerCase(Locale.ENGLISH));
906
this.rs = windows31JConn.createStatement().executeQuery(
908
assertTrue(this.rs.next());
910
if (!versionMeetsMinimum(4, 1, 11)) {
911
assertEquals("sjis".toLowerCase(Locale.ENGLISH),
912
((com.mysql.jdbc.ResultSetMetaData) this.rs
913
.getMetaData()).getColumnCharacterSet(1)
914
.toLowerCase(Locale.ENGLISH));
916
assertEquals("windows-31j".toLowerCase(Locale.ENGLISH),
917
((com.mysql.jdbc.ResultSetMetaData) this.rs
918
.getMetaData()).getColumnCharacterSet(1)
919
.toLowerCase(Locale.ENGLISH));
922
props = new Properties();
923
props.setProperty("characterEncoding", "CP943");
925
cp943Conn = getConnectionWithProps(props);
927
this.rs = cp943Conn.createStatement().executeQuery(
928
"SHOW VARIABLES LIKE 'character_set_client'");
929
assertTrue(this.rs.next());
930
encoding = this.rs.getString(2);
931
assertTrue("sjis".equalsIgnoreCase(encoding));
933
this.rs = cp943Conn.createStatement().executeQuery(
935
assertTrue(this.rs.next());
937
charSetUC = ((com.mysql.jdbc.ResultSetMetaData) this.rs
938
.getMetaData()).getColumnCharacterSet(1).toUpperCase(
941
if (isRunningOnJdk131()) {
942
assertEquals("WINDOWS-31J", charSetUC);
944
assertEquals("CP943", charSetUC);
948
if (ms932Conn != null) {
952
if (shiftJisConn != null) {
953
shiftJisConn.close();
956
if (windows31JConn != null) {
957
windows31JConn.close();
960
if (cp943Conn != null) {
968
* In some case Connector/J's round-robin function doesn't work.
970
* I had 2 mysqld, node1 "localhost:3306" and node2 "localhost:3307".
972
* 1. node1 is up, node2 is up
974
* 2. java-program connect to node1 by using properties
975
* "autoRecconect=true","roundRobinLoadBalance=true","failOverReadOnly=false".
977
* 3. node1 is down, node2 is up
979
* 4. java-program execute a query and fail, but Connector/J's round-robin
980
* fashion failover work and if java-program retry a query it can succeed
981
* (connection is change to node2 by Connector/j)
983
* 5. node1 is up, node2 is up
985
* 6. node1 is up, node2 is down
987
* 7. java-program execute a query, but this time Connector/J doesn't work
988
* althought node1 is up and usable.
993
public void testBug8643() throws Exception {
994
if (runMultiHostTests()) {
995
Properties defaultProps = getMasterSlaveProps();
997
defaultProps.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
998
defaultProps.remove(NonRegisteringDriver.PORT_PROPERTY_KEY);
1000
defaultProps.put("autoReconnect", "true");
1001
defaultProps.put("roundRobinLoadBalance", "true");
1002
defaultProps.put("failOverReadOnly", "false");
1004
Connection con = null;
1006
con = DriverManager.getConnection(getMasterSlaveUrl(),
1008
Statement stmt1 = con.createStatement();
1010
ResultSet rs1 = stmt1
1011
.executeQuery("show variables like 'port'");
1014
rs1 = stmt1.executeQuery("select connection_id()");
1016
String originalConnectionId = rs1.getString(1);
1017
this.stmt.executeUpdate("kill " + originalConnectionId);
1021
SQLException caughtException = null;
1023
while (caughtException == null && numLoops > 0) {
1027
rs1 = stmt1.executeQuery("show variables like 'port'");
1028
} catch (SQLException sqlEx) {
1029
caughtException = sqlEx;
1033
assertNotNull(caughtException);
1035
// failover and retry
1036
rs1 = stmt1.executeQuery("show variables like 'port'");
1039
assertTrue(!((com.mysql.jdbc.Connection) con)
1040
.isMasterConnection());
1042
rs1 = stmt1.executeQuery("select connection_id()");
1044
String nextConnectionId = rs1.getString(1);
1045
assertTrue(!nextConnectionId.equals(originalConnectionId));
1047
this.stmt.executeUpdate("kill " + nextConnectionId);
1051
caughtException = null;
1053
while (caughtException == null && numLoops > 0) {
1057
rs1 = stmt1.executeQuery("show variables like 'port'");
1058
} catch (SQLException sqlEx) {
1059
caughtException = sqlEx;
1063
assertNotNull(caughtException);
1065
// failover and retry
1066
rs1 = stmt1.executeQuery("show variables like 'port'");
1069
assertTrue(((com.mysql.jdbc.Connection) con)
1070
.isMasterConnection());
1076
} catch (Exception e) {
1077
e.printStackTrace();
1085
* Tests fix for BUG#9206, can not use 'UTF-8' for characterSetResults
1086
* configuration property.
1088
public void testBug9206() throws Exception {
1089
Properties props = new Properties();
1090
props.setProperty("characterSetResults", "UTF-8");
1091
getConnectionWithProps(props).close();
1095
* These two charsets have different names depending on version of MySQL
1099
* if the test fails.
1101
public void testNewCharsetsConfiguration() throws Exception {
1102
Properties props = new Properties();
1103
props.setProperty("useUnicode", "true");
1104
props.setProperty("characterEncoding", "EUC_KR");
1105
getConnectionWithProps(props).close();
1107
props = new Properties();
1108
props.setProperty("useUnicode", "true");
1109
props.setProperty("characterEncoding", "KOI8_R");
1110
getConnectionWithProps(props).close();
1114
* Tests fix for BUG#10144 - Memory leak in ServerPreparedStatement if
1115
* serverPrepare() fails.
1118
public void testBug10144() throws Exception {
1119
if (versionMeetsMinimum(4, 1)) {
1120
Properties props = new Properties();
1121
props.setProperty("emulateUnsupportedPstmts", "false");
1122
props.setProperty("useServerPrepStmts", "true");
1124
Connection bareConn = getConnectionWithProps(props);
1126
int currentOpenStatements = ((com.mysql.jdbc.Connection) bareConn)
1127
.getActiveStatementCount();
1130
bareConn.prepareStatement("Boo!");
1131
fail("Should not've been able to prepare that one!");
1132
} catch (SQLException sqlEx) {
1133
assertEquals(currentOpenStatements,
1134
((com.mysql.jdbc.Connection) bareConn)
1135
.getActiveStatementCount());
1137
if (bareConn != null) {
1145
* Tests fix for BUG#10496 - SQLException is thrown when using property
1146
* "characterSetResults"
1148
public void testBug10496() throws Exception {
1149
if (versionMeetsMinimum(5, 0, 3)) {
1150
Properties props = new Properties();
1151
props.setProperty("useUnicode", "true");
1152
props.setProperty("characterEncoding", "WINDOWS-31J");
1153
props.setProperty("characterSetResults", "WINDOWS-31J");
1154
getConnectionWithProps(props).close();
1156
props = new Properties();
1157
props.setProperty("useUnicode", "true");
1158
props.setProperty("characterEncoding", "EUC_JP");
1159
props.setProperty("characterSetResults", "EUC_JP");
1160
getConnectionWithProps(props).close();
1165
* Tests fix for BUG#11259, autoReconnect ping causes exception on
1166
* connection startup.
1169
* if the test fails.
1171
public void testBug11259() throws Exception {
1172
Connection dsConn = null;
1174
Properties props = new Properties();
1175
props.setProperty("autoReconnect", "true");
1176
dsConn = getConnectionWithProps(props);
1178
if (dsConn != null) {
1185
* Tests fix for BUG#11879 -- ReplicationConnection won't switch to slave,
1186
* throws "Catalog can't be null" exception.
1191
public void testBug11879() throws Exception {
1192
if (runMultiHostTests()) {
1193
Connection replConn = null;
1196
replConn = getMasterSlaveReplicationConnection();
1197
replConn.setReadOnly(true);
1198
replConn.setReadOnly(false);
1200
if (replConn != null) {
1208
* Tests fix for BUG#11976 - maxPerformance.properties mis-spells
1209
* "elideSetAutoCommits".
1212
* if the test fails.
1214
public void testBug11976() throws Exception {
1215
if (isRunningOnJdk131()) {
1216
return; // test not valid on JDK-1.3.1
1219
Properties props = new Properties();
1220
props.setProperty("useConfigs", "maxPerformance");
1222
Connection maxPerfConn = getConnectionWithProps(props);
1223
assertEquals(true, ((com.mysql.jdbc.Connection) maxPerfConn)
1224
.getElideSetAutoCommits());
1228
* Tests fix for BUG#12218, properties shared between master and slave with
1229
* replication connection.
1232
* if the test fails.
1234
public void testBug12218() throws Exception {
1235
if (runMultiHostTests()) {
1236
Connection replConn = null;
1239
replConn = getMasterSlaveReplicationConnection();
1240
assertTrue(!((ReplicationConnection) replConn)
1241
.getMasterConnection().hasSameProperties(
1242
((ReplicationConnection) replConn)
1243
.getSlavesConnection()));
1245
if (replConn != null) {
1253
* Tests fix for BUG#12229 - explainSlowQueries hangs with server-side
1254
* prepared statements.
1257
* if the test fails.
1259
public void testBug12229() throws Exception {
1260
createTable("testBug12229", "(`int_field` integer )");
1261
this.stmt.executeUpdate("insert into testBug12229 values (123456),(1)");
1263
Properties props = new Properties();
1264
props.put("profileSQL", "true");
1265
props.put("slowQueryThresholdMillis", "0");
1266
props.put("logSlowQueries", "true");
1267
props.put("explainSlowQueries", "true");
1268
props.put("useServerPrepStmts", "true");
1270
Connection explainConn = getConnectionWithProps(props);
1272
this.pstmt = explainConn
1273
.prepareStatement("SELECT `int_field` FROM `testBug12229` WHERE `int_field` = ?");
1274
this.pstmt.setInt(1, 1);
1276
this.rs = this.pstmt.executeQuery();
1277
assertTrue(this.rs.next());
1279
this.rs = this.pstmt.executeQuery();
1280
assertTrue(this.rs.next());
1282
this.rs = this.pstmt.executeQuery();
1283
assertTrue(this.rs.next());
1287
* Tests fix for BUG#12752 - Cp1251 incorrectly mapped to win1251 for
1288
* servers newer than 4.0.x.
1291
* if the test fails.
1293
public void testBug12752() throws Exception {
1294
Properties props = new Properties();
1295
props.setProperty("characterEncoding", "Cp1251");
1296
getConnectionWithProps(props).close();
1300
* Tests fix for BUG#12753, sessionVariables=....=...., doesn't work as it's
1301
* tokenized incorrectly.
1304
* if the test fails.
1306
public void testBug12753() throws Exception {
1307
if (versionMeetsMinimum(4, 1)) {
1308
Properties props = new Properties();
1309
props.setProperty("sessionVariables", "sql_mode=ansi");
1311
Connection sessionConn = null;
1314
sessionConn = getConnectionWithProps(props);
1316
String sqlMode = getMysqlVariable(sessionConn, "sql_mode");
1317
assertTrue(sqlMode.indexOf("ANSI") != -1);
1319
if (sessionConn != null) {
1320
sessionConn.close();
1328
* Tests fix for BUG#13048 - maxQuerySizeToLog is not respected.
1333
public void testBug13048() throws Exception {
1335
Connection profileConn = null;
1336
PrintStream oldErr = System.err;
1339
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
1340
System.setErr(new PrintStream(bOut));
1342
Properties props = new Properties();
1343
props.setProperty("profileSQL", "true");
1344
props.setProperty("maxQuerySizeToLog", "2");
1345
props.setProperty("logger", "com.mysql.jdbc.log.StandardLogger");
1347
profileConn = getConnectionWithProps(props);
1349
StringBuffer queryBuf = new StringBuffer("SELECT '");
1351
for (int i = 0; i < 500; i++) {
1352
queryBuf.append("a");
1355
queryBuf.append("'");
1357
this.rs = profileConn.createStatement().executeQuery(
1358
queryBuf.toString());
1361
String logString = new String(bOut.toString("ISO8859-1"));
1362
assertTrue(logString.indexOf("... (truncated)") != -1);
1364
bOut = new ByteArrayOutputStream();
1365
System.setErr(new PrintStream(bOut));
1367
this.rs = profileConn.prepareStatement(queryBuf.toString())
1369
logString = new String(bOut.toString("ISO8859-1"));
1371
assertTrue(logString.indexOf("... (truncated)") != -1);
1373
System.setErr(oldErr);
1375
if (profileConn != null) {
1376
profileConn.close();
1379
if (this.rs != null) {
1380
ResultSet toClose = this.rs;
1388
* Tests fix for BUG#13453 - can't use & or = in URL configuration values
1389
* (we now allow you to use www-form-encoding).
1394
public void testBug13453() throws Exception {
1395
StringBuffer urlBuf = new StringBuffer(dbUrl);
1397
if (dbUrl.indexOf('?') == -1) {
1403
urlBuf.append("sessionVariables=@testBug13453='%25%26+%3D'");
1405
Connection encodedConn = null;
1408
encodedConn = DriverManager.getConnection(urlBuf.toString(), null);
1410
this.rs = encodedConn.createStatement().executeQuery(
1411
"SELECT @testBug13453");
1412
assertTrue(this.rs.next());
1413
assertEquals("%& =", this.rs.getString(1));
1415
if (this.rs != null) {
1420
if (encodedConn != null) {
1421
encodedConn.close();
1427
* Tests fix for BUG#15065 - Usage advisor complains about unreferenced
1428
* columns, even though they've been referenced.
1431
* if the test fails.
1433
public void testBug15065() throws Exception {
1434
if (isRunningOnJdk131()) {
1435
return; // test not valid on JDK-1.3.1
1438
createTable("testBug15065", "(field1 int)");
1440
this.stmt.executeUpdate("INSERT INTO testBug15065 VALUES (1)");
1442
Connection advisorConn = null;
1443
Statement advisorStmt = null;
1446
Properties props = new Properties();
1447
props.setProperty("useUsageAdvisor", "true");
1448
props.setProperty("logger", "com.mysql.jdbc.log.StandardLogger");
1450
advisorConn = getConnectionWithProps(props);
1451
advisorStmt = advisorConn.createStatement();
1453
Method[] getMethods = ResultSet.class.getMethods();
1455
PrintStream oldErr = System.err;
1458
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
1459
System.setErr(new PrintStream(bOut));
1461
HashMap methodsToSkipMap = new HashMap();
1463
// Needs an actual URL
1464
methodsToSkipMap.put("getURL", null);
1466
// Java6 JDBC4.0 methods we don't implement
1467
methodsToSkipMap.put("getNCharacterStream", null);
1468
methodsToSkipMap.put("getNClob", null);
1469
methodsToSkipMap.put("getNString", null);
1470
methodsToSkipMap.put("getRowId", null);
1471
methodsToSkipMap.put("getSQLXML", null);
1473
for (int j = 0; j < 2; j++) {
1474
for (int i = 0; i < getMethods.length; i++) {
1475
String methodName = getMethods[i].getName();
1477
if (methodName.startsWith("get")
1478
&& !methodsToSkipMap.containsKey(methodName)) {
1479
Class[] parameterTypes = getMethods[i]
1480
.getParameterTypes();
1482
if (parameterTypes.length == 1
1483
&& parameterTypes[0] == Integer.TYPE) {
1485
this.rs = advisorStmt
1486
.executeQuery("SELECT COUNT(*) FROM testBug15065");
1488
this.rs = advisorConn
1490
"SELECT COUNT(*) FROM testBug15065")
1498
getMethods[i].invoke(this.rs,
1499
new Object[] { new Integer(1) });
1500
} catch (InvocationTargetException invokeEx) {
1501
// we don't care about bad values, just that
1503
// column gets "touched"
1508
java.sql.SQLException.class)
1514
"com.mysql.jdbc.NotImplemented")) {
1526
String logOut = bOut.toString("ISO8859-1");
1528
if (logOut.indexOf(".Level") != -1) {
1529
return; // we ignore for warnings
1532
assertTrue("Usage advisor complained about columns:\n\n"
1533
+ logOut, logOut.indexOf("columns") == -1);
1535
System.setErr(oldErr);
1538
if (advisorConn != null) {
1539
advisorConn.close();
1546
* Tests fix for BUG#15544, no "dos" character set in MySQL > 4.1.0
1551
public void testBug15544() throws Exception {
1552
Properties props = new Properties();
1553
props.setProperty("characterEncoding", "Cp437");
1554
Connection dosConn = null;
1557
dosConn = getConnectionWithProps(props);
1559
if (dosConn != null) {
1565
public void testCSC5765() throws Exception {
1566
if (isRunningOnJdk131()) {
1567
return; // test not valid on JDK-1.3.1
1570
Properties props = new Properties();
1571
props.setProperty("useUnicode", "true");
1572
props.setProperty("characterEncoding", "utf8");
1573
props.setProperty("characterSetResults", "utf8");
1574
props.setProperty("connectionCollation", "utf8_bin");
1576
Connection utf8Conn = null;
1579
utf8Conn = getConnectionWithProps(props);
1580
this.rs = utf8Conn.createStatement().executeQuery(
1581
"SHOW VARIABLES LIKE 'character_%'");
1582
while (this.rs.next()) {
1583
System.out.println(this.rs.getString(1) + " = "
1584
+ this.rs.getString(2));
1587
this.rs = utf8Conn.createStatement().executeQuery(
1588
"SHOW VARIABLES LIKE 'collation_%'");
1589
while (this.rs.next()) {
1590
System.out.println(this.rs.getString(1) + " = "
1591
+ this.rs.getString(2));
1594
if (utf8Conn != null) {
1600
private Connection getMasterSlaveReplicationConnection()
1601
throws SQLException {
1603
Connection replConn = new ReplicationDriver().connect(
1604
getMasterSlaveUrl(), getMasterSlaveProps());
1609
protected String getMasterSlaveUrl() throws SQLException {
1610
StringBuffer urlBuf = new StringBuffer("jdbc:mysql://");
1611
Properties defaultProps = getPropertiesFromTestsuiteUrl();
1612
String hostname = defaultProps
1613
.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
1615
int colonIndex = hostname.indexOf(":");
1617
String portNumber = "3306";
1619
if (colonIndex != -1 && !hostname.startsWith(":")) {
1620
portNumber = hostname.substring(colonIndex + 1);
1621
hostname = hostname.substring(0, colonIndex);
1622
} else if (hostname.startsWith(":")) {
1623
portNumber = hostname.substring(1);
1624
hostname = "localhost";
1626
portNumber = defaultProps
1627
.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);
1630
for (int i = 0; i < 2; i++) {
1631
urlBuf.append(hostname);
1633
urlBuf.append(portNumber);
1641
return urlBuf.toString();
1644
protected Properties getMasterSlaveProps() throws SQLException {
1645
Properties props = getPropertiesFromTestsuiteUrl();
1647
props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
1648
props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY);
1654
* Tests fix for BUG#15570 - ReplicationConnection incorrectly copies state,
1655
* doesn't transfer connection context correctly when transitioning between
1656
* the same read-only states.
1658
* (note, this test will fail if the test user doesn't have permission to
1662
* if the test fails.
1664
public void testBug15570() throws Exception {
1665
Connection replConn = null;
1668
replConn = getMasterSlaveReplicationConnection();
1670
int masterConnectionId = Integer
1671
.parseInt(getSingleIndexedValueWithQuery(replConn, 1,
1672
"SELECT CONNECTION_ID()").toString());
1674
replConn.setReadOnly(false);
1676
assertEquals(masterConnectionId, Integer
1677
.parseInt(getSingleIndexedValueWithQuery(replConn, 1,
1678
"SELECT CONNECTION_ID()").toString()));
1680
String currentCatalog = replConn.getCatalog();
1682
replConn.setCatalog(currentCatalog);
1683
assertEquals(currentCatalog, replConn.getCatalog());
1685
replConn.setReadOnly(true);
1687
int slaveConnectionId = Integer
1688
.parseInt(getSingleIndexedValueWithQuery(replConn, 1,
1689
"SELECT CONNECTION_ID()").toString());
1691
// The following test is okay for now, as the chance
1692
// of MySQL wrapping the connection id counter during our
1693
// testsuite is very small.
1695
assertTrue("Slave id " + slaveConnectionId
1696
+ " is not newer than master id " + masterConnectionId,
1697
slaveConnectionId > masterConnectionId);
1699
assertEquals(currentCatalog, replConn.getCatalog());
1701
String newCatalog = "mysql";
1703
replConn.setCatalog(newCatalog);
1704
assertEquals(newCatalog, replConn.getCatalog());
1706
replConn.setReadOnly(true);
1707
assertEquals(newCatalog, replConn.getCatalog());
1709
replConn.setReadOnly(false);
1710
assertEquals(masterConnectionId, Integer
1711
.parseInt(getSingleIndexedValueWithQuery(replConn, 1,
1712
"SELECT CONNECTION_ID()").toString()));
1714
if (replConn != null) {
1721
* Tests bug where downed slave caused round robin load balance not to
1722
* cycle back to first host in the list.
1725
* if the test fails...Note, test is timing-dependent, but
1726
* should work in most cases.
1728
public void testBug23281() throws Exception {
1729
Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
1730
props.setProperty("autoReconnect", "false");
1731
props.setProperty("roundRobinLoadBalance", "true");
1732
props.setProperty("failoverReadOnly", "false");
1734
if (!isRunningOnJdk131()) {
1735
props.setProperty("connectTimeout", "5000");
1738
// Re-build the connection information
1739
int firstIndexOfHost = BaseTestCase.dbUrl.indexOf("//") + 2;
1740
int lastIndexOfHost = BaseTestCase.dbUrl.indexOf("/", firstIndexOfHost);
1742
String hostPortPair = BaseTestCase.dbUrl.substring(firstIndexOfHost,
1745
StringTokenizer st = new StringTokenizer(hostPortPair, ":");
1750
if (st.hasMoreTokens()) {
1751
String possibleHostOrPort = st.nextToken();
1753
if (Character.isDigit(possibleHostOrPort.charAt(0)) &&
1754
(possibleHostOrPort.indexOf(".") == -1 /* IPV4 */) &&
1755
(possibleHostOrPort.indexOf("::") == -1 /* IPV6 */)) {
1756
port = possibleHostOrPort;
1759
host = possibleHostOrPort;
1763
if (st.hasMoreTokens()) {
1764
port = st.nextToken();
1775
StringBuffer newHostBuf = new StringBuffer();
1777
newHostBuf.append(host);
1779
newHostBuf.append(":");
1780
newHostBuf.append(port);
1783
newHostBuf.append(",");
1784
//newHostBuf.append(host);
1785
newHostBuf.append("192.0.2.1"); // non-exsitent machine from RFC3330 test network
1786
newHostBuf.append(":65532"); // make sure the slave fails
1788
props.remove("PORT");
1789
props.remove("HOST");
1791
Connection failoverConnection = null;
1794
failoverConnection = getConnectionWithProps("jdbc:mysql://"
1795
+ newHostBuf.toString() + "/", props);
1797
String originalConnectionId = getSingleIndexedValueWithQuery(
1798
failoverConnection, 1, "SELECT CONNECTION_ID()").toString();
1800
System.out.println(originalConnectionId);
1802
Connection nextConnection = getConnectionWithProps("jdbc:mysql://"
1803
+ newHostBuf.toString() + "/", props);
1805
String nextId = getSingleIndexedValueWithQuery(
1806
nextConnection, 1, "SELECT CONNECTION_ID()").toString();
1808
System.out.println(nextId);
1811
if (failoverConnection != null) {
1812
failoverConnection.close();
1818
* Tests to insure proper behavior for BUG#24706.
1820
* @throws Exception if the test fails.
1822
public void testBug24706() throws Exception {
1823
if (!versionMeetsMinimum(5, 0)) {
1824
return; // server status isn't there to support this feature
1827
Properties props = new Properties();
1828
props.setProperty("elideSetAutoCommits", "true");
1829
props.setProperty("logger", "StandardLogger");
1830
props.setProperty("profileSQL", "true");
1831
Connection c = null;
1833
StringBuffer logBuf = new StringBuffer();
1835
StandardLogger.bufferedLog = logBuf;
1838
c = getConnectionWithProps(props);
1839
c.setAutoCommit(true);
1840
c.createStatement().execute("SELECT 1");
1841
c.setAutoCommit(true);
1842
c.setAutoCommit(false);
1843
c.createStatement().execute("SELECT 1");
1844
c.setAutoCommit(false);
1846
// We should only see _one_ "set autocommit=" sent to the server
1848
String log = logBuf.toString();
1853
while ((found = log.indexOf("SET autocommit=", searchFrom)) != -1) {
1854
searchFrom = found + 1;
1858
// The SELECT doesn't actually start a transaction, so being pedantic the
1859
// driver issues SET autocommit=0 again in this case.
1860
assertEquals(2, count);
1862
StandardLogger.bufferedLog = null;
1872
* Tests fix for BUG#25514 - Timer instance used for Statement.setQueryTimeout()
1873
* created per-connection, rather than per-VM, causing memory leak.
1875
* @throws Exception if the test fails.
1877
public void testBug25514() throws Exception {
1879
for (int i = 0; i < 10; i++) {
1880
getConnectionWithProps(null).close();
1883
ThreadGroup root = Thread.currentThread().getThreadGroup().getParent();
1885
while (root.getParent() != null) {
1886
root = root.getParent();
1889
int numThreadsNamedTimer = findNamedThreadCount(root, "Timer");
1891
if (numThreadsNamedTimer == 0) {
1892
numThreadsNamedTimer = findNamedThreadCount(root, "MySQL Statement Cancellation Timer");
1895
// Notice that this seems impossible to test on JDKs prior to 1.5, as there is no
1896
// reliable way to find the TimerThread, so we have to rely on new JDKs for this
1898
assertTrue("More than one timer for cancel was created", numThreadsNamedTimer <= 1);
1901
private int findNamedThreadCount(ThreadGroup group, String nameStart) {
1905
int numThreads = group.activeCount();
1906
Thread[] threads = new Thread[numThreads*2];
1907
numThreads = group.enumerate(threads, false);
1909
for (int i=0; i<numThreads; i++) {
1910
if (threads[i].getName().startsWith(nameStart)) {
1915
int numGroups = group.activeGroupCount();
1916
ThreadGroup[] groups = new ThreadGroup[numGroups*2];
1917
numGroups = group.enumerate(groups, false);
1919
for (int i=0; i<numGroups; i++) {
1920
count += findNamedThreadCount(groups[i], nameStart);
1927
* Ensures that we don't miss getters/setters for driver properties in
1928
* ConnectionProperties so that names given in documentation work with
1929
* DataSources which will use JavaBean-style names and reflection to
1930
* set the values (and often fail silently! when the method isn't available).
1934
public void testBug23626() throws Exception {
1935
Class clazz = this.conn.getClass();
1937
DriverPropertyInfo[] dpi = new NonRegisteringDriver().getPropertyInfo(dbUrl, null);
1938
StringBuffer missingSettersBuf = new StringBuffer();
1939
StringBuffer missingGettersBuf = new StringBuffer();
1941
Class[][] argTypes = {new Class[] { String.class }, new Class[] {Integer.TYPE}, new Class[] {Long.TYPE}, new Class[] {Boolean.TYPE}};
1943
for (int i = 0; i < dpi.length; i++) {
1945
String propertyName = dpi[i].name;
1947
if (propertyName.equals("HOST") || propertyName.equals("PORT")
1948
|| propertyName.equals("DBNAME") || propertyName.equals("user") ||
1949
propertyName.equals("password")) {
1953
StringBuffer mutatorName = new StringBuffer("set");
1954
mutatorName.append(Character.toUpperCase(propertyName.charAt(0)));
1955
mutatorName.append(propertyName.substring(1));
1957
StringBuffer accessorName = new StringBuffer("get");
1958
accessorName.append(Character.toUpperCase(propertyName.charAt(0)));
1959
accessorName.append(propertyName.substring(1));
1962
clazz.getMethod(accessorName.toString(), null);
1963
} catch (NoSuchMethodException nsme) {
1964
missingGettersBuf.append(accessorName.toString());
1965
missingGettersBuf.append("\n");
1968
boolean foundMethod = false;
1970
for (int j = 0; j < argTypes.length; j++) {
1972
clazz.getMethod(mutatorName.toString(), argTypes[j]);
1975
} catch (NoSuchMethodException nsme) {
1981
missingSettersBuf.append(mutatorName);
1982
missingSettersBuf.append("\n");
1986
assertEquals("Missing setters for listed configuration properties.", "", missingSettersBuf.toString());
1987
assertEquals("Missing getters for listed configuration properties.", "", missingSettersBuf.toString());
1991
* Tests fix for BUG#25545 - Client flags not sent correctly during handshake
1994
* Requires test certificates from testsuite/ssl-test-certs to be installed
1995
* on the server being tested.
1997
* @throws Exception if the test fails.
1999
public void testBug25545() throws Exception {
2000
if (!versionMeetsMinimum(5, 0)) {
2004
if (isRunningOnJdk131()) {
2008
createProcedure("testBug25545", "() BEGIN SELECT 1; END");
2010
String trustStorePath = "src/testsuite/ssl-test-certs/test-cert-store";
2012
System.setProperty("javax.net.ssl.keyStore", trustStorePath);
2013
System.setProperty("javax.net.ssl.keyStorePassword","password");
2014
System.setProperty("javax.net.ssl.trustStore", trustStorePath);
2015
System.setProperty("javax.net.ssl.trustStorePassword","password");
2018
Connection sslConn = null;
2021
Properties props = new Properties();
2022
props.setProperty("useSSL", "true");
2023
props.setProperty("requireSSL", "true");
2025
sslConn = getConnectionWithProps(props);
2026
sslConn.prepareCall("{ call testBug25545()}").execute();
2028
if (sslConn != null) {
2035
* Tests fix for BUG#27655 - getTransactionIsolation() uses
2036
* "SHOW VARIABLES LIKE" which is very inefficient on MySQL-5.0+
2040
public void testBug27655() throws Exception {
2041
StringBuffer logBuf = new StringBuffer();
2042
Properties props = new Properties();
2043
props.setProperty("profileSQL", "true");
2044
props.setProperty("logger", "StandardLogger");
2045
StandardLogger.bufferedLog = logBuf;
2047
Connection loggedConn = null;
2050
loggedConn = getConnectionWithProps(props);
2051
loggedConn.getTransactionIsolation();
2053
if (versionMeetsMinimum(4, 0, 3)) {
2054
assertEquals(-1, logBuf.toString().indexOf("SHOW VARIABLES LIKE 'tx_isolation'"));
2057
if (loggedConn != null) {
2064
* Tests fix for issue where a failed-over connection would let
2065
* an application call setReadOnly(false), when that call
2066
* should be ignored until the connection is reconnected to a
2069
* @throws Exception if the test fails.
2071
public void testFailoverReadOnly() throws Exception {
2072
Properties props = getMasterSlaveProps();
2073
props.setProperty("autoReconnect", "true");
2076
Connection failoverConn = null;
2079
Statement failoverStmt =
2083
failoverConn = getConnectionWithProps(getMasterSlaveUrl(), props);
2085
((com.mysql.jdbc.Connection)failoverConn).setPreferSlaveDuringFailover(true);
2087
failoverStmt = failoverConn.createStatement();
2089
String masterConnectionId = getSingleIndexedValueWithQuery(failoverConn, 1, "SELECT connection_id()").toString();
2092
this.stmt.execute("KILL " + masterConnectionId);
2094
// die trying, so we get the next host
2095
for (int i = 0; i < 100; i++) {
2097
failoverStmt.executeQuery("SELECT 1");
2098
} catch (SQLException sqlEx) {
2103
String slaveConnectionId = getSingleIndexedValueWithQuery(failoverConn, 1, "SELECT connection_id()").toString();
2105
assertTrue("Didn't get a new physical connection",
2106
!masterConnectionId.equals(slaveConnectionId));
2108
failoverConn.setReadOnly(false); // this should be ignored
2110
assertTrue(failoverConn.isReadOnly());
2112
((com.mysql.jdbc.Connection)failoverConn).setPreferSlaveDuringFailover(false);
2114
this.stmt.execute("KILL " + slaveConnectionId); // we can't issue this on our own connection :p
2116
// die trying, so we get the next host
2117
for (int i = 0; i < 100; i++) {
2119
failoverStmt.executeQuery("SELECT 1");
2120
} catch (SQLException sqlEx) {
2125
String newMasterId = getSingleIndexedValueWithQuery(failoverConn, 1, "SELECT connection_id()").toString();
2127
assertTrue("Didn't get a new physical connection",
2128
!slaveConnectionId.equals(newMasterId));
2130
failoverConn.setReadOnly(false);
2132
assertTrue(!failoverConn.isReadOnly());
2134
if (failoverStmt != null) {
2135
failoverStmt.close();
2138
if (failoverConn != null) {
2139
failoverConn.close();
b'\\ No newline at end of file'