You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by ka...@apache.org on 2006/04/21 09:02:41 UTC

svn commit: r395799 - in /db/derby/code/trunk/java/engine/org/apache/derby: catalog/SystemProcedures.java impl/jdbc/EmbedDatabaseMetaData.java impl/sql/catalog/DD_Version.java

Author: kahatlen
Date: Fri Apr 21 00:02:39 2006
New Revision: 395799

URL: http://svn.apache.org/viewcvs?rev=395799&view=rev
Log:
DERBY-1176: Stored prepared statements in the SYSIBM schema are not
updated on upgrade

  1. DD_Version.doFullUpgrade() now drops and recreates SPSs in all
     system schemas, not SYSIBM only.

  2. EmbedDatabaseMetaData already has the necessary machinery to load
     queries from system tables or metadata.properties depending on
     whether the engine is in soft upgrade mode or not. I extended it
     so that it could read queries from metadata_net.properties as
     well.

  3. New method EmbedDatabaseMetaData.getClientCachedMetaData() which
     executes SYSIBM.METADATA (either from system table or
     metadata_net.properties) to fetch the metadata that will be
     cached on the client.

  4. SystemProcedures.METADATA() now invokes
     EmbedDatabaseMetaData.getClientCachedMetaData() instead of
     executing the METADATA SPS directly.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedDatabaseMetaData.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/DD_Version.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java?rev=395799&r1=395798&r2=395799&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java Fri Apr 21 00:02:39 2006
@@ -635,11 +635,7 @@
 	public static void METADATA (ResultSet[] rs)
 		throws SQLException
 	{
-		
-		Connection conn = getDefaultConn();
-		PreparedStatement ps = conn.prepareStatement("execute statement SYSIBM.METADATA");
-		rs[0] = ps.executeQuery();
-		conn.close();
+		rs[0] = ((EmbedDatabaseMetaData) getDMD()).getClientCachedMetaData();
 	}
 
 	/**

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedDatabaseMetaData.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedDatabaseMetaData.java?rev=395799&r1=395798&r2=395799&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedDatabaseMetaData.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedDatabaseMetaData.java Fri Apr 21 00:02:39 2006
@@ -24,6 +24,8 @@
 
 import org.apache.derby.iapi.services.monitor.Monitor;
 
+import org.apache.derby.iapi.services.sanity.SanityManager;
+
 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
 
 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
@@ -115,28 +117,54 @@
 
 	}
 
-	private static Properties queryDescriptions;
-	protected final Properties getQueryDescriptions() {
-		Properties p = EmbedDatabaseMetaData.queryDescriptions;
-		if (p != null)
-			return p;
-
-		return (EmbedDatabaseMetaData.queryDescriptions = loadQueryDescriptions());
-	}
-
-	private Properties PBloadQueryDescriptions() {
-		Properties p = new Properties();
-		try {
+    /** Cached query descriptions from metadata.properties. */
+    private static Properties queryDescriptions;
+    /** Cached query descriptions from metadata_net.properties. */
+    private static Properties queryDescriptions_net;
+    /**
+     * Return all queries found in either metadata.properties or
+     * metadata_net.properties.
+     *
+     * @param net if <code>true</code>, read metadata_net.properties;
+     * otherwise, read metadata.properties.
+     * @return a <code>Properties</code> value with the queries
+     */
+    private Properties getQueryDescriptions(boolean net) {
+        Properties p = net ? queryDescriptions_net : queryDescriptions;
+        if (p != null) {
+            return p;
+        }
+        loadQueryDescriptions();
+        return net ? queryDescriptions_net : queryDescriptions;
+    }
 
-			// SECURITY PERMISSION - IP3
-			InputStream is = getClass().getResourceAsStream("metadata.properties");
-			
-			p.load(is);
-			is.close();
-		} catch (IOException ioe) {
-		}
-		return p;
-	}
+    /**
+     * Read the query descriptions from metadata.properties and
+     * metadata_net.properties. This method must be invoked from
+     * within a privileged block.
+     */
+    private void PBloadQueryDescriptions() {
+        String[] files = {
+            "metadata.properties",
+            "/org/apache/derby/impl/sql/catalog/metadata_net.properties"
+        };
+        Properties[] props = new Properties[files.length];
+        for (int i = 0; i < files.length; ++i) {
+            try {
+                props[i] = new Properties();
+                // SECURITY PERMISSION - IP3
+                InputStream is = getClass().getResourceAsStream(files[i]);
+                props[i].load(is);
+                is.close();
+            } catch (IOException ioe) {
+                if (SanityManager.DEBUG) {
+                    SanityManager.THROWASSERT("Error reading " + files[i], ioe);
+                }
+            }
+        }
+        queryDescriptions = props[0];
+        queryDescriptions_net = props[1];
+    }
 
 	//////////////////////////////////////////////////////////////
 	//
@@ -1648,7 +1676,8 @@
 			ResultSet rs = null;
 			try {
 			
-			String queryText = getQueryDescriptions().getProperty("getTables");
+			String queryText =
+				getQueryDescriptions(false).getProperty("getTables");
 
 			/*
 			 * The query text is assumed to end with a "where" clause, so
@@ -2187,7 +2216,8 @@
      * This can happen if we are in soft upgrade mode. Since in soft upgrade 
      * mode, we can't change these stored metadata queries in a backward 
      * incompatible way, engine needs to read the metadata sql from 
-     * metadata.properties file rather than rely on system tables.
+     * metadata.properties or metadata_net.properties file rather than
+     * rely on system tables.
      * 
      * @return true if we are not in soft upgrade mode
      * @throws SQLException
@@ -2918,7 +2948,7 @@
       ResultSet rs = null;
       int getClassTypes = 0;
       try {
-        String queryText = getQueryDescriptions().getProperty("getUDTs");
+        String queryText = getQueryDescriptions(false).getProperty("getUDTs");
 
         if (types != null  &&  types.length >= 1) {
           for (int i=0; i<types.length; i++){
@@ -3285,20 +3315,67 @@
 	//
 	//////////////////////////////////////////////////////////////
 	
+    /**
+     * Get metadata that the client driver will cache. The metadata is
+     * fetched using SYSIBM.METADATA (found in metadata_net.properties).
+     *
+     * @return the result set returned by SYSIBM.METADATA
+     * @exception SQLException if a database error occurs
+     */
+    public ResultSet getClientCachedMetaData() throws SQLException {
+        return getSimpleQuery("METADATA", true);
+    }
+
 	/*
 	 * utility helper routines:
 	 */
 
-	protected ResultSet getSimpleQuery(String nameKey) throws SQLException
+    /**
+     * Execute a query in metadata.properties (or SPS in the SYS
+     * schema) or metadata_net.properties (or SPS in the SYSIBM
+     * schema).
+     *
+     * @param nameKey the name of the query
+     * @param net if <code>true</code>, execute a query in
+     * metadata_net.properties; otherwise, execute a query in
+     * metadata.properties
+     * @return a <code>ResultSet</code> value
+     * @exception SQLException if a database error occurs
+     */
+    private ResultSet getSimpleQuery(String nameKey, boolean net)
+        throws SQLException
 	{
-		PreparedStatement ps = getPreparedQuery(nameKey);
+		PreparedStatement ps = getPreparedQuery(nameKey, net);
 		if (ps == null)
 			return null;
 	
 		return ps.executeQuery();
 	}
 
-	private PreparedStatement getPreparedQueryUsingSystemTables(String nameKey) throws SQLException 
+    /**
+     * Execute a query in metadata.properties, or an SPS in the SYS
+     * schema.
+     *
+     * @param nameKey the name of the query
+     * @return a <code>ResultSet</code> value
+     * @exception SQLException if a database error occurs
+     */
+    protected ResultSet getSimpleQuery(String nameKey) throws SQLException {
+        return getSimpleQuery(nameKey, false);
+    }
+
+    /**
+     * Get a stored prepared statement from the system tables.
+     *
+     * @param nameKey the name of the query
+     * @param net if <code>true</code>, find query in SYSIBM schema;
+     * otherwise, find query in SYS schema
+     * @return a <code>PreparedStatement</code> value
+     * @exception SQLException if a database error occurs
+     */
+    private PreparedStatement getPreparedQueryUsingSystemTables(String nameKey,
+                                                                boolean net)
+        throws SQLException 
 	{
 		synchronized (getConnectionSynchronization())
 		{
@@ -3307,13 +3384,14 @@
 
 			try
 			{
-				String queryText = getQueryDescriptions().getProperty(nameKey);
+				String queryText =
+					getQueryDescriptions(net).getProperty(nameKey);
 				if (queryText == null)
 				{
                     throw Util.notImplemented(nameKey);
 				}
 				
-                ps = prepareSPS(nameKey, queryText);
+                ps = prepareSPS(nameKey, queryText, net);
 			}
 
 			catch (Throwable t) 
@@ -3331,7 +3409,8 @@
 
 	/**
 	 * Either get the prepared query for the metadata call from the
-	 * system tables or from the metadata.properties file.
+	 * system tables, or from the metadata.properties or
+	 * metadata_net.properties file.
 	 * In soft upgrade mode, the queries stored in the system tables
 	 * might not be upto date with the Derby engine release because
 	 * system tables can't be modified in backward incompatible way in
@@ -3339,7 +3418,7 @@
 	 * soft upgrade mode, get the queries from metadata.properties
 	 * file rather than from the system tables.
 	 *
-	 * Getting queries from metadata.properties might cause problems
+	 * Getting queries from metadata(_net).properties might cause problems
 	 * if system catalogs have been changed between versions either by
 	 * addition of columns or have new catalogs. To continue
 	 * to support soft upgrade from older versions of database, find
@@ -3347,21 +3426,26 @@
 	 *
 	 * @param queryName Name of the metadata query for which we need
 	 * a prepared statement
+	 * @param net if <code>true</code>, use metadata_net.properties
+	 * instead of metadata.properties
 	 * @return PreparedStatement
+	 * @exception SQLException if a database error occurs
 	 */
-	protected PreparedStatement getPreparedQuery(String queryName)
+	private PreparedStatement getPreparedQuery(String queryName,
+											   boolean net)
 			throws SQLException {
 		PreparedStatement s;
 		//We can safely goto system table since we are not in soft upgrade
 		//mode and hence metadata sql in system tables are uptodate
 		//with this Derby release.
 		if (notInSoftUpgradeMode())
-			s = getPreparedQueryUsingSystemTables(queryName);
+			s = getPreparedQueryUsingSystemTables(queryName, net);
 		else {
 			try {
 				//Can't use stored prepared statements because we are in soft upgrade
 				//mode and hence need to get metadata sql from metadata.properties file 
-				String queryText = getQueryFromDescription(queryName);
+				//or metadata_net.properties
+				String queryText = getQueryFromDescription(queryName, net);
 				s = getEmbedConnection().prepareMetaDataStatement(queryText);
 			} catch (Throwable t) {
 				throw handleException(t);
@@ -3370,7 +3454,20 @@
 		return s;
 	}	
 
-	/*
+    /**
+     * Get a prepared query from system tables or metadata.properties.
+     *
+     * @param queryName name of the query
+     * @return a <code>PreparedStatement</code> value
+     * @exception SQLException if a database error occurs
+     */
+    protected PreparedStatement getPreparedQuery(String queryName)
+        throws SQLException
+    {
+        return getPreparedQuery(queryName, false);
+    }
+
+	/**
 	 * Given a queryName, find closest match in queryDescriptions. This method
 	 * should be called in soft-upgrade mode only, where current software version
 	 * doesn't match dictionary version. For these cases, there may be
@@ -3385,8 +3482,14 @@
 	 * all Major_Minor versions between software version and dictionary version
 	 * and try each one from Dictionary version to current version. Since only
 	 * needed for two queries, overhead may not be worth it yet.
+	 *
+	 * @param queryName name of the query
+	 * @param net if <code>true</code>, get the query from
+	 * metadata_net.properties instead of metadata.properties
+	 * @return the query text
+	 * @exception StandardException if an error occurs
 	 */
-	private String getQueryFromDescription(String queryName)
+	private String getQueryFromDescription(String queryName, boolean net)
 		throws StandardException
 	{
 		DataDictionary dd = getLanguageConnectionContext().getDataDictionary();
@@ -3403,7 +3506,7 @@
 				queryName = "getTablePrivileges_10_1";
 		}
 
-		return getQueryDescriptions().getProperty(queryName);
+		return getQueryDescriptions(net).getProperty(queryName);
 	}
 
 	/*
@@ -3413,7 +3516,8 @@
 	** 
 	*/
 	private PreparedStatement prepareSPS(String	spsName, 
-										 String	spsText)
+										 String	spsText,
+										 boolean net)
 		throws StandardException, SQLException
 	{
 
@@ -3428,6 +3532,7 @@
 		DataDictionary dd = getLanguageConnectionContext().getDataDictionary();
 		SPSDescriptor spsd = dd.getSPSDescriptor(
 										spsName, 
+										net ? dd.getSysIBMSchemaDescriptor() :
 										dd.getSystemSchemaDescriptor());
 		lcc.commitNestedTransaction();
 
@@ -3446,8 +3551,10 @@
 		** statement, but we have no (easy) way of turning
 		** the statement into a java.sql.PreparedStatement.
 		*/	
-		return getEmbedConnection().prepareMetaDataStatement(
-									"EXECUTE STATEMENT SYS.\""+spsName+"\"");
+		String queryText =
+			"EXECUTE STATEMENT " + (net ? "SYSIBM" : "SYS") +
+			".\"" + spsName + "\"";
+		return getEmbedConnection().prepareMetaDataStatement(queryText);
 
 	}
 
@@ -3490,13 +3597,24 @@
 	** Priv block code, moved out of the old Java2 version.
 	*/
 
-	private final Properties loadQueryDescriptions() {
-		return (Properties) java.security.AccessController.doPrivileged(this);
-	}
+    /**
+     * Loads the query descriptions from metadata.properties and
+     * metadata_net.properties into <code>queryDescriptions</code> and
+     * <code>queryDescriptions_net</code>.
+     */
+    private void loadQueryDescriptions() {
+        java.security.AccessController.doPrivileged(this);
+    }
 
+	/**
+	 * Performs a privileged action. Reads the query descriptions.
+	 *
+	 * @return <code>null</code>
+	 */
 	public final Object run() {
 		// SECURITY PERMISSION - IP3
-		return PBloadQueryDescriptions();
+		PBloadQueryDescriptions();
+		return null;
 	}
 
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/DD_Version.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/DD_Version.java?rev=395799&r1=395798&r2=395799&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/DD_Version.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/DD_Version.java Fri Apr 21 00:02:39 2006
@@ -321,7 +321,7 @@
 		//This is to make sure that we have the stored versions of JDBC database
 		//metadata queries matching with this release of the engine.
 		dropJDBCMetadataSPSes(tc, false);
-		bootingDictionary.createSPSSet(tc, false, bootingDictionary.getSystemSchemaDescriptor().getUUID());
+		bootingDictionary.createSystemSps(tc);
 
 		/*
 		 * OLD Cloudscape 5.1 upgrade code, Derby does not support
@@ -440,6 +440,10 @@
 	 * on upgrade time).
 	 *
 	 * @param tc the xact
+	 * @param removeSYSIBMonly if <code>true</code>, remove stored
+	 * prepared statements in the SYSIBM schema only; otherwise,
+	 * remove stored prepared statements in all system schemas
+	 * (including SYSIBM)
 	 *
 	 * @exception StandardException  Standard Cloudscape error policy.
 	 */
@@ -452,16 +456,21 @@
 			SchemaDescriptor sd = spsd.getSchemaDescriptor();
 			// need to compare the name, old SYSIBM is not built-in
 			boolean isSYSIBM = sd.getSchemaName().equals(SchemaDescriptor.IBM_SYSTEM_SCHEMA_NAME);
-			if (! sd.isSystemSchema() && ! isSYSIBM)
+
+			// don't drop statements in non-system schemas
+			if (!sd.isSystemSchema() && !isSYSIBM) {
+				continue;
+			}
+
+			// don't drop statements outside the SYSIBM schema if
+			// we're told not to
+			if (removeSYSIBMonly && !isSYSIBM) {
 				continue;
-			/*
-			** Is it in SYS? if so, zap it.
-			*/
-			if ((removeSYSIBMonly && isSYSIBM) || (! removeSYSIBMonly && ! isSYSIBM))
-			{
-				bootingDictionary.dropSPSDescriptor(spsd, tc);
-				bootingDictionary.dropDependentsStoredDependencies(spsd.getUUID(), tc);
 			}
+
+			bootingDictionary.dropSPSDescriptor(spsd, tc);
+			bootingDictionary.dropDependentsStoredDependencies(spsd.getUUID(),
+															   tc);
 		}
 	}