db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject svn commit: r230523 - in /db/derby/code/trunk/java: engine/org/apache/derby/iapi/jdbc/ engine/org/apache/derby/impl/jdbc/ engine/org/apache/derby/jdbc/ testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/
Date Sat, 06 Aug 2005 02:59:43 GMT
Author: tmnk
Date: Fri Aug  5 19:59:31 2005
New Revision: 230523

URL: http://svn.apache.org/viewcvs?rev=230523&view=rev
Log:
DERBY-412 - Connection toString should show type information and the meaning of the identifier
that it prints - Patch by David Van Couvering(David.Vancouvering@Sun.COM)

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredConnection.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java
    db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/build.xml
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/checkDataSource.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredConnection.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredConnection.java?rev=230523&r1=230522&r2=230523&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredConnection.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredConnection.java Fri
Aug  5 19:59:31 2005
@@ -51,6 +51,7 @@
 
 	protected final BrokeredConnectionControl control;
 	private boolean isClosed;
+        private String connString;
 
 	/**
 		Maintain state as seen by this Connection handle, not the state
@@ -486,33 +487,30 @@
 	}
             
     /**
-     * Get the string representation for the underlying physical
-     * connection.
-     *
-     *  When a physical connection is created, it is assigned a unique id 
-     *  that is unchanged for the lifetime of the connection. When an 
-     *  application calls Connection.toString(), it gets the string 
-     *  representation of the underlying physical connection, regardless 
-     *  of whether the application has a reference to the physical connection 
-     *  itself or a reference to a proxy connection (aka brokered connection) 
-     *  that wraps the physical connection.
-     *
-     *  Since this BrokeredConnection is a proxy connection, we return the
-     *  string value of its underlying physical connection
+     * Get the string representation for this connection.  Return
+     * the class name/hash code and various debug information.
      * 
-     * @return unique string representation of the underlying
-     *   physical connection
+     * @return unique string representation for this connection
      */
     public String toString() 
     {
-        try
+        if ( connString == null )
         {
-            return getRealConnection().toString();
-        }
-        catch ( SQLException e )
-        {
-            return "<no connection>";
+            String wrappedString;
+            try
+            {
+                wrappedString = getRealConnection().toString();
+            }
+            catch ( SQLException e )
+            {
+                wrappedString = "<none>";
+            }
+            
+            connString = this.getClass().getName() + "@" + this.hashCode() +
+                ", Wrapped Connection = " + wrappedString;
         }
+        
+        return connString;
     }
 
 	protected int getJDBCLevel() { return 2;}

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=230523&r1=230522&r2=230523&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 Fri Aug
 5 19:59:31 2005
@@ -145,7 +145,7 @@
 	private int resultSetId;
     
     /** Cached string representation of the connection id */
-    private String idString;
+    private String connString;
 
 
 	//////////////////////////////////////////////////////////
@@ -1919,7 +1919,8 @@
 
     /**
      * Get a String representation that uniquely identifies
-     * this connection
+     * this connection.  Include the same information that is
+     * printed in the log for various trace and error messages.
      *
      * In Derby the "physical" connection is a LanguageConnectionContext, 
      * or LCC.
@@ -1928,18 +1929,27 @@
      * Note that this is a big aid in debugging, because much of the
      * engine trace and log code prints the LCC id. 
      *
-     * @return a string representation of the unique id for the
-     *    underlying LanguageConnectionContext
+     * @return a string representation for this connection
      */
     public String toString()
     {
-        if ( idString == null )
+        if ( connString == null )
         {
-            idString = 
-              Integer.toString(getLanguageConnection().getInstanceNumber());
-        }
+            
+            LanguageConnectionContext lcc = getLanguageConnection();
+
+            connString = 
+              this.getClass().getName() + "@" + this.hashCode() + " " +
+                lcc.xidStr +                  
+                    lcc.getTransactionExecute().getTransactionIdString() + 
+                    "), " +
+                lcc.lccStr + 
+                    Integer.toString(lcc.getInstanceNumber()) + "), " +
+                lcc.dbnameStr + lcc.getDbname() + "), " +
+                lcc.drdaStr + lcc.getDrdaID() + ") ";
+        }       
         
-        return idString;
+        return connString;
     }
 
 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection.java?rev=230523&r1=230522&r2=230523&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection.java Fri Aug
 5 19:59:31 2005
@@ -63,8 +63,8 @@
     /** The id for this connection.  */
     private int connectionId;
     
-    /** String representation of id */
-    private String idString;
+    /** the connection string */
+    private String connString;
     
 	private Vector eventListener; // who wants to know I am closed or error
 
@@ -459,12 +459,18 @@
      */
     public String toString()
     {
-        if ( idString == null )
+        if ( connString == null )
         {
-            idString = Integer.toString(connectionId);
-        }
+            String physicalConnString = isActive ?
+                realConnection.toString() : "<none>";
+            
+            connString = 
+              this.getClass().getName() + "@" + this.hashCode() + " " +
+                "(ID = " + connectionId + "), " +
+                "Physical Connection = " + physicalConnString;
+        }    
         
-        return idString;
+        return connString;
     }
 
 }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/build.xml
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/build.xml?rev=230523&r1=230522&r2=230523&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/build.xml
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/build.xml
Fri Aug  5 19:59:31 2005
@@ -119,8 +119,9 @@
       srcdir="${derby.testing.src.dir}"
       destdir="${out.dir}">
       <classpath>
-        <pathelement location="${jta1_2}"/>
         <pathelement path="${java13compile.classpath}"/>
+        <pathelement location="${oro}"/>
+        <pathelement location="${jta1_2}"/>
       </classpath>
       <!--exclude name=""/-->
       <include name="${this.dir}/checkDataSource.java"/>

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/checkDataSource.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/checkDataSource.java?rev=230523&r1=230522&r2=230523&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/checkDataSource.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/checkDataSource.java
Fri Aug  5 19:59:31 2005
@@ -24,6 +24,8 @@
 import org.apache.derby.jdbc.EmbeddedSimpleDataSource;
 import org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource;
 import org.apache.derby.jdbc.EmbeddedXADataSource;
+import org.apache.derby.iapi.jdbc.BrokeredConnection;
+import org.apache.derby.impl.jdbc.EmbedConnection;
 
 import java.sql.Connection;
 import java.sql.ResultSet;
@@ -49,13 +51,33 @@
 import java.io.*;
 import java.util.Hashtable;
 import java.util.Iterator;
-
+import org.apache.oro.text.perl.Perl5Util;
 import javax.naming.*;
 import javax.naming.directory.*;
 
 public class checkDataSource
 { 
+    /**
+     * A hashtable of opened connections.  This is used when checking to
+     * make sure connection strings are unique; we need to make sure all
+     * the connections are closed when we are done, so they are stored
+     * in this hashtable
+     */
     protected static Hashtable conns = new Hashtable();
+    
+    /**
+     * This is a utility that knows how to do pattern matching.  Used
+     * in checking the format of a connection string
+     */
+    protected static Perl5Util p5u = new Perl5Util();
+    
+    /** The expected format of a connection string. In English:
+     * "<classname>@<hashcode> (XID=<xid>), (SESSION = <sessionid>),
+     *  (DATABASE=<dbname>), (DRDAID = <drdaid>)"
+     */
+    private static final String CONNSTRING_FORMAT = "\\S+@[0-9]+ " +
+        "\\(XID = .*\\), \\(SESSIONID = [0-9]+\\), " +
+        "\\(DATABASE = [A-Za-z]+\\), \\(DRDAID = .+\\)";
 
 	public static void main(String[] args) throws Exception {
 
@@ -841,8 +863,9 @@
     /**
      * Make sure this connection's string is unique (DERBY-243)
      */
-    protected void checkToString(Connection conn) throws Exception
+    protected static void checkToString(Connection conn) throws Exception
     {
+        checkStringFormat(conn);
         String str = conn.toString();
 
         if ( conns.containsKey(str))
@@ -853,11 +876,89 @@
         conns.put(str, conn);
     }
     
+    /** 
+     * Check the format of a pooled connection
+     **/
+    protected static void checkStringFormat(PooledConnection pc) throws Exception
+    {
+        String prefix = checkStringPrefix(pc);
+        String connstr = pc.toString();
+        String format = "/" + prefix + 
+            " \\(ID = [0-9]+\\), Physical Connection = " +
+            "<none>|" + CONNSTRING_FORMAT + "/";
+        
+        if ( ! p5u.match(format, connstr) )
+        {
+            throw new Exception( "Connection.toString() (" + connstr + ") " +
+              "does not match expected format (" + format + ")");
+        }
+    }
+    
+    /**
+     * Check the format for the toString() of a Connection that is an
+     * instance of BrokeredConnection.  This is different, as the Brokered
+     * Connection prints out its class name and then the toString() value
+     * of the underlying, wrapped connection.
+     */
+    protected static void checkStringFormat(BrokeredConnection conn) throws Exception
+    {
+        String connstr = conn.toString();
+        String prefix = checkStringPrefix(connstr);
+        
+        String format = "/" + prefix + ", Wrapped Connection = " + 
+            "<none>|" + CONNSTRING_FORMAT + "/";
+
+        if ( ! p5u.match(format, connstr) )
+        {
+            throw new Exception( "Connection.toString() (" + connstr + ") " +
+                "does not match expected format (" + format + ")");
+        }
+    }
+        
+     /**
+     * Check the format of the connection string.  This is the default test
+     * to run if this is not a BrokeredConnection class
+     */
+    protected static void checkStringFormat(Connection conn) throws Exception
+    {
+        String prefix = checkStringPrefix(conn);
+        
+        String str = conn.toString();        
+       
+        // See if the connection string matches the format pattern    
+        if ( ! p5u.match("/" + CONNSTRING_FORMAT + "/", str) )
+        {
+            throw new Exception( "Connection.toString() (" + str + ") " +
+                "does not match expected format (" + CONNSTRING_FORMAT + ")");
+        }
+    }
+    
+    /**
+     * Make sure the connection string starts with the right prefix, which
+     * is the classname@hashcode.
+     *
+     * @return the expected prefix string, this is used in further string
+     *   format checking
+     */
+    protected static String checkStringPrefix(Object conn) throws Exception
+    {
+        String connstr = conn.toString();
+        String prefix = conn.getClass().getName() + "@" + conn.hashCode();
+        if ( ! connstr.startsWith(prefix) )
+        {
+            throw new Exception("Connection class and hash code for " +
+                "connection string (" + connstr + ") does not match expected " +
+                "(" + prefix + ")");
+        }  
+        
+        return prefix;
+    }
+    
     /**
      * Clear out and close connections in the connections
      * hashtable. 
      */
-    protected void clearConnections() throws SQLException
+    protected static void clearConnections() throws SQLException
     {
         java.util.Iterator it = conns.values().iterator();
         while ( it.hasNext() )
@@ -872,7 +973,7 @@
      * Get connections  using ij.startJBMS() and make sure
      * they're unique
      */
-    protected void checkJBMSToString() throws Exception
+    protected static void checkJBMSToString() throws Exception
     {
         clearConnections();
         // Open ten connections rather than just two to
@@ -893,7 +994,7 @@
      * Check uniqueness of connection strings coming from a
      * DataSouce
      */
-    protected void checkToString(DataSource ds) throws Exception
+    protected static void checkToString(DataSource ds) throws Exception
     {
         clearConnections();
         
@@ -912,7 +1013,7 @@
      * We want to check the PooledConnection as well as the
      * underlying physical connection. 
      */
-    protected void checkToString(ConnectionPoolDataSource pds)
+    protected static void checkToString(ConnectionPoolDataSource pds)
         throws Exception
     {
         int numConnections = 10;
@@ -923,6 +1024,7 @@
         for ( int i = 0 ; i < numConnections ; i++ )
         {
             PooledConnection pc = pds.getPooledConnection();
+            checkStringFormat(pc);
             String str = pc.toString();
             if ( pooledConns.get(str) != null )
             {
@@ -952,26 +1054,12 @@
             pc.close();
         }
         pooledConns.clear();
-        
-        // Now check that two connections from the same 
-        // PooledConnection have the same string value
-        PooledConnection pc = pds.getPooledConnection();
-        Connection conn = pc.getConnection();
-        String str = conn.toString();
-        conn = pc.getConnection();
-        if ( ! conn.toString().equals(str) )
-        {
-            throw new Exception("Two connections from the " +
-              "same pooled connection have different string " +
-              "values: " + str + ", " + conn.toString());
-        }
-        pc.close();
     }
     
     /**
      * Check uniqueness of strings for an XA data source
      */
-    protected void checkToString(XADataSource xds) throws Exception
+    protected static void checkToString(XADataSource xds) throws Exception
     {
         int numConnections = 10;
         
@@ -981,6 +1069,7 @@
         for ( int i = 0 ; i < numConnections ; i++ )
         {
             XAConnection xc = xds.getXAConnection();
+            checkStringFormat(xc);
             String str = xc.toString();
             if ( xaConns.get(str) != null )
             {
@@ -1010,20 +1099,6 @@
             xc.close();
         }
         xaConns.clear();
-        
-        // Now check that two connections from the same 
-        // XAConnection have the same string value
-        XAConnection xc = xds.getXAConnection();
-        Connection conn = xc.getConnection();
-        String str = conn.toString();
-        conn = xc.getConnection();
-        if ( ! conn.toString().equals(str) )
-        {
-            throw new Exception("Two connections from the " +
-              "same pooled connection have different string " +
-              "values: " + str + ", " + conn.toString());
-        }
-        xc.close();
     }
     
 	protected static void checkConnectionPreCloseS(String dsName, Connection conn) throws SQLException
{



Mime
View raw message