db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r399604 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/jdbc/ testing/org/apache/derbyTesting/functionTests/tests/jdbc4/
Date Thu, 04 May 2006 08:17:01 GMT
Author: kahatlen
Date: Thu May  4 01:17:00 2006
New Revision: 399604

URL: http://svn.apache.org/viewcvs?rev=399604&view=rev
Log:
DERBY-1234 (partial) Make the methods in EmbedConnection raise an
exception when the connection is closed.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection40.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ClosedObjectTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ConnectionTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java?rev=399604&r1=399603&r2=399604&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java Thu May
 4 01:17:00 2006
@@ -517,8 +517,7 @@
     								 int resultSetHoldability)
 						throws SQLException
 	{
-		if (isClosed())
-			throw Util.noCurrentConnection();
+		checkIfClosed();
 
 		return factory.newEmbedStatement(this, false,
 			setResultSetType(resultSetType), resultSetConcurrency,
@@ -795,8 +794,7 @@
 				 int resultSetConcurrency, int resultSetHoldability)
 		throws SQLException 
 	{
-		if (SanityManager.DEBUG)
-			SanityManager.ASSERT(!isClosed(), "connection is closed");
+		checkIfClosed();
 
 		synchronized (getConnectionSynchronization())
 		{
@@ -824,7 +822,8 @@
      * parameter placeholders
      * @return the native form of this statement
      */
-    public String nativeSQL(String sql) {
+    public String nativeSQL(String sql) throws SQLException {
+        checkIfClosed();
 		// we don't massage the strings at all, so this is easy:
 		return sql;
 	}
@@ -851,6 +850,7 @@
      * @exception SQLException if a database-access error occurs.
      */
 	public void setAutoCommit(boolean autoCommit) throws SQLException {
+		checkIfClosed();
 
 		// Is this a nested connection
 		if (rootConnection != this) {
@@ -870,7 +870,8 @@
      * @return Current state of auto-commit mode.
      * @see #setAutoCommit 
      */
-    public boolean getAutoCommit() {
+    public boolean getAutoCommit() throws SQLException {
+        checkIfClosed();
 		return autoCommit;
 	}
 
@@ -1043,8 +1044,7 @@
      * @exception SQLException if a database-access error occurs.
      */
     public DatabaseMetaData getMetaData() throws SQLException {
-		if (isClosed())
-			throw Util.noCurrentConnection();
+        checkIfClosed();
 
 		if (dbMetadata == null) {
 
@@ -1068,8 +1068,8 @@
 	 * or ResultSet.CLOSE_CURSORS_AT_COMMIT
 	 *
 	 */
-	public final int getHoldability()
-	{
+	public final int getHoldability() throws SQLException {
+		checkIfClosed();
 		return connectionHoldAbility;
 	}
 
@@ -1083,8 +1083,8 @@
 	 * or ResultSet.CLOSE_CURSORS_AT_COMMIT
 	 *
 	 */
-	public final void setHoldability(int holdability)
-	{
+	public final void setHoldability(int holdability) throws SQLException {
+		checkIfClosed();
 		connectionHoldAbility = holdability;
 	}
 
@@ -1122,6 +1122,7 @@
      */
     public final boolean isReadOnly() throws SQLException
 	{
+		checkIfClosed();
 		return getLanguageConnection().isReadOnly();
 	}
 
@@ -1133,6 +1134,7 @@
      * @exception SQLException if a database-access error occurs.
      */
     public void setCatalog(String catalog) throws SQLException {
+        checkIfClosed();
 		// silently ignoring this request like the javadoc said.
 		return;
 	}
@@ -1144,6 +1146,7 @@
      * @exception SQLException if a database-access error occurs.
      */
 	public String getCatalog() throws SQLException {
+		checkIfClosed();
 		// we do not have support for Catalog, just return null as
 		// the JDBC specs mentions then.
 		return null;
@@ -1211,7 +1214,7 @@
      * @exception SQLException if a database-access error occurs.
      */
     public final int getTransactionIsolation() throws SQLException {
-
+        checkIfClosed();
 		return ExecutionContext.CS_TO_JDBC_ISOLATION_LEVEL_MAP[getLanguageConnection().getCurrentIsolationLevel()];
 	}
 
@@ -1227,7 +1230,8 @@
 	 * Synchronization note: Warnings are synchronized 
 	 * on nesting level
      */
-	public final synchronized SQLWarning getWarnings() {
+	public final synchronized SQLWarning getWarnings() throws SQLException {
+		checkIfClosed();
    		return topWarning;
 	}
 
@@ -1238,7 +1242,8 @@
 	 * Synchronization node: Warnings are synchonized 
 	 * on nesting level
      */
-    public final synchronized void clearWarnings() {
+    public final synchronized void clearWarnings() throws SQLException {
+        checkIfClosed();
 		topWarning = null;
 	}
 
@@ -1255,7 +1260,8 @@
 	 * JDBC 2.0 - java.util.Map requires JDK 1
      *
      */
-    public java.util.Map getTypeMap() {
+    public java.util.Map getTypeMap() throws SQLException {
+        checkIfClosed();
 		// just return an immuntable empty map
 		return java.util.Collections.EMPTY_MAP;
     }
@@ -1268,7 +1274,7 @@
      * @exception SQLException Feature not implemented for now.
 	 */
     public final void setTypeMap(java.util.Map map) throws SQLException {
-
+        checkIfClosed();
         if( map == null)
             throw Util.generateCsSQLException(SQLState.INVALID_API_PARAMETER,map,"map",
                                               "java.sql.Connection.setTypeMap");
@@ -1319,6 +1325,17 @@
 		return getTR().getLcc();
 	}
 
+    /**
+     * Raises an exception if the connection is closed.
+     *
+     * @exception SQLException if the connection is closed
+     */
+    protected final void checkIfClosed() throws SQLException {
+        if (isClosed()) {
+            throw Util.noCurrentConnection();
+        }
+    }
+
 	//EmbedConnection30 overrides this method so it can release the savepoints array if
 	//the exception severity is transaction level
 	SQLException handleException(Throwable thrownException)
@@ -1493,8 +1510,7 @@
 			is in a finally block.
 		 */
 
-		if (isClosed())
-			throw Util.noCurrentConnection();
+		checkIfClosed();
 
 		getTR().setupContextStack();
 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection40.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection40.java?rev=399604&r1=399603&r2=399604&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection40.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection40.java Thu
May  4 01:17:00 2006
@@ -30,6 +30,8 @@
 import java.sql.SQLException;
 import java.sql.SQLXML;
 import java.sql.Struct;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Properties;
 import org.apache.derby.jdbc.InternalDriver;
 import org.apache.derby.iapi.reference.SQLState;
@@ -128,6 +130,27 @@
     throws SQLException{
         throw Util.notImplemented();
     }
+
+    /**
+     * Returns the type map for this connection.
+     *
+     * @return type map for this connection
+     * @exception SQLException if a database access error occurs
+     */
+    public final Map<String, Class<?>> getTypeMap() throws SQLException {
+        // This method is already implemented with a non-generic
+        // signature in EmbedConnection. We could just use that method
+        // directly, but then we get a compiler warning (unchecked
+        // cast/conversion). Copy the map to avoid the compiler
+        // warning.
+        Map typeMap = super.getTypeMap();
+        if (typeMap == null) return null;
+        Map<String, Class<?>> genericTypeMap = new HashMap<String, Class<?>>();
+        for (Object key : typeMap.keySet()) {
+            genericTypeMap.put((String) key, (Class) typeMap.get(key));
+        }
+        return genericTypeMap;
+    }
     
     /**
      * This method forwards all the calls to default query object provided by 
@@ -137,6 +160,7 @@
      */
     public <T extends BaseQuery> T createQueryObject(Class<T> ifc) 
                                                     throws SQLException {
+        checkIfClosed();
         return QueryObjectFactory.createDefaultQueryObject (ifc, this);
     } 
     
@@ -152,6 +176,7 @@
      *                                with the given interface.
      */
     public boolean isWrapperFor(Class<?> interfaces) throws SQLException {
+        checkIfClosed();
         return interfaces.isInstance(this);
     }
     
@@ -165,6 +190,7 @@
      */
     public <T> T unwrap(java.lang.Class<T> interfaces) 
                             throws SQLException{
+        checkIfClosed();
         //Derby does not implement non-standard methods on 
         //JDBC objects
         //hence return this if this class implements the interface 

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ClosedObjectTest.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ClosedObjectTest.java?rev=399604&r1=399603&r2=399604&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ClosedObjectTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ClosedObjectTest.java
Thu May  4 01:17:00 2006
@@ -295,16 +295,22 @@
         /**
          * Checks whether a method expects an exception to be thrown
          * when the object is closed. Currently, only
-         * <code>close()</code> and <code>isClosed()</code> don't
-         * expect exceptions.
+         * <code>close()</code>, <code>isClosed()</code> and
+         * <code>isValid()</code> don't expect exceptions.
          *
          * @param method a method
          * @return <code>true</code> if an exception is expected
          */
         public boolean expectsException(Method method) {
-            return
-                !(method.getName().equals("close") ||
-                  method.getName().equals("isClosed"));
+            final String[] exceptionLessMethods = {
+                "close",
+                "isClosed",
+                "isValid",
+            };
+            for (String name : exceptionLessMethods) {
+                if (name.equals(method.getName())) return false;
+            }
+            return true;
         }
 
         /**

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ConnectionTest.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ConnectionTest.java?rev=399604&r1=399603&r2=399604&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ConnectionTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ConnectionTest.java
Thu May  4 01:17:00 2006
@@ -171,6 +171,15 @@
         }
     }
 
+    /**
+     * Tests that <code>getTypeMap()</code> returns an empty map when
+     * no type map has been installed.
+     * @exception SQLException if an error occurs
+     */
+    public void testGetTypeMapReturnsEmptyMap() throws SQLException {
+        assertTrue(con.getTypeMap().isEmpty());
+    }
+
     public void testIsWrapperReturnsFalse()
         throws SQLException {
         assertFalse(con.isWrapperFor(ResultSet.class));



Mime
View raw message