【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了ConnectionTest.java提供大家参考,希望对大家有所帮助!
/*
Copyright (C) 2002-2004 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
There are special exceptions to the terms and conditions of the GPL
as it is applied to this software. View the full text of the
exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
software distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package testsuite.simple;
import testsuite.BaseTestCase;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
import java.util.Properties;
import java.util.StringTokenizer;
import com.mysql.jdbc.ConnectionPropertiesTransform;
import com.mysql.jdbc.NonRegisteringDriver;
import com.mysql.jdbc.SQLError;
import com.
mysql.jdbc.StringUtils;
import com.my
sql.jdbc.log.StandardLogger;
/**
* Tests java.sql.Connection functionality ConnectionTest.java,v 1.1 2002/12/06
* 22:01:05 mmatthew Exp
*
* @author Mark Matthews
*/
public class ConnectionTest extends BaseTestCase {
/**
* Constructor for ConnectionTest.
*
* @param name
* the name of the test to run
*/
public ConnectionTest(String name) {
super(name);
}
/**
* Runs all test cases in this test suite
*
* @param args
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(ConnectionTest.class);
}
/**
* Tests catalog functionality
*
* @throws Exception
* if an error occurs
*/
public void testCatalog() throws Exception {
String currentCatalog = this.conn.getCatalog();
this.conn.setCatalog(currentCatalog);
assertTrue(currentCatalog.equals(this.conn.getCatalog()));
}
/**
* Tests a cluster connection for failover, requires a two-node cluster URL
* specfied in com.mysql.jdbc.testsuite.ClusterUrl system proeprty.
*
* @throws Exception
* DOCUMENT ME!
*/
public void testClusterConnection() throws Exception {
String url = System.getProperty("com.mysql.jdbc.testsuite.ClusterUrl");
if ((url != null) && (url.length() > 0)) {
Object versionNumObj = getSingleValueWithQuery("SHOW VARIABLES LIKE 'version'");
if ((versionNumObj != null)
&& (versionNumObj.toString().indexOf("cluster") != -1)) {
Connection clusterConn = null;
Statement clusterStmt = null;
try {
clusterConn = new NonRegisteringDriver().connect(url, null);
clusterStmt = clusterConn.createStatement();
clusterStmt
.executeQuery("DROP TABLE IF EXISTS testClusterConn");
clusterStmt
.executeQuery("CREATE TABLE testClusterConn (field1 INT) TYPE=ndbcluster");
clusterStmt
.executeQuery("INSERT INTO testClusterConn VALUES (1)");
clusterConn.setAutoCommit(false);
clusterStmt.executeQuery("SELECT * FROM testClusterConn");
clusterStmt
.executeUpdate("UPDATE testClusterConn SET field1=4");
// Kill the connection
String connectionId = getSingleValueWithQuery(
"SELECT CONNECTION_ID()").toString();
System.out
.println("Please kill the MySQL server now and press return...");
System.in.read();
System.out.println("Waiting for TCP/IP timeout...");
Thread.sleep(10);
System.out.println("Attempting auto reconnect");
try {
clusterConn.setAutoCommit(true);
clusterConn.setAutoCommit(false);
} catch (SQLException sqlEx) {
System.out.println(sqlEx);
}
//
// Test that this 'new' connection is not read-only
//
clusterStmt
.executeUpdate("UPDATE testClusterConn SET field1=5");
ResultSet rs = clusterStmt
.executeQuery("SELECT * FROM testClusterConn WHERE field1=5");
assertTrue("One row should be returned", rs.next());
} finally {
if (clusterStmt != null) {
clusterStmt
.executeQuery("D