1 25 package testsuite.simple; 26 27 import testsuite.BaseTestCase; 28 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.FileWriter ; 32 import java.sql.Connection ; 33 import java.sql.DatabaseMetaData ; 34 import java.sql.PreparedStatement ; 35 import java.sql.ResultSet ; 36 import java.sql.ResultSetMetaData ; 37 import java.sql.SQLException ; 38 import java.sql.Savepoint ; 39 import java.sql.Statement ; 40 41 import java.util.Properties ; 42 import java.util.StringTokenizer ; 43 44 import com.mysql.jdbc.ConnectionPropertiesTransform; 45 import com.mysql.jdbc.NonRegisteringDriver; 46 import com.mysql.jdbc.SQLError; 47 import com.mysql.jdbc.StringUtils; 48 import com.mysql.jdbc.log.StandardLogger; 49 50 56 public class ConnectionTest extends BaseTestCase { 57 63 public ConnectionTest(String name) { 64 super(name); 65 } 66 67 72 public static void main(String [] args) { 73 junit.textui.TestRunner.run(ConnectionTest.class); 74 } 75 76 82 public void testCatalog() throws Exception { 83 String currentCatalog = this.conn.getCatalog(); 84 this.conn.setCatalog(currentCatalog); 85 assertTrue(currentCatalog.equals(this.conn.getCatalog())); 86 } 87 88 95 public void testClusterConnection() throws Exception { 96 String url = System.getProperty("com.mysql.jdbc.testsuite.ClusterUrl"); 97 98 if ((url != null) && (url.length() > 0)) { 99 Object versionNumObj = getSingleValueWithQuery("SHOW VARIABLES LIKE 'version'"); 100 101 if ((versionNumObj != null) 102 && (versionNumObj.toString().indexOf("cluster") != -1)) { 103 Connection clusterConn = null; 104 Statement clusterStmt = null; 105 106 try { 107 clusterConn = new NonRegisteringDriver().connect(url, null); 108 109 clusterStmt = clusterConn.createStatement(); 110 clusterStmt 111 .executeQuery("DROP TABLE IF EXISTS testClusterConn"); 112 clusterStmt 113 .executeQuery("CREATE TABLE testClusterConn (field1 INT) TYPE=ndbcluster"); 114 clusterStmt 115 .executeQuery("INSERT INTO testClusterConn VALUES (1)"); 116 117 clusterConn.setAutoCommit(false); 118 119 clusterStmt.executeQuery("SELECT * FROM testClusterConn"); 120 clusterStmt 121 .executeUpdate("UPDATE testClusterConn SET field1=4"); 122 123 String connectionId = getSingleValueWithQuery( 125 "SELECT CONNECTION_ID()").toString(); 126 127 System.out 128 .println("Please kill the MySQL server now and press return..."); 129 System.in.read(); 130 131 System.out.println("Waiting for TCP/IP timeout..."); 132 Thread.sleep(10); 133 134 System.out.println("Attempting auto reconnect"); 135 136 try { 137 clusterConn.setAutoCommit(true); 138 clusterConn.setAutoCommit(false); 139 } catch (SQLException sqlEx) { 140 System.out.println(sqlEx); 141 } 142 143 clusterStmt 147 .executeUpdate("UPDATE testClusterConn SET field1=5"); 148 149 ResultSet rs = clusterStmt 150 .executeQuery("SELECT * FROM testClusterConn WHERE field1=5"); 151 152 assertTrue("One row should be returned", rs.next()); 153 } finally { 154 if (clusterStmt != null) { 155 clusterStmt 156 .executeQuery("DROP TABLE IF EXISTS testClusterConn"); 157 clusterStmt.close(); 158 } 159 160 if (clusterConn != null) { 161 clusterConn.close(); 162 } 163 } 164 } 165 } 166 } 167 168 174 public void testDeadlockDetection() throws Exception { 175 try { 176 this.rs = this.stmt 177 .executeQuery("SHOW VARIABLES LIKE 'innodb_lock_wait_timeout'"); 178 this.rs.next(); 179 180 int timeoutSecs = this.rs.getInt(2); 181 182 this.stmt.executeUpdate("DROP TABLE IF EXISTS t1"); 183 this.stmt 184 .executeUpdate("CREATE TABLE t1 (id INTEGER, x INTEGER) TYPE=INNODB"); 185 this.stmt.executeUpdate("INSERT INTO t1 VALUES(0, 0)"); 186 this.conn.setAutoCommit(false); 187 this.conn.createStatement().executeQuery( 188 "SELECT * FROM t1 WHERE id=0 FOR UPDATE"); 189 190 Connection deadlockConn = getConnectionWithProps(new Properties ()); 191 deadlockConn.setAutoCommit(false); 192 193 deadlockConn.createStatement().executeUpdate( 195 "UPDATE t1 SET x=2 WHERE id=0"); 196 deadlockConn.commit(); 197 198 Thread.sleep(timeoutSecs * 2 * 1000); 199 } catch (SQLException sqlEx) { 200 System.out 201 .println("Caught SQLException due to deadlock/lock timeout"); 202 System.out.println("SQLState: " + sqlEx.getSQLState()); 203 System.out.println("Vendor error: " + sqlEx.getErrorCode()); 204 System.out.println("Message: " + sqlEx.getMessage()); 205 206 assertTrue(SQLError.SQL_STATE_DEADLOCK.equals(sqlEx.getSQLState())); 210 assertTrue(sqlEx.getErrorCode() == 1205); 211 } finally { 212 this.conn.setAutoCommit(true); 213 this.stmt.executeUpdate("DROP TABLE IF EXISTS t1"); 214 } 215 } 216 217 223 public void testCharsets() throws Exception { 224 if (versionMeetsMinimum(4, 1)) { 225 try { 226 Properties props = new Properties (); 227 props.setProperty("useUnicode", "true"); 228 props.setProperty("characterEncoding", "UTF-8"); 229 230 Connection utfConn = getConnectionWithProps(props); 231 232 this.stmt = utfConn.createStatement(); 233 234 this.stmt.executeUpdate("DROP TABLE IF EXISTS t1"); 235 237 this.stmt.executeUpdate("CREATE TABLE t1 (" 238 + "comment CHAR(32) ASCII NOT NULL," 239 + "koi8_ru_f CHAR(32) CHARACTER SET koi8r NOT NULL" 240 + ") CHARSET=latin5"); 241 242 this.stmt 243 .executeUpdate("ALTER TABLE t1 CHANGE comment comment CHAR(32) CHARACTER SET latin2 NOT NULL"); 244 this.stmt 245 .executeUpdate("ALTER TABLE t1 ADD latin5_f CHAR(32) NOT NULL"); 246 this.stmt.executeUpdate("ALTER TABLE t1 CHARSET=latin2"); 247 this.stmt 248 .executeUpdate("ALTER TABLE t1 ADD latin2_f CHAR(32) NOT NULL"); 249 this.stmt 250 .executeUpdate("ALTER TABLE t1 DROP latin2_f, DROP latin5_f"); 251 252 this.stmt 253 .executeUpdate("INSERT INTO t1 (koi8_ru_f,comment) VALUES ('a','LAT SMALL A')"); 254 333 334 String cyrillicSmallA = "\u0430"; 335 this.stmt 336 .executeUpdate("INSERT INTO t1 (koi8_ru_f,comment) VALUES ('" 337 + cyrillicSmallA + "','CYR SMALL A')"); 338 339 467 468 this.stmt 469 .executeUpdate("ALTER TABLE t1 ADD utf8_f CHAR(32) CHARACTER SET utf8 NOT NULL"); 470 this.stmt 471 .executeUpdate("UPDATE t1 SET utf8_f=CONVERT(koi8_ru_f USING utf8)"); 472 this.stmt.executeUpdate("SET CHARACTER SET koi8r"); 473 this.rs = this.stmt.executeQuery("SELECT * FROM t1"); 475 476 ResultSetMetaData rsmd = this.rs.getMetaData(); 477 478 int numColumns = rsmd.getColumnCount(); 479 480 for (int i = 0; i < numColumns; i++) { 481 System.out.print(rsmd.getColumnName(i + 1)); 482 System.out.print("\t\t"); 483 } 484 485 System.out.println(); 486 487 while (this.rs.next()) { 488 System.out.println(this.rs.getString(1) + "\t\t" 489 + this.rs.getString(2) + "\t\t" 490 + this.rs.getString(3)); 491 492 if (this.rs.getString(1).equals("CYR SMALL A")) { 493 this.rs.getString(2); 494 } 495 } 496 497 System.out.println(); 498 499 this.stmt.executeUpdate("SET NAMES utf8"); 500 this.rs = this.stmt.executeQuery("SELECT _koi8r 0xC1;"); 501 502 rsmd = this.rs.getMetaData(); 503 504 numColumns = rsmd.getColumnCount(); 505 506 for (int i = 0; i < numColumns; i++) { 507 System.out.print(rsmd.getColumnName(i + 1)); 508 System.out.print("\t\t"); 509 } 510 511 System.out.println(); 512 513 while (this.rs.next()) { 514 System.out.println(this.rs.getString(1).equals("\u0430") 515 + "\t\t"); 516 System.out 517 .println(new String (this.rs.getBytes(1), "KOI8_R")); 518 519 } 520 521 char[] c = new char[] { 0xd0b0 }; 522 523 System.out.println(new String (c)); 524 System.out.println("\u0430"); 525 } finally { 526 } 528 } 529 } 530 531 537 public void testIsolationLevel() throws Exception { 538 if (versionMeetsMinimum(4, 0)) { 539 String [] isoLevelNames = new String [] { 540 "Connection.TRANSACTION_NONE", 541 "Connection.TRANSACTION_READ_COMMITTED", 542 "Connection.TRANSACTION_READ_UNCOMMITTED", 543 "Connection.TRANSACTION_REPEATABLE_READ", 544 "Connection.TRANSACTION_SERIALIZABLE" }; 545 546 int[] isolationLevels = new int[] { Connection.TRANSACTION_NONE, 547 Connection.TRANSACTION_READ_COMMITTED, 548 Connection.TRANSACTION_READ_UNCOMMITTED, 549 Connection.TRANSACTION_REPEATABLE_READ, 550 Connection.TRANSACTION_SERIALIZABLE }; 551 552 DatabaseMetaData dbmd = this.conn.getMetaData(); 553 554 for (int i = 0; i < isolationLevels.length; i++) { 555 if (dbmd.supportsTransactionIsolationLevel(isolationLevels[i])) { 556 this.conn.setTransactionIsolation(isolationLevels[i]); 557 558 assertTrue( 559 "Transaction isolation level that was set (" 560 + isoLevelNames[i] 561 + ") was not returned, nor was a more restrictive isolation level used by the server", 562 this.conn.getTransactionIsolation() == isolationLevels[i] 563 || this.conn.getTransactionIsolation() > isolationLevels[i]); 564 } 565 } 566 } 567 } 568 569 575 public void testSavepoint() throws Exception { 576 DatabaseMetaData dbmd = this.conn.getMetaData(); 577 578 if (dbmd.supportsSavepoints()) { 579 System.out.println("Testing SAVEPOINTs"); 580 581 try { 582 this.conn.setAutoCommit(true); 583 584 this.stmt.executeUpdate("DROP TABLE IF EXISTS testSavepoints"); 585 this.stmt 586 .executeUpdate("CREATE TABLE testSavepoints (field1 int) TYPE=InnoDB"); 587 588 this.conn.setAutoCommit(false); 590 this.stmt 591 .executeUpdate("INSERT INTO testSavepoints VALUES (1)"); 592 593 Savepoint afterInsert = this.conn.setSavepoint("afterInsert"); 594 this.stmt.executeUpdate("UPDATE testSavepoints SET field1=2"); 595 596 Savepoint afterUpdate = this.conn.setSavepoint("afterUpdate"); 597 this.stmt.executeUpdate("DELETE FROM testSavepoints"); 598 599 assertTrue("Row count should be 0", 600 getRowCount("testSavepoints") == 0); 601 this.conn.rollback(afterUpdate); 602 assertTrue("Row count should be 1", 603 getRowCount("testSavepoints") == 1); 604 assertTrue("Value should be 2", "2".equals(getSingleValue( 605 "testSavepoints", "field1", null).toString())); 606 this.conn.rollback(afterInsert); 607 assertTrue("Value should be 1", "1".equals(getSingleValue( 608 "testSavepoints", "field1", null).toString())); 609 this.conn.rollback(); 610 assertTrue("Row count should be 0", 611 getRowCount("testSavepoints") == 0); 612 613 this.conn.rollback(); 615 616 this.stmt 617 .executeUpdate("INSERT INTO testSavepoints VALUES (1)"); 618 afterInsert = this.conn.setSavepoint(); 619 this.stmt.executeUpdate("UPDATE testSavepoints SET field1=2"); 620 afterUpdate = this.conn.setSavepoint(); 621 this.stmt.executeUpdate("DELETE FROM testSavepoints"); 622 623 assertTrue("Row count should be 0", 624 getRowCount("testSavepoints") == 0); 625 this.conn.rollback(afterUpdate); 626 assertTrue("Row count should be 1", 627 getRowCount("testSavepoints") == 1); 628 assertTrue("Value should be 2", "2".equals(getSingleValue( 629 "testSavepoints", "field1", null).toString())); 630 this.conn.rollback(afterInsert); 631 assertTrue("Value should be 1", "1".equals(getSingleValue( 632 "testSavepoints", "field1", null).toString())); 633 this.conn.rollback(); 634 635 this.conn.releaseSavepoint(this.conn.setSavepoint()); 636 } finally { 637 this.conn.setAutoCommit(true); 638 this.stmt.executeUpdate("DROP TABLE IF EXISTS testSavepoints"); 639 } 640 } else { 641 System.out.println("MySQL version does not support SAVEPOINTs"); 642 } 643 } 644 645 651 public void testNonStandardConnectionCollation() throws Exception { 652 if (versionMeetsMinimum(4, 1)) { 653 String collationToSet = "utf8_bin"; 654 String characterSet = "utf8"; 655 656 Properties props = new Properties (); 657 props.setProperty("connectionCollation", collationToSet); 658 props.setProperty("characterEncoding", characterSet); 659 660 Connection collConn = null; 661 Statement collStmt = null; 662 ResultSet collRs = null; 663 664 try { 665 collConn = getConnectionWithProps(props); 666 667 collStmt = collConn.createStatement(); 668 669 collRs = collStmt 670 .executeQuery("SHOW VARIABLES LIKE 'collation_connection'"); 671 672 assertTrue(collRs.next()); 673 assertTrue(collationToSet.equalsIgnoreCase(collRs.getString(2))); 674 } finally { 675 if (collConn != null) { 676 collConn.close(); 677 } 678 } 679 } 680 } 681 682 public void testDumpQueriesOnException() throws Exception { 683 Properties props = new Properties (); 684 props.setProperty("dumpQueriesOnException", "true"); 685 String bogusSQL = "SELECT 1 TO BAZ"; 686 Connection dumpConn = getConnectionWithProps(props); 687 688 try { 689 dumpConn.createStatement().executeQuery(bogusSQL); 690 } catch (SQLException sqlEx) { 691 assertTrue(sqlEx.getMessage().indexOf(bogusSQL) != -1); 692 } 693 694 try { 695 ((com.mysql.jdbc.Connection) dumpConn).clientPrepareStatement( 696 bogusSQL).executeQuery(); 697 } catch (SQLException sqlEx) { 698 assertTrue(sqlEx.getMessage().indexOf(bogusSQL) != -1); 699 } 700 701 try { 702 this.stmt 703 .executeUpdate("DROP TABLE IF EXISTS testDumpQueriesOnException"); 704 this.stmt 705 .executeUpdate("CREATE TABLE testDumpQueriesOnException (field1 int UNIQUE)"); 706 this.stmt 707 .executeUpdate("INSERT INTO testDumpQueriesOnException VALUES (1)"); 708 709 PreparedStatement pStmt = dumpConn 710 .prepareStatement("INSERT INTO testDumpQueriesOnException VALUES (?)"); 711 pStmt.setInt(1, 1); 712 pStmt.executeUpdate(); 713 } catch (SQLException sqlEx) { 714 assertTrue(sqlEx.getMessage().indexOf( 715 "INSERT INTO testDumpQueriesOnException") != -1); 716 } finally { 717 this.stmt 718 .executeUpdate("DROP TABLE IF EXISTS testDumpQueriesOnException"); 719 } 720 721 try { 722 dumpConn.prepareStatement(bogusSQL); 723 } catch (SQLException sqlEx) { 724 assertTrue(sqlEx.getMessage().indexOf(bogusSQL) != -1); 725 } 726 } 727 728 734 public void testConnectionPropertiesTransform() throws Exception { 735 String transformClassName = SimpleTransformer.class.getName(); 736 737 Properties props = new Properties (); 738 739 props.setProperty(NonRegisteringDriver.PROPERTIES_TRANSFORM_KEY, 740 transformClassName); 741 742 NonRegisteringDriver driver = new NonRegisteringDriver(); 743 744 Properties transformedProps = driver 745 .parseURL(BaseTestCase.dbUrl, props); 746 747 assertTrue("albequerque".equals(transformedProps 748 .getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY))); 749 } 750 751 757 public void testLocalInfileWithUrl() throws Exception { 758 File infile = File.createTempFile("foo", "txt"); 759 infile.deleteOnExit(); 760 String url = infile.toURL().toExternalForm(); 761 FileWriter output = new FileWriter (infile); 762 output.write("Test"); 763 output.flush(); 764 output.close(); 765 766 try { 767 this.stmt 768 .executeUpdate("DROP TABLE IF EXISTS testLocalInfileWithUrl"); 769 this.stmt 770 .executeUpdate("CREATE TABLE testLocalInfileWithUrl (field1 LONGTEXT)"); 771 772 Properties props = new Properties (); 773 props.setProperty("allowUrlInLocalInfile", "true"); 774 775 Connection loadConn = getConnectionWithProps(props); 776 Statement loadStmt = loadConn.createStatement(); 777 778 try { 779 loadStmt.executeQuery("LOAD DATA LOCAL INFILE '" + url 780 + "' INTO TABLE testLocalInfileWithUrl"); 781 } catch (SQLException sqlEx) { 782 sqlEx.printStackTrace(); 783 784 throw sqlEx; 785 } 786 787 this.rs = this.stmt 788 .executeQuery("SELECT * FROM testLocalInfileWithUrl"); 789 assertTrue(this.rs.next()); 790 assertTrue("Test".equals(this.rs.getString(1))); 791 int count = this.stmt 792 .executeUpdate("DELETE FROM testLocalInfileWithUrl"); 793 assertTrue(count == 1); 794 795 StringBuffer escapedPath = new StringBuffer (); 796 String path = infile.getCanonicalPath(); 797 798 for (int i = 0; i < path.length(); i++) { 799 char c = path.charAt(i); 800 801 if (c == '\\') { 802 escapedPath.append('\\'); 803 } 804 805 escapedPath.append(c); 806 } 807 808 loadStmt.executeQuery("LOAD DATA LOCAL INFILE '" 809 + escapedPath.toString() 810 + "' INTO TABLE testLocalInfileWithUrl"); 811 this.rs = this.stmt 812 .executeQuery("SELECT * FROM testLocalInfileWithUrl"); 813 assertTrue(this.rs.next()); 814 assertTrue("Test".equals(this.rs.getString(1))); 815 816 try { 817 loadStmt 818 .executeQuery("LOAD DATA LOCAL INFILE 'foo:///' INTO TABLE testLocalInfileWithUrl"); 819 } catch (SQLException sqlEx) { 820 assertTrue(sqlEx.getMessage() != null); 821 assertTrue(sqlEx.getMessage().indexOf("FileNotFoundException") != -1); 822 } 823 824 } finally { 825 this.stmt 826 .executeUpdate("DROP TABLE IF EXISTS testLocalInfileWithUrl"); 827 } 828 } 829 830 public void testServerConfigurationCache() throws Exception { 831 Properties props = new Properties (); 832 833 props.setProperty("cacheServerConfiguration", "true"); 834 props.setProperty("profileSQL", "true"); 835 props.setProperty("logFactory", "com.mysql.jdbc.log.StandardLogger"); 836 837 Connection conn1 = getConnectionWithProps(props); 838 839 StandardLogger.saveLogsToBuffer(); 840 841 Connection conn2 = getConnectionWithProps(props); 842 843 assertTrue("Configuration wasn't cached", StandardLogger.bufferedLog 844 .toString().indexOf("SHOW VARIABLES") == -1); 845 846 if (versionMeetsMinimum(4, 1)) { 847 assertTrue("Configuration wasn't cached", 848 StandardLogger.bufferedLog.toString().indexOf( 849 "SHOW COLLATION") == -1); 850 851 } 852 } 853 854 862 public void testUseLocalSessionState() throws Exception { 863 Properties props = new Properties (); 864 865 props.setProperty("useLocalSessionState", "true"); 866 props.setProperty("profileSQL", "true"); 867 props.setProperty("logFactory", "com.mysql.jdbc.log.StandardLogger"); 868 869 Connection conn1 = getConnectionWithProps(props); 870 conn1.setAutoCommit(true); 871 conn1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); 872 873 StandardLogger.saveLogsToBuffer(); 874 StandardLogger.bufferedLog.setLength(0); 875 876 conn1.setAutoCommit(true); 877 conn1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); 878 conn1.getTransactionIsolation(); 879 880 String logAsString = StandardLogger.bufferedLog.toString(); 881 882 assertTrue(logAsString.indexOf("SET SESSION") == -1 883 && logAsString.indexOf("SHOW VARIABLES LIKE 'tx_isolation'") == -1 884 && logAsString.indexOf("SET autocommit=") == -1); 885 886 } 887 888 894 public void testFailoverConnection() throws Exception { 895 896 Properties props = new Properties (); 897 props.setProperty("autoReconnect", "true"); 898 props.setProperty("failOverReadOnly", "false"); 899 900 int firstIndexOfHost = BaseTestCase.dbUrl.indexOf("//") + 2; 902 int lastIndexOfHost = BaseTestCase.dbUrl.indexOf("/", firstIndexOfHost); 903 904 String hostPortPair = BaseTestCase.dbUrl.substring(firstIndexOfHost, 905 lastIndexOfHost); 906 System.out.println(hostPortPair); 907 908 StringTokenizer st = new StringTokenizer (hostPortPair, ":"); 909 910 String host = null; 911 String port = null; 912 913 if (st.hasMoreTokens()) { 914 String possibleHostOrPort = st.nextToken(); 915 916 if (Character.isDigit(possibleHostOrPort.charAt(0))) { 917 port = possibleHostOrPort; 918 host = "localhost"; 919 } else { 920 host = possibleHostOrPort; 921 } 922 } 923 924 if (host == null) { 925 host = "localhost"; 926 } 927 928 if (st.hasMoreTokens()) { 929 port = st.nextToken(); 930 } 931 932 StringBuffer newHostBuf = new StringBuffer (); 933 newHostBuf.append(host); 934 if (port != null) { 935 newHostBuf.append(":"); 936 newHostBuf.append(port); 937 } 938 newHostBuf.append(","); 939 newHostBuf.append(host); 940 if (port != null) { 941 newHostBuf.append(":"); 942 newHostBuf.append(port); 943 } 944 945 props 946 .put(NonRegisteringDriver.HOST_PROPERTY_KEY, newHostBuf 947 .toString()); 948 949 Connection failoverConnection = null; 950 951 try { 952 failoverConnection = getConnectionWithProps(props); 953 954 String originalConnectionId = getSingleIndexedValueWithQuery( 955 failoverConnection, 1, "SELECT connection_id()").toString(); 956 System.out.println("Original Connection Id = " 957 + originalConnectionId); 958 959 assertTrue("Connection should not be in READ_ONLY state", 960 !failoverConnection.isReadOnly()); 961 962 this.stmt.executeUpdate("KILL " + originalConnectionId); 964 965 967 Thread.sleep(3000); 968 969 try { 970 failoverConnection.createStatement().executeQuery("SELECT 1"); 971 fail("We expect an exception here, because the connection should be gone until the reconnect code picks it up again"); 972 } catch (SQLException sqlEx) { 973 ; } 975 976 978 failoverConnection.setAutoCommit(true); 979 980 String newConnectionId = getSingleIndexedValueWithQuery( 981 failoverConnection, 1, "SELECT connection_id()").toString(); 982 System.out.println("new Connection Id = " + newConnectionId); 983 984 assertTrue( 985 "We should have a new connection to the server in this case", 986 !newConnectionId.equals(originalConnectionId)); 987 assertTrue("Connection should not be read-only", 988 !failoverConnection.isReadOnly()); 989 } finally { 990 if (failoverConnection != null) { 991 failoverConnection.close(); 992 } 993 } 994 995 } 996 997 public void testCannedConfigs() throws Exception { 998 String url = "jdbc:mysql:///?useConfigs=clusterBase"; 999 1000 Properties cannedProps = new NonRegisteringDriver().parseURL(url, null); 1001 1002 assertTrue("true".equals(cannedProps.getProperty("autoReconnect"))); 1003 assertTrue("false".equals(cannedProps.getProperty("failOverReadOnly"))); 1004 assertTrue("true".equals(cannedProps 1005 .getProperty("roundRobinLoadBalance"))); 1006 1007 url = "jdbc:mysql:///?useConfigs=clusterBase,clusterBase2"; 1009 1010 try { 1011 cannedProps = new NonRegisteringDriver().parseURL(url, null); 1012 fail("should've bailed on that one!"); 1013 } catch (SQLException sqlEx) { 1014 assertTrue(SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE 1015 .equals(sqlEx.getSQLState())); 1016 } 1017 } 1018 1019 public void testUseOldUTF8Behavior() throws Exception { 1020 1021 Properties props = new Properties (); 1022 props.setProperty("useOldUTF8Behavior", "true"); 1023 props.setProperty("useUnicode", "true"); 1024 props.setProperty("characterEncoding", "UTF-8"); 1025 props.setProperty("logFactory", "com.mysql.jdbc.log.StandardLogger"); 1026 props.setProperty("profileSQL", "true"); 1027 StandardLogger.saveLogsToBuffer(); 1028 StandardLogger.bufferedLog.setLength(0); 1029 1030 try { 1031 getConnectionWithProps(props); 1032 1033 assertTrue(StringUtils.indexOfIgnoreCase(StandardLogger.bufferedLog 1034 .toString(), "SET NAMES utf8") == -1); 1035 } finally { 1036 StandardLogger.bufferedLog = null; 1037 } 1038 } 1039 1040 1046 public void testDontTrackOpenResources() throws Exception { 1047 Properties props = new Properties (); 1048 1049 props.setProperty("dontTrackOpenResources", "true"); 1050 Connection noTrackConn = null; 1051 Statement noTrackStatement = null; 1052 PreparedStatement noTrackPstmt = null; 1053 ResultSet rs2 = null; 1054 1055 try { 1056 noTrackConn = getConnectionWithProps(props); 1057 noTrackStatement = noTrackConn.createStatement(); 1058 noTrackPstmt = noTrackConn.prepareStatement("SELECT 1"); 1059 rs2 = noTrackPstmt.executeQuery(); 1060 rs2.next(); 1061 1062 this.rs = noTrackStatement.executeQuery("SELECT 1"); 1063 this.rs.next(); 1064 1065 noTrackConn.close(); 1066 1067 1070 this.rs.getString(1); 1071 rs2.getString(1); 1072 } finally { 1073 if (rs2 != null) { 1074 rs2.close(); 1075 } 1076 1077 if (noTrackStatement != null) { 1078 noTrackStatement.close(); 1079 } 1080 1081 if (noTrackConn != null & !noTrackConn.isClosed()) { 1082 noTrackConn.close(); 1083 } 1084 } 1085 } 1086 1087 public void testPing() throws SQLException { 1088 Connection conn2 = getConnectionWithProps(null); 1089 1090 ((com.mysql.jdbc.Connection) conn2).ping(); 1091 conn2.close(); 1092 1093 try { 1094 ((com.mysql.jdbc.Connection) conn2).ping(); 1095 fail("Should have failed with an exception"); 1096 } catch (SQLException sqlEx) { 1097 } 1099 1100 1103 Properties props = new Properties (); 1104 props.setProperty("autoReconnect", "true"); 1105 1106 getConnectionWithProps(props); 1107 } 1108 1109 public void testSessionVariables() throws Exception { 1110 String getInitialMaxAllowedPacket = getMysqlVariable("max_allowed_packet"); 1111 1112 int newMaxAllowedPacket = Integer.parseInt(getInitialMaxAllowedPacket) + 1024; 1113 1114 Properties props = new Properties (); 1115 props.setProperty("sessionVariables", "max_allowed_packet=" 1116 + newMaxAllowedPacket); 1117 props.setProperty("profileSQL", "true"); 1118 1119 Connection varConn = getConnectionWithProps(props); 1120 1121 assertTrue(!getInitialMaxAllowedPacket.equals(getMysqlVariable(varConn, 1122 "max_allowed_packet"))); 1123 } 1124 1125 1131 public void testSetProfileSql() throws Exception { 1132 ((com.mysql.jdbc.Connection) this.conn).setProfileSql(false); 1133 stmt.executeQuery("SELECT 1"); 1134 ((com.mysql.jdbc.Connection) this.conn).setProfileSql(true); 1135 stmt.executeQuery("SELECT 1"); 1136 } 1137 1138 public void testCreateDatabaseIfNotExist() throws Exception { 1139 if (isAdminConnectionConfigured()) { 1140 Properties props = new Properties (); 1141 props.setProperty("createDatabaseIfNotExist", "true"); 1142 props.setProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY, 1143 "testcreatedatabaseifnotexists"); 1144 1145 Connection newConn = getAdminConnectionWithProps(props); 1146 newConn.createStatement().executeUpdate( 1147 "DROP DATABASE testcreatedatabaseifnotexists"); 1148 } 1149 } 1150} 1151 | Popular Tags |