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 km...@apache.org on 2005/06/09 06:52:01 UTC

svn commit: r189704 - in /incubator/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/master/ testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ testing/org/apache/derbyTesting/functionTests/tests/lang/

Author: kmarsden
Date: Wed Jun  8 21:52:00 2005
New Revision: 189704

URL: http://svn.apache.org/viewcvs?rev=189704&view=rev
Log:
DERBY-243

Change connection toString to print connection id

			contributed by David Van Couvering

Modified:
    incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredConnection.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection.java
    incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/miscerrors.out
    incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/checkDataSource.java
    incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/miscerrors.sql

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredConnection.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredConnection.java?rev=189704&r1=189703&r2=189704&view=diff
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredConnection.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredConnection.java Wed Jun  8 21:52:00 2005
@@ -452,6 +452,36 @@
 	{
 		return control.getPrepareIsolation();
 	}
+            
+    /**
+     * 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
+     * 
+     * @return unique string representation of the underlying
+     *   physical connection
+     */
+    public String toString() 
+    {
+        try
+        {
+            return getRealConnection().toString();
+        }
+        catch ( SQLException e )
+        {
+            return "<no connection>";
+        }
+    }
 
 	protected int getJDBCLevel() { return 2;}
 }

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java?rev=189704&r1=189703&r2=189704&view=diff
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection.java Wed Jun  8 21:52:00 2005
@@ -143,6 +143,9 @@
 		returned in order of their creation. Is maintained at the root connection.
 	*/
 	private int resultSetId;
+    
+    /** Cached string representation of the connection id */
+    private String idString;
 
 
 	//////////////////////////////////////////////////////////
@@ -1914,5 +1917,30 @@
 	//
 	/////////////////////////////////////////////////////////////////////////
 
-    public	String	toString() { return "EmbedConnection"; }
+    /**
+     * Get a String representation that uniquely identifies
+     * this connection
+     *
+     * In Derby the "physical" connection is a LanguageConnectionContext, 
+     * or LCC.
+     * The JDBC Connection is an JDBC-specific layer on top of this.  Rather
+     * than create a new id here, we simply use the id of the underlying LCC.
+     * 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
+     */
+    public String toString()
+    {
+        if ( idString == null )
+        {
+            idString = 
+              Integer.toString(getLanguageConnection().getInstanceNumber());
+        }
+        
+        return idString;
+    }
+
+
 }

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection.java?rev=189704&r1=189703&r2=189704&view=diff
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection.java Wed Jun  8 21:52:00 2005
@@ -57,6 +57,15 @@
 class EmbedPooledConnection implements javax.sql.PooledConnection, BrokeredConnectionControl
 {
 
+    /** Static counter for connection ids */
+    private static int idCounter = 0;
+    
+    /** The id for this connection.  */
+    private int connectionId;
+    
+    /** String representation of id */
+    private String idString;
+    
 	private Vector eventListener; // who wants to know I am closed or error
 
 	protected EmbedConnection realConnection;
@@ -74,9 +83,16 @@
 	private final boolean requestPassword;
 
 	private boolean isActive;
+    
+    private synchronized int nextId()
+    {
+        return idCounter++;
+    }
 
 	EmbedPooledConnection(ReferenceableDataSource ds, String u, String p, boolean requestPassword) throws SQLException
 	{
+        connectionId = nextId();
+
 		dataSource = ds;
 		username = u;
 		password = p;
@@ -427,5 +443,28 @@
 	{
 		return realConnection.getPrepareIsolation();
 	}
+    
+    /** 
+     * Get the string representation of this pooled connection.
+     *
+     * A pooled connection is assigned a separate id from a physical 
+     * connection. When a container calls PooledConnection.toString(), 
+     * it gets the string representation of this id. This is useful for 
+     * developers implementing connection pools when they are trying to
+     * debug pooled connections. 
+     *
+     * @return a string representation of the uniquie id for this pooled
+     *    connection.
+     *
+     */
+    public String toString()
+    {
+        if ( idString == null )
+        {
+            idString = Integer.toString(connectionId);
+        }
+        
+        return idString;
+    }
 
 }

Modified: incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/miscerrors.out
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/miscerrors.out?rev=189704&r1=189703&r2=189704&view=diff
==============================================================================
--- incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/miscerrors.out (original)
+++ incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/miscerrors.out Wed Jun  8 21:52:00 2005
@@ -28,4 +28,14 @@
 ij> -- set isolation to repeatable read
 set isolation serializable;
 0 rows inserted/updated/deleted
+ij> -- Print the output of the ErrorLogVTI, make sure it's valid
+SELECT * FROM new org.apache.derby.diag.ErrorLogReader() vti;
+TS                        |THREADID                                |XID            |LCCID          |DATABASE                                                                                                                        |DRDAID                                            |LOGTEXT                                                                                                                         
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+xxxxxxFILTERED-TIMESTAMPxxxxx|main,5,main                             |92             |0              | wombat                                                                                                                         | null                                             |Cleanup action starting                                                                                                         
+xxxxxxFILTERED-TIMESTAMPxxxxx|main,5,main                             |92             |0              | wombat                                                                                                                         | null                                             |Failed Statement is: --                                                                                                         
+xxxxxxFILTERED-TIMESTAMPxxxxx|main,5,main                             |97             |0              | wombat                                                                                                                         | null                                             |Cleanup action starting                                                                                                         
+xxxxxxFILTERED-TIMESTAMPxxxxx|main,5,main                             |97             |0              | wombat                                                                                                                         | null                                             |Failed Statement is: create table a (one int, two int)                                                                          
+xxxxxxFILTERED-TIMESTAMPxxxxx|main,5,main                             |100            |0              | wombat                                                                                                                         | null                                             |Cleanup action starting                                                                                                         
+xxxxxxFILTERED-TIMESTAMPxxxxx|main,5,main                             |100            |0              | wombat                                                                                                                         | null                                             |Failed Statement is: create table a (one int)                                                                                   
 ij> 

Modified: incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/checkDataSource.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/checkDataSource.java?rev=189704&r1=189703&r2=189704&view=diff
==============================================================================
--- incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/checkDataSource.java (original)
+++ incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/checkDataSource.java Wed Jun  8 21:52:00 2005
@@ -48,16 +48,26 @@
 
 import java.io.*;
 import java.util.Hashtable;
+import java.util.Iterator;
 
 import javax.naming.*;
 import javax.naming.directory.*;
 
 public class checkDataSource
 { 
+    protected static Hashtable conns = new Hashtable();
 
 	public static void main(String[] args) throws Exception {
 
-		new checkDataSource().runTest(args);
+        try
+        {
+			new checkDataSource().runTest(args);
+        }
+        catch ( Exception e )
+        {
+            e.printStackTrace();
+            throw e;
+        }
 		System.out.println("Completed checkDataSource");
 
 	}
@@ -81,10 +91,12 @@
 		
 
 		checkConnection("DriverManager ", dmc);
+		checkJBMSToString();
 
 
 		EmbeddedDataSource dscs = new EmbeddedDataSource();
 		dscs.setDatabaseName("wombat");
+		checkToString(dscs);
 
 		DataSource ds = dscs;
 
@@ -99,6 +111,7 @@
 		dscsp.setDatabaseName("wombat");
 		//dscsp.setConnectionAttributes("unicode=true");
 		ConnectionPoolDataSource dsp = dscsp;
+		checkToString(dsp);
 
 		PooledConnection pc = dsp.getPooledConnection();
 		pc.addConnectionEventListener(new EventCatcher(1));
@@ -157,6 +170,7 @@
 		//dscsx.setConnectionAttributes("unicode=true");
 
 		XADataSource dsx = dscsx;
+		checkToString(dsx);
 
 		XAConnection xac = dsx.getXAConnection();
 		xac.addConnectionEventListener(new EventCatcher(3));
@@ -823,7 +837,195 @@
 			System.out.println(dsName + " <closedstmt>.execute() " + sqle.getSQLState() + " - " + sqle.getMessage());
 		}
 	}
-
+        
+    /**
+     * Make sure this connection's string is unique (DERBY-243)
+     */
+    protected void checkToString(Connection conn) throws Exception
+    {
+        String str = conn.toString();
+
+        if ( conns.containsKey(str))
+        {
+            throw new Exception("ERROR: Connection toString() is not unique: " 
+              + str);
+        }
+        conns.put(str, conn);
+    }
+    
+    /**
+     * Clear out and close connections in the connections
+     * hashtable. 
+     */
+    protected void clearConnections() throws SQLException
+    {
+        java.util.Iterator it = conns.values().iterator();
+        while ( it.hasNext() )
+        {
+            Connection conn = (Connection)it.next();
+            conn.close();
+        }
+        conns.clear();
+    }
+    
+    /**
+     * Get connections  using ij.startJBMS() and make sure
+     * they're unique
+     */
+    protected void checkJBMSToString() throws Exception
+    {
+        clearConnections();
+        // Open ten connections rather than just two to
+        // try and catch any odd uniqueness bugs.  Still
+        // no guarantee but is better than just two.
+        int numConnections = 10;
+        for ( int i = 0 ; i < numConnections ; i++ )
+        {
+            Connection conn = ij.startJBMS();
+            checkToString(conn);
+        }
+        
+        // Now close the connections
+        clearConnections();
+    }
+    
+    /**
+     * Check uniqueness of connection strings coming from a
+     * DataSouce
+     */
+    protected void checkToString(DataSource ds) throws Exception
+    {
+        clearConnections();
+        
+        int numConnections = 10;
+        for ( int i = 0 ; i < numConnections ; i++ )
+        {
+            Connection conn = ds.getConnection();
+            checkToString(conn);
+        }
+        
+        clearConnections();
+    }
+    
+    /**
+     * Check uniqueness of strings with a pooled data source.
+     * We want to check the PooledConnection as well as the
+     * underlying physical connection. 
+     */
+    protected void checkToString(ConnectionPoolDataSource pds)
+        throws Exception
+    {
+        int numConnections = 10;
+        
+        //  First get a bunch of pooled connections
+        //  and make sure they're all unique
+        Hashtable pooledConns = new Hashtable();
+        for ( int i = 0 ; i < numConnections ; i++ )
+        {
+            PooledConnection pc = pds.getPooledConnection();
+            String str = pc.toString();
+            if ( pooledConns.get(str) != null )
+            {
+                throw new Exception("Pooled connection toString " +
+                  "value " + str + " is not unique");
+            }
+            pooledConns.put(str, pc);
+        }
+
+        // Now check that connections from each of these
+        // pooled connections have different string values
+        Iterator it = pooledConns.values().iterator();
+        clearConnections();
+        while ( it.hasNext() )
+        {
+            PooledConnection pc = (PooledConnection)it.next();
+            Connection conn = pc.getConnection();
+            checkToString(conn);
+        }
+        clearConnections();
+        
+        // Now clear out the pooled connections
+        it = pooledConns.values().iterator();
+        while ( it.hasNext() )
+        {
+            PooledConnection pc = (PooledConnection)it.next();
+            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
+    {
+        int numConnections = 10;
+        
+        //  First get a bunch of pooled connections
+        //  and make sure they're all unique
+        Hashtable xaConns = new Hashtable();
+        for ( int i = 0 ; i < numConnections ; i++ )
+        {
+            XAConnection xc = xds.getXAConnection();
+            String str = xc.toString();
+            if ( xaConns.get(str) != null )
+            {
+                throw new Exception("XA connection toString " +
+                  "value " + str + " is not unique");
+            }
+            xaConns.put(str, xc);
+        }
+
+        // Now check that connections from each of these
+        // pooled connections have different string values
+        Iterator it = xaConns.values().iterator();
+        clearConnections();
+        while ( it.hasNext() )
+        {
+            XAConnection xc = (XAConnection)it.next();
+            Connection conn = xc.getConnection();
+            checkToString(conn);
+        }
+        clearConnections();
+        
+        // Now clear out the pooled connections
+        it = xaConns.values().iterator();
+        while ( it.hasNext() )
+        {
+            XAConnection xc = (XAConnection)it.next();
+            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 {
 		if (dsName.endsWith("DataSource")) {
 

Modified: incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/miscerrors.sql
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/miscerrors.sql?rev=189704&r1=189703&r2=189704&view=diff
==============================================================================
--- incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/miscerrors.sql (original)
+++ incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/miscerrors.sql Wed Jun  8 21:52:00 2005
@@ -28,3 +28,6 @@
 
 -- set isolation to repeatable read
 set isolation serializable;
+
+-- Print the output of the ErrorLogVTI, make sure it's valid
+SELECT * FROM new org.apache.derby.diag.ErrorLogReader() vti;