Modified:
trunk/jdbc-4-0-examples/src/examples/cex2007/ExceptionsSamples.java
trunk/jdbc-4-0-examples/src/examples/cex2007/WrapUnwrapSamples.java
trunk/jdbc-4-0-examples/src/examples/cex2007/XMLSamples.java
Log:
Added copyright notices, more comments.
Modified: trunk/jdbc-4-0-examples/src/examples/cex2007/ExceptionsSamples.java
===================================================================
--- trunk/jdbc-4-0-examples/src/examples/cex2007/ExceptionsSamples.java 2007-04-23 21:40:24 UTC (rev 6407)
+++ trunk/jdbc-4-0-examples/src/examples/cex2007/ExceptionsSamples.java 2007-04-23 21:54:31 UTC (rev 6408)
@@ -1,3 +1,25 @@
+/*
+ Copyright (C) 2007 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 examples.cex2007;
import java.sql.Connection;
@@ -7,27 +29,53 @@
import testsuite.BaseTestCase;
+/**
+ * Examples of JDBC-4.0 exception-related functionality.
+ *
+ * @version $Id: $
+ */
public class ExceptionsSamples extends BaseTestCase {
public ExceptionsSamples(String name) {
super(name);
}
+ /*
+ * Shows how to use the JDBC-4.0 method isValid() to determine
+ * if a connection is valid or not.
+ */
public void testIsValid() throws Exception {
+ /*
+ * This just gets a new connection for us to work with
+ */
Connection connToTest = getConnectionWithProps((String)null);
+ /*
+ * Send a "ping" to MySQL, driver times out if no response
+ * within 5 seconds.
+ */
assertTrue(connToTest.isValid(5));
- String mysqlThreadNumber = getSingleIndexedValueWithQuery(connToTest, 1, "SELECT connection_id()").toString();
+ /*
+ * Figure out which thread to kill (ourselves)
+ */
+ String mysqlThreadNumber = getSingleIndexedValueWithQuery(connToTest, 1,
+ "SELECT connection_id()").toString();
this.stmt.executeQuery("KILL " + mysqlThreadNumber);
+ /*
+ * Wait until TCP/IP notices the connection is dead...
+ */
try {
dieTrying(connToTest);
} catch (SQLException sqlEx) {
// ignore
}
+ /*
+ * Test again, with a "ping".
+ */
assertFalse(connToTest.isValid(1));
}
@@ -44,12 +92,16 @@
PreparedStatement pstmt = null;
try {
- String mysqlThreadNumber = getSingleIndexedValueWithQuery(c, 1, "SELECT connection_id()").toString();
+ String mysqlThreadNumber = getSingleIndexedValueWithQuery(c, 1,
+ "SELECT connection_id()").toString();
c.setAutoCommit(false);
- pstmt = c.prepareStatement("INSERT INTO recoverableExample VALUES (?)");
+ pstmt = c.prepareStatement(
+ "INSERT INTO recoverableExample VALUES (?)");
pstmt.setInt(1, 1);
- // Simulate a failure
+ /*
+ * Simulate a failure on the first time through
+ */
if (numRetries == 2) {
this.stmt.executeQuery("KILL " + mysqlThreadNumber);
@@ -63,7 +115,10 @@
break;
} catch (SQLRecoverableException recoverableEx) {
- // Log it?
+ /*
+ * A "real" application would probably log this
+ */
+
continue;
} finally {
if (!commited && !c.isClosed()) {
@@ -78,7 +133,9 @@
}
}
- // Check that we actually got the row inserted
+ /*
+ * Check that we actually got the row inserted
+ */
assertEquals(1, getRowCount("recoverableExample"));
}
Modified: trunk/jdbc-4-0-examples/src/examples/cex2007/WrapUnwrapSamples.java
===================================================================
--- trunk/jdbc-4-0-examples/src/examples/cex2007/WrapUnwrapSamples.java 2007-04-23 21:40:24 UTC (rev 6407)
+++ trunk/jdbc-4-0-examples/src/examples/cex2007/WrapUnwrapSamples.java 2007-04-23 21:54:31 UTC (rev 6408)
@@ -1,8 +1,34 @@
+/*
+ Copyright (C) 2007 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 examples.cex2007;
import java.sql.Connection;
+import java.sql.SQLException;
import java.sql.Statement;
+import javax.sql.ConnectionEvent;
+import javax.sql.ConnectionEventListener;
import javax.sql.PooledConnection;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
@@ -15,14 +41,46 @@
super(name);
}
+ /**
+ * Demonstrates standardized access to vendor extensions through
+ * the java.sql.Wrapper interface.
+ *
+ * @throws Exception
+ */
public void testUnwrapOfPooledConnection() throws Exception {
+ /*
+ * Creates a connection pool datasource (notice that this is
+ * _not_ a connection pool, only a source for connections
+ * that can be pooled (and fire events on errors to interested
+ * listeners)
+ */
MysqlConnectionPoolDataSource cpds = new MysqlConnectionPoolDataSource();
cpds.setURL(dbUrl);
PooledConnection pooledConn = cpds.getPooledConnection();
+ pooledConn.addConnectionEventListener(new ConnectionEventListener() {
+ public void connectionClosed(ConnectionEvent event) {
+ System.out.println("Connection closed event: " + event.toString());
+ }
+
+ public void connectionErrorOccurred(ConnectionEvent event) {
+ System.out.println("Connection error event: " +
+ event.toString() + "\n\n");
+ event.getSQLException().printStackTrace();
+ }
+ });
+
+ /*
+ * This is a "wrapped" physical connection (in nearly every
+ * implementation I've seen).
+ */
Connection logicalConnection = pooledConn.getConnection();
+ /*
+ * Is the instance an inteface _of_ or wrapper _for_
+ * java.sql.Connection?
+ */
assertTrue(logicalConnection.isWrapperFor(
java.sql.Connection.class));
@@ -35,13 +93,45 @@
com.mysql.jdbc.Connection unwrappedConn = (com.mysql.jdbc.Connection)
logicalConnection.unwrap(com.mysql.jdbc.Connection.class);
- // Notice you get the same _instance_ if you unwrap again for the
- // same interface
+ /*
+ * Notice you get the same _instance_ if you unwrap again for the
+ * same interface (for performance reasons)
+ */
assertSame(unwrappedConn,
logicalConnection.unwrap(com.mysql.jdbc.Connection.class));
+ /*
+ * Here we call a method that's _not_ part of java.sql.Connection,
+ * but _is_ in com.mysql.jdbc.Connection
+ */
+ unwrappedConn.setStatementComment("I've been unwrapped!");
- unwrappedConn.setStatementComment("I've been unwrapped!");
- Statement stmt = unwrappedConn.createStatement();
- System.out.println(stmt);
+ /*
+ * Let's kill the connection, and then run an unwrapped method
+ * on a Statement that we get from an _unwrapped_ Connection.
+ */
+ Statement unwrappedStmt = unwrappedConn.createStatement();
+
+ String connectionId = getSingleIndexedValueWithQuery(
+ unwrappedConn, 1,
+ "SELECT CONNECTION_ID()").toString();
+
+ try {
+ unwrappedStmt.execute("KILL " + connectionId);
+ } catch (SQLException sqlEx) {
+ // ignore, we're expecting it
+ }
+
+ /*
+ * Depending on timing, the above statement might die,
+ * _or_ we won't notice connection death until the
+ * TCP/IP stack notices it...
+ */
+ for (;;) {
+ try {
+ unwrappedStmt.executeQuery("SELECT 1");
+ } catch (SQLException sqlEx) {
+ break;
+ }
+ }
}
-}
+}
\ No newline at end of file
Modified: trunk/jdbc-4-0-examples/src/examples/cex2007/XMLSamples.java
===================================================================
--- trunk/jdbc-4-0-examples/src/examples/cex2007/XMLSamples.java 2007-04-23 21:40:24 UTC (rev 6407)
+++ trunk/jdbc-4-0-examples/src/examples/cex2007/XMLSamples.java 2007-04-23 21:54:31 UTC (rev 6408)
@@ -1,3 +1,26 @@
+/*
+ Copyright (C) 2007 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 examples.cex2007;
import java.io.BufferedInputStream;
| Thread |
|---|
| • Connector/J commit: r6408 - trunk/jdbc-4-0-examples/src/examples/cex2007 | mmatthews | 23 Apr |