Modified: 
incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientDataSourceFactory.java
URL: 
http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientDataSourceFactory.java?rev=165585&r1=165584&r2=165585&view=diff
==============================================================================
--- 
incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientDataSourceFactory.java
 (original)
+++ 
incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientDataSourceFactory.java
 Sun May  1 23:25:59 2005
@@ -20,85 +20,74 @@
 
 package org.apache.derby.client;
 
+import org.apache.derby.jdbc.ClientConnectionPoolDataSource;
 import org.apache.derby.jdbc.ClientDataSource;
 import org.apache.derby.jdbc.ClientXADataSource;
-import org.apache.derby.jdbc.ClientConnectionPoolDataSource;
 
 /**
- * The data source factory currrently for ClientDataSource only.
- * This factory will support XA and pooling-enabled data sources in the future.
- * <p>
- * This factory reconstructs a DERBY simple data source object
- * when it is retrieved from JNDI.
- * References are needed since many naming services
- * don't have the ability to store Java objects in their serialized form.
- * When a data source object is bound in this type of naming service the
- * Reference for that object is actually stored by the JNDI implementation,
- * not the data source object itself.
- * <p>
- * A JNDI administrator is responsible for making sure that both the object 
factory and
- * data source implementation classes provided by a JDBC driver vendor are 
accessible to
- * the JNDI service provider at runtime.
- * <p>
- * An object factory implements the javax.naming.spi.ObjectFactory interface. 
This
- * interface contains a single method, getObjectInstance, which is called by a 
JNDI
- * service provider to reconstruct an object when that object is retrieved 
from JNDI. A
- * JDBC driver vendor should provide an object factory as part of their JDBC 
2.0 product.
+ * The data source factory currrently for ClientDataSource only. This factory 
will support XA and pooling-enabled data
+ * sources in the future.
+ * <p/>
+ * This factory reconstructs a DERBY simple data source object when it is 
retrieved from JNDI. References are needed
+ * since many naming services don't have the ability to store Java objects in 
their serialized form. When a data source
+ * object is bound in this type of naming service the Reference for that 
object is actually stored by the JNDI
+ * implementation, not the data source object itself.
+ * <p/>
+ * A JNDI administrator is responsible for making sure that both the object 
factory and data source implementation
+ * classes provided by a JDBC driver vendor are accessible to the JNDI service 
provider at runtime.
+ * <p/>
+ * An object factory implements the javax.naming.spi.ObjectFactory interface. 
This interface contains a single method,
+ * getObjectInstance, which is called by a JNDI service provider to 
reconstruct an object when that object is retrieved
+ * from JNDI. A JDBC driver vendor should provide an object factory as part of 
their JDBC 2.0 product.
  *
  * @see ClientDataSource
- **/
-public class ClientDataSourceFactory implements javax.naming.spi.ObjectFactory
-{
-  static final String className__ = 
"org.apache.derby.jdbc.ClientDataSourceFactory";
-
-  public ClientDataSourceFactory ()
-  { }
-
-  /**
-   * Reconstructs a ClientDataSource object from a JNDI data source reference.
-   * <p>
-   * The getObjectInstance() method is passed a reference that corresponds to 
the object
-   * being retrieved as its first parameter. The other parameters are optional 
in the case of
-   * JDBC data source objects. The object factory should use the information 
contained in
-   * the reference to reconstruct the data source. If for some reason, a data 
source object cannot
-   * be reconstructed from the reference, a value of null may be returned. 
This allows
-   * other object factories that may be registered in JNDI to be tried. If an 
exception is
-   * thrown then no other object factories are tried.
-   *
-   * @param refObj
-   *   The possibly null object containing location or reference information
-   *   that can be used in creating an object.
-   * @param name
-   *   The name of this object relative to nameContext, or null if no name is 
specified.
-   * @param nameContext
-   *   Context relative to which the name parameter is specified,
-   *   or null if name is relative to the default initial context.
-   * @param environment
-   *   Possibly null environment that is used in creating the object.
-   * @return
-   *   object created; null if an object cannot be created
-   */
-  public Object getObjectInstance (Object refObj,
-                                   javax.naming.Name name,
-                                   javax.naming.Context nameContext,
-                                   java.util.Hashtable environment) throws 
java.lang.Exception
-  {
-    javax.naming.Reference ref = (javax.naming.Reference) refObj;
-
-    // Create the proper data source object shell.
-    ClientDataSource ds = null;
-    if (ref.getClassName().equals (ClientDataSource.className__))
-      ds = new ClientDataSource();
-    else if (ref.getClassName().equals (ClientXADataSource.className__))
-      ds = new ClientXADataSource();
-    else if (ref.getClassName().equals 
(ClientConnectionPoolDataSource.className__))
-      ds = new ClientConnectionPoolDataSource();
-    else
-      return null;
+ */
+public class ClientDataSourceFactory implements javax.naming.spi.ObjectFactory 
{
+    static final String className__ = 
"org.apache.derby.jdbc.ClientDataSourceFactory";
+
+    public ClientDataSourceFactory() {
+    }
+
+    /**
+     * Reconstructs a ClientDataSource object from a JNDI data source 
reference.
+     * <p/>
+     * The getObjectInstance() method is passed a reference that corresponds 
to the object being retrieved as its first
+     * parameter. The other parameters are optional in the case of JDBC data 
source objects. The object factory should
+     * use the information contained in the reference to reconstruct the data 
source. If for some reason, a data source
+     * object cannot be reconstructed from the reference, a value of null may 
be returned. This allows other object
+     * factories that may be registered in JNDI to be tried. If an exception 
is thrown then no other object factories
+     * are tried.
+     *
+     * @param refObj      The possibly null object containing location or 
reference information that can be used in
+     *                    creating an object.
+     * @param name        The name of this object relative to nameContext, or 
null if no name is specified.
+     * @param nameContext Context relative to which the name parameter is 
specified, or null if name is relative to the
+     *                    default initial context.
+     * @param environment Possibly null environment that is used in creating 
the object.
+     *
+     * @return object created; null if an object cannot be created
+     */
+    public Object getObjectInstance(Object refObj,
+                                    javax.naming.Name name,
+                                    javax.naming.Context nameContext,
+                                    java.util.Hashtable environment) throws 
java.lang.Exception {
+        javax.naming.Reference ref = (javax.naming.Reference) refObj;
+
+        // Create the proper data source object shell.
+        ClientDataSource ds = null;
+        if (ref.getClassName().equals(ClientDataSource.className__)) {
+            ds = new ClientDataSource();
+        } else if (ref.getClassName().equals(ClientXADataSource.className__)) {
+            ds = new ClientXADataSource();
+        } else if 
(ref.getClassName().equals(ClientConnectionPoolDataSource.className__)) {
+            ds = new ClientConnectionPoolDataSource();
+        } else {
+            return null;
+        }
 
-    // Fill in the data source object shell with values from the jndi 
reference.
-    ds.hydrateFromReference (ref);
+        // Fill in the data source object shell with values from the jndi 
reference.
+        ds.hydrateFromReference(ref);
 
-    return ds;
-  }
+        return ds;
+    }
 }

Modified: 
incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientPooledConnection.java
URL: 
http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientPooledConnection.java?rev=165585&r1=165584&r2=165585&view=diff
==============================================================================
--- 
incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientPooledConnection.java
 (original)
+++ 
incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientPooledConnection.java
 Sun May  1 23:25:59 2005
@@ -19,189 +19,193 @@
 */
 package org.apache.derby.client;
 
-import org.apache.derby.client.net.NetLogWriter;
 import org.apache.derby.client.am.SqlException;
+import org.apache.derby.client.net.NetLogWriter;
 import org.apache.derby.jdbc.ClientDataSource;
 
-public class ClientPooledConnection implements javax.sql.PooledConnection
-{
-  private boolean newPC_ = true;
-
-  private java.util.Vector listeners_ = null;
-  org.apache.derby.client.am.Connection physicalConnection_ = null;
-  org.apache.derby.client.net.NetConnection netPhysicalConnection_ = null;
-  org.apache.derby.client.net.NetXAConnection netXAPhysicalConnection_ = null;
-
-  org.apache.derby.client.am.LogicalConnection logicalConnection_ = null;
-
-  protected org.apache.derby.client.am.LogWriter logWriter_ = null;
-
-  protected int rmId_ = 0;
-
-  // Cached stuff from constructor
-  private ClientDataSource ds_;
-  private String user_;
-  private String password_;
-
-  // Constructor for Non-XA pooled connections.
-  // Using standard Java APIs, a CPDS is passed in.
-  // user/password overrides anything on the ds.
-  public ClientPooledConnection (ClientDataSource ds,
-                              org.apache.derby.client.am.LogWriter logWriter,
-                              String user,
-                              String password) throws SqlException
-  {
-    logWriter_ = logWriter;
-    ds_ = ds;
-    user_ = user;
-    password_ = password;
-    listeners_ = new java.util.Vector();
-
-    netPhysicalConnection_ =  new org.apache.derby.client.net.NetConnection 
((NetLogWriter) logWriter_,
-          user,
-          password,
-          ds,
-          -1,
-          false);
-    physicalConnection_ = netPhysicalConnection_;
-  }
-
-  // Constructor for XA pooled connections only.
-  // Using standard Java APIs, a CPDS is passed in.
-  // user/password overrides anything on the ds.
-  public ClientPooledConnection (ClientDataSource ds,
-                              org.apache.derby.client.am.LogWriter logWriter,
-                              String user,
-                              String password,
-                              int rmId) throws SqlException
-  {
-    logWriter_ = logWriter;
-    ds_ = ds;
-    user_ = user;
-    password_ = password;
-    rmId_ = rmId;
-    listeners_ = new java.util.Vector();
-    netXAPhysicalConnection_ = new org.apache.derby.client.net.NetXAConnection 
((NetLogWriter) logWriter_,
-            user,
-            password,
-            ds,
-            rmId,
-            true);
-    physicalConnection_ = netXAPhysicalConnection_;
-  }
-
-  public ClientPooledConnection (ClientDataSource ds,
-                              org.apache.derby.client.am.LogWriter logWriter
-                              ) throws SqlException
-  {
-    logWriter_ = logWriter;
-    ds_ = ds;
-    listeners_ = new java.util.Vector();
-    netPhysicalConnection_ =  new org.apache.derby.client.net.NetConnection 
((NetLogWriter) logWriter_,
-          null,
-          null,
-          ds,
-          -1,
-          false);
-    physicalConnection_ = netPhysicalConnection_;
-  }
-
-  protected void finalize() throws java.lang.Throwable
-  {
-    if (logWriter_ != null) logWriter_.traceEntry (this, "finalize");
-    close();
-  }
-
-  public synchronized void close() throws SqlException
-  {
-    if (logWriter_ != null) logWriter_.traceEntry (this, "close");
-
-    if ( logicalConnection_ != null ) {
-      logicalConnection_.nullPhysicalConnection();
-      logicalConnection_ = null;
-    }
-
-    if (physicalConnection_ == null) return;
-    try {
-      // Even if the physcial connection is marked closed (in the pool),
-      // this will close its underlying resources.
-      physicalConnection_.closeResources();
-    }
-    finally {
-      physicalConnection_ = null;
-    }
-  }
-
-  // This is the standard API for getting a logical connection handle for a 
pooled connection.
-  // No "resettable" properties are passed, so user, password, and all other 
properties may not change.
-  public synchronized java.sql.Connection getConnection() throws SqlException
-  {
-    if (logWriter_ != null) logWriter_.traceEntry (this, "getConnection");
-    createLogicalConnection ();
-
-       if (!newPC_)
-      physicalConnection_.reset (logWriter_, user_, password_, ds_, false); // 
false means do not recompute
-                                                                            // 
properties from the dataSource
-    // properties don't change
-   else {
-     physicalConnection_.lightReset();    //poolfix
-    }
-    newPC_ = false;
-
-    if (logWriter_ != null) logWriter_.traceExit (this, "getConnection", 
logicalConnection_);
-    return logicalConnection_;
-  }
-
-  private void createLogicalConnection () throws SqlException
-  {
-    if (physicalConnection_ == null)
-      throw new SqlException (logWriter_, "getConnection() is not valid on a 
closed PooledConnection.");
-    // Not the usual case, but if we have an existing logical connection, then 
we must close it by spec.
-    // We close the logical connection without notifying the pool manager that 
this pooled connection is availabe for reuse.
-    if (logicalConnection_ != null) 
logicalConnection_.closeWithoutRecyclingToPool();
-    logicalConnection_ = new org.apache.derby.client.am.LogicalConnection 
(physicalConnection_, this);
-  }
-
-  public synchronized void addConnectionEventListener 
(javax.sql.ConnectionEventListener listener)
-  {
-    if (logWriter_ != null) logWriter_.traceEntry (this, 
"addConnectionEventListener", listener);
-    listeners_.addElement (listener);
-  }
-
-  public synchronized void removeConnectionEventListener 
(javax.sql.ConnectionEventListener listener )
-  {
-    if (logWriter_ != null) logWriter_.traceEntry (this, 
"removeConnectionEventListener", listener);
-    listeners_.removeElement( listener );
-  }
-
-  // Not public, but needs to be visible to am.LogicalConnection
-  public void recycleConnection ()
-  {
-    if (physicalConnection_.agent_.loggingEnabled())
-      physicalConnection_.agent_.logWriter_.traceEntry (this, 
"recycleConnection");
-
-    for (java.util.Enumeration e = listeners_.elements(); e.hasMoreElements(); 
) {
-      javax.sql.ConnectionEventListener listener = 
(javax.sql.ConnectionEventListener) e.nextElement();
-      javax.sql.ConnectionEvent event = new javax.sql.ConnectionEvent (this);
-      listener.connectionClosed (event);
-    }
-  }
-
-  // Not public, but needs to be visible to am.LogicalConnection
-  public void trashConnection (SqlException exception)
-  {
-    for (java.util.Enumeration e = listeners_.elements(); e.hasMoreElements(); 
) {
-      javax.sql.ConnectionEventListener listener = 
(javax.sql.ConnectionEventListener) e.nextElement();
-      javax.sql.ConnectionEvent event = new javax.sql.ConnectionEvent (this, 
exception);
-      listener.connectionErrorOccurred (event);
-    }
-  }
-
-  // Used by LogicalConnection close when it disassociates itself from the 
ClientPooledConnection
-  public synchronized void nullLogicalConnection ()
-  {
-    logicalConnection_ = null;
-  }
+public class ClientPooledConnection implements javax.sql.PooledConnection {
+    private boolean newPC_ = true;
+
+    private java.util.Vector listeners_ = null;
+    org.apache.derby.client.am.Connection physicalConnection_ = null;
+    org.apache.derby.client.net.NetConnection netPhysicalConnection_ = null;
+    org.apache.derby.client.net.NetXAConnection netXAPhysicalConnection_ = 
null;
+
+    org.apache.derby.client.am.LogicalConnection logicalConnection_ = null;
+
+    protected org.apache.derby.client.am.LogWriter logWriter_ = null;
+
+    protected int rmId_ = 0;
+
+    // Cached stuff from constructor
+    private ClientDataSource ds_;
+    private String user_;
+    private String password_;
+
+    // Constructor for Non-XA pooled connections.
+    // Using standard Java APIs, a CPDS is passed in.
+    // user/password overrides anything on the ds.
+    public ClientPooledConnection(ClientDataSource ds,
+                                  org.apache.derby.client.am.LogWriter 
logWriter,
+                                  String user,
+                                  String password) throws SqlException {
+        logWriter_ = logWriter;
+        ds_ = ds;
+        user_ = user;
+        password_ = password;
+        listeners_ = new java.util.Vector();
+
+        netPhysicalConnection_ = new 
org.apache.derby.client.net.NetConnection((NetLogWriter) logWriter_,
+                user,
+                password,
+                ds,
+                -1,
+                false);
+        physicalConnection_ = netPhysicalConnection_;
+    }
+
+    // Constructor for XA pooled connections only.
+    // Using standard Java APIs, a CPDS is passed in.
+    // user/password overrides anything on the ds.
+    public ClientPooledConnection(ClientDataSource ds,
+                                  org.apache.derby.client.am.LogWriter 
logWriter,
+                                  String user,
+                                  String password,
+                                  int rmId) throws SqlException {
+        logWriter_ = logWriter;
+        ds_ = ds;
+        user_ = user;
+        password_ = password;
+        rmId_ = rmId;
+        listeners_ = new java.util.Vector();
+        netXAPhysicalConnection_ = new 
org.apache.derby.client.net.NetXAConnection((NetLogWriter) logWriter_,
+                user,
+                password,
+                ds,
+                rmId,
+                true);
+        physicalConnection_ = netXAPhysicalConnection_;
+    }
+
+    public ClientPooledConnection(ClientDataSource ds,
+                                  org.apache.derby.client.am.LogWriter 
logWriter) throws SqlException {
+        logWriter_ = logWriter;
+        ds_ = ds;
+        listeners_ = new java.util.Vector();
+        netPhysicalConnection_ = new 
org.apache.derby.client.net.NetConnection((NetLogWriter) logWriter_,
+                null,
+                null,
+                ds,
+                -1,
+                false);
+        physicalConnection_ = netPhysicalConnection_;
+    }
+
+    protected void finalize() throws java.lang.Throwable {
+        if (logWriter_ != null) {
+            logWriter_.traceEntry(this, "finalize");
+        }
+        close();
+    }
+
+    public synchronized void close() throws SqlException {
+        if (logWriter_ != null) {
+            logWriter_.traceEntry(this, "close");
+        }
+
+        if (logicalConnection_ != null) {
+            logicalConnection_.nullPhysicalConnection();
+            logicalConnection_ = null;
+        }
+
+        if (physicalConnection_ == null) {
+            return;
+        }
+        try {
+            // Even if the physcial connection is marked closed (in the pool),
+            // this will close its underlying resources.
+            physicalConnection_.closeResources();
+        } finally {
+            physicalConnection_ = null;
+        }
+    }
+
+    // This is the standard API for getting a logical connection handle for a 
pooled connection.
+    // No "resettable" properties are passed, so user, password, and all other 
properties may not change.
+    public synchronized java.sql.Connection getConnection() throws 
SqlException {
+        if (logWriter_ != null) {
+            logWriter_.traceEntry(this, "getConnection");
+        }
+        createLogicalConnection();
+
+        if (!newPC_) {
+            physicalConnection_.reset(logWriter_, user_, password_, ds_, 
false); // false means do not recompute
+        }
+        // properties from the dataSource
+        // properties don't change
+        else {
+            physicalConnection_.lightReset();    //poolfix
+        }
+        newPC_ = false;
+
+        if (logWriter_ != null) {
+            logWriter_.traceExit(this, "getConnection", logicalConnection_);
+        }
+        return logicalConnection_;
+    }
+
+    private void createLogicalConnection() throws SqlException {
+        if (physicalConnection_ == null) {
+            throw new SqlException(logWriter_, "getConnection() is not valid 
on a closed PooledConnection.");
+        }
+        // Not the usual case, but if we have an existing logical connection, 
then we must close it by spec.
+        // We close the logical connection without notifying the pool manager 
that this pooled connection is availabe for reuse.
+        if (logicalConnection_ != null) {
+            logicalConnection_.closeWithoutRecyclingToPool();
+        }
+        logicalConnection_ = new 
org.apache.derby.client.am.LogicalConnection(physicalConnection_, this);
+    }
+
+    public synchronized void 
addConnectionEventListener(javax.sql.ConnectionEventListener listener) {
+        if (logWriter_ != null) {
+            logWriter_.traceEntry(this, "addConnectionEventListener", 
listener);
+        }
+        listeners_.addElement(listener);
+    }
+
+    public synchronized void 
removeConnectionEventListener(javax.sql.ConnectionEventListener listener) {
+        if (logWriter_ != null) {
+            logWriter_.traceEntry(this, "removeConnectionEventListener", 
listener);
+        }
+        listeners_.removeElement(listener);
+    }
+
+    // Not public, but needs to be visible to am.LogicalConnection
+    public void recycleConnection() {
+        if (physicalConnection_.agent_.loggingEnabled()) {
+            physicalConnection_.agent_.logWriter_.traceEntry(this, 
"recycleConnection");
+        }
+
+        for (java.util.Enumeration e = listeners_.elements(); 
e.hasMoreElements();) {
+            javax.sql.ConnectionEventListener listener = 
(javax.sql.ConnectionEventListener) e.nextElement();
+            javax.sql.ConnectionEvent event = new 
javax.sql.ConnectionEvent(this);
+            listener.connectionClosed(event);
+        }
+    }
+
+    // Not public, but needs to be visible to am.LogicalConnection
+    public void trashConnection(SqlException exception) {
+        for (java.util.Enumeration e = listeners_.elements(); 
e.hasMoreElements();) {
+            javax.sql.ConnectionEventListener listener = 
(javax.sql.ConnectionEventListener) e.nextElement();
+            javax.sql.ConnectionEvent event = new 
javax.sql.ConnectionEvent(this, exception);
+            listener.connectionErrorOccurred(event);
+        }
+    }
+
+    // Used by LogicalConnection close when it disassociates itself from the 
ClientPooledConnection
+    public synchronized void nullLogicalConnection() {
+        logicalConnection_ = null;
+    }
 }
 
 

Modified: 
incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientXAConnection.java
URL: 
http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientXAConnection.java?rev=165585&r1=165584&r2=165585&view=diff
==============================================================================
--- 
incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientXAConnection.java
 (original)
+++ 
incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientXAConnection.java
 Sun May  1 23:25:59 2005
@@ -19,120 +19,112 @@
 */
 package org.apache.derby.client;
 
+import java.sql.Connection;
+import javax.sql.XAConnection;
+import javax.transaction.xa.XAResource;
+
 import org.apache.derby.client.am.SqlException;
-import org.apache.derby.jdbc.ClientXADataSource;
-import org.apache.derby.client.net.NetXAConnection;
 import org.apache.derby.client.net.NetLogWriter;
-import java.sql.*;
-import javax.sql.*;
-import javax.transaction.xa.*;
-
-public class ClientXAConnection extends ClientPooledConnection implements 
XAConnection
-{
-  private static int rmIdSeed_ = 95688932; // semi-random starting value for 
rmId
-
-  private ClientXADataSource derbyds_ = null;
-  private XAResource xares_ = null;
-  private org.apache.derby.client.net.NetXAResource netXares_ = null;
-  private boolean fFirstGetConnection_ = true;
-  private Connection logicalCon_; // logicalConnection_ is inherited from 
ClientPooledConnection 
-  // This connection is used to access the indoubt table
-  private NetXAConnection controlCon_ = null;
-
-  public ClientXAConnection (ClientXADataSource ds,
-                          org.apache.derby.client.net.NetLogWriter logWtr,
-                          String          userId,
-                          String          password) throws SqlException
-  {
-    super (ds,logWtr,userId, password, getUnigueRmId());
-    derbyds_ = ds;
-
-    // Have to instantiate a real connection here,
-    // otherwise if XA function is called before the connect happens,
-    // an error will be returned
-    // Note: conApp will be set after this call
-    logicalCon_ = super.getConnection();
-
-    netXares_ = new org.apache.derby.client.net.NetXAResource( this,
-            rmId_, userId, password, netXAPhysicalConnection_ );
-    xares_ = netXares_;
-  }
-
-  public Connection getConnection() throws SqlException
-  {
-    if ( fFirstGetConnection_ )
-    {
-      // Since super.getConnection() has already been called once
-      // in the constructor, we don't need to call it again for the
-      // call of this method.
-      fFirstGetConnection_ = false;
-    }
-    else
-    {
-      // A new connection object is required
-      logicalCon_ = super.getConnection();
-      if( this.physicalConnection_ != null )
-      { // have a physical connection, check if a NetXAResource
-        if( netXAPhysicalConnection_ != null )
-        { // the XAResource is a NetXAResource, re-initialize it
-          netXares_.initForReuse();
+import org.apache.derby.client.net.NetXAConnection;
+import org.apache.derby.jdbc.ClientXADataSource;
+
+public class ClientXAConnection extends ClientPooledConnection implements 
XAConnection {
+    private static int rmIdSeed_ = 95688932; // semi-random starting value for 
rmId
+
+    private ClientXADataSource derbyds_ = null;
+    private XAResource xares_ = null;
+    private org.apache.derby.client.net.NetXAResource netXares_ = null;
+    private boolean fFirstGetConnection_ = true;
+    private Connection logicalCon_; // logicalConnection_ is inherited from 
ClientPooledConnection
+    // This connection is used to access the indoubt table
+    private NetXAConnection controlCon_ = null;
+
+    public ClientXAConnection(ClientXADataSource ds,
+                              org.apache.derby.client.net.NetLogWriter logWtr,
+                              String userId,
+                              String password) throws SqlException {
+        super(ds, logWtr, userId, password, getUnigueRmId());
+        derbyds_ = ds;
+
+        // Have to instantiate a real connection here,
+        // otherwise if XA function is called before the connect happens,
+        // an error will be returned
+        // Note: conApp will be set after this call
+        logicalCon_ = super.getConnection();
+
+        netXares_ = new org.apache.derby.client.net.NetXAResource(this,
+                rmId_, userId, password, netXAPhysicalConnection_);
+        xares_ = netXares_;
+    }
+
+    public Connection getConnection() throws SqlException {
+        if (fFirstGetConnection_) {
+            // Since super.getConnection() has already been called once
+            // in the constructor, we don't need to call it again for the
+            // call of this method.
+            fFirstGetConnection_ = false;
+        } else {
+            // A new connection object is required
+            logicalCon_ = super.getConnection();
+            if (this.physicalConnection_ != null) { // have a physical 
connection, check if a NetXAResource
+                if (netXAPhysicalConnection_ != null) { // the XAResource is a 
NetXAResource, re-initialize it
+                    netXares_.initForReuse();
+                }
+            }
+        }
+        return logicalCon_;
+    }
+
+    private static synchronized int getUnigueRmId() {
+        rmIdSeed_ += 1;
+        return rmIdSeed_;
+    }
+
+    public int getRmId() {
+        return rmId_;
+    }
+
+    public XAResource getXAResource() throws SqlException {
+        if (logWriter_ != null) {
+            logWriter_.traceExit(this, "getXAResource", xares_);
+        }
+
+        return xares_;
+    }
+
+    public ClientXADataSource getDataSource() throws SqlException {
+        if (logWriter_ != null) {
+            logWriter_.traceExit(this, "getDataSource", derbyds_);
+        }
+
+        return derbyds_;
+    }
+
+    public NetXAConnection createControlConnection(NetLogWriter logWriter,
+                                                   String user,
+                                                   String password,
+                                                   
org.apache.derby.jdbc.ClientDataSource dataSource,
+                                                   int rmId,
+                                                   boolean isXAConn) throws 
SqlException {
+
+        controlCon_ = new NetXAConnection(logWriter,
+                user,
+                password,
+                dataSource,
+                rmId,
+                isXAConn);
+        
controlCon_.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
+
+        if (logWriter_ != null) {
+            logWriter_.traceExit(this, "createControlConnection", controlCon_);
         }
-      }
+
+        return controlCon_;
     }
-    return logicalCon_;
-  }
 
-  private static synchronized int getUnigueRmId()
-  {
-    rmIdSeed_ += 1;
-    return rmIdSeed_;
-  }
-
-  public int getRmId()
-  {
-    return rmId_;
-  }
-
-  public XAResource getXAResource() throws SqlException
-  {
-    if (logWriter_ != null) logWriter_.traceExit (this, "getXAResource", 
xares_);
-
-    return xares_;
-  }
-
-  public ClientXADataSource getDataSource() throws SqlException
-  {
-    if (logWriter_ != null) logWriter_.traceExit (this, "getDataSource", 
derbyds_);
-
-    return derbyds_;
-  }
-
-  public NetXAConnection createControlConnection(NetLogWriter logWriter,
-      String user,
-      String password,
-      org.apache.derby.jdbc.ClientDataSource dataSource,
-      int rmId,
-      boolean isXAConn) throws SqlException
-  {
-
-    controlCon_ = new NetXAConnection (
-        logWriter,
-        user,
-        password,
-        dataSource,
-        rmId,
-        isXAConn);
-    
controlCon_.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
-
-    if (logWriter_ != null) logWriter_.traceExit (this, 
"createControlConnection", controlCon_);
-
-    return controlCon_;
-  }
-
-
-  public synchronized void close() throws SqlException
-  {
-    super.close();
-  }
+
+    public synchronized void close() throws SqlException {
+        super.close();
+    }
 }
 

Modified: 
incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientXid.java
URL: 
http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientXid.java?rev=165585&r1=165584&r2=165585&view=diff
==============================================================================
--- 
incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientXid.java 
(original)
+++ 
incubator/derby/code/trunk/java/client/org/apache/derby/client/ClientXid.java 
Sun May  1 23:25:59 2005
@@ -19,202 +19,187 @@
 */
 package org.apache.derby.client;
 
-import javax.transaction.xa.*;
+import javax.transaction.xa.Xid;
 
-public class ClientXid implements Xid
-{
-  //
-  // The format identifier for the Xid. A value of -1 indicates
-  // that the NULLXid
-  //
-  private int          formatID_;
-
-  //
-  // The number of bytes in the global transaction identfier
-  //
-  private int          gtrid_length_;
-
-  //
-  // The number of bytes in the branch qualifier
-  //
-  private int          bqual_length_;
-
-  //
-  // The data for the Xid.
-  // <p> The Xid is made up of two contiguous parts. The first (of size
-  // <b>gtrid_length</b>) is the global transaction identfier and the second
-  // (of size <b>bqual_length</b>) is the branch qualifier.
-  // <p>If the <b>formatID</b> is -1, indicating the NULLXid, the data is
-  //    ignored.
-  //
-  private byte         data_[];
-
-  //
-  // The size of <b>data</b>.
-  //
-  static private final int XidDATASIZE = 128;
-
-  //
-  // The maximum size of the global transaction identifier.
-  //
-  static public final int MAXGTRIDSIZE= 64;
-
-  //
-  // The maximum size of the branch qualifier.
-  //
-  static public  final int MAXBQUALSIZE= 64;
-
-  static private final String hextab_= "0123456789ABCDEF";
-
-
-  //
-  // Constructs a new null Xid.
-  // <p>After construction the data within the Xid should be initialized.
-  //
-  public ClientXid()
-  {
-    data_ = new byte[XidDATASIZE];
-    gtrid_length_ = 0;
-    bqual_length_ = 0;
-    formatID_ = -1;
-  }
-
-  //
-  // another contructor
-  //
-  public ClientXid( int formatID, byte[] gtrid, byte[] bqual )
-  {
-
-    formatID_ = formatID;
-    gtrid_length_ = gtrid.length;
-    bqual_length_ = bqual.length;
-    data_ = new byte[XidDATASIZE];
-    System.arraycopy(gtrid, 0, data_, 0, gtrid_length_);
-    System.arraycopy(bqual, 0, data_, gtrid_length_, bqual_length_);
-  }
-
-  //
-  // Return a string representing this Xid for debuging
-  //
-  // @return the string representation of this Xid
-  //
-  public String toString()
-  {
-     StringBuffer      d;             // Data String, in HeXidecimal
-     String            s;             // Resultant String
-     int               i;
-     int               v;
-     int               L;
-
-     L = gtrid_length_ + bqual_length_;
-     d = new StringBuffer(L+L);
-
-     for( i = 0; i < L; i++ )
-     {
-       // Convert data string to hex
-       v = data_[i] & 0xff;
-       d.append(hextab_.charAt(v/16));
-       d.append(hextab_.charAt(v&15));
-       if ( (i+1)%4 == 0 && (i+1) < L )
-         d.append(" ");
-     }
-
-     s =  "{ClientXid: " +
-                    "formatID("     + formatID_     + "), " +
-                    "gtrid_length(" + gtrid_length_ + "), " +
-                    "bqual_length(" + bqual_length_ + "), " +
-         "data("         + d.toString()            + ")" +
-         "}" ;
-     return s;
-  }
-
-  //
-  // Returns the branch qualifier for this Xid.
-  //
-  // @return the branch qualifier
-  //
-  public byte[] getBranchQualifier()
-  {
-    byte[] bqual = new byte[bqual_length_];
-    System.arraycopy(data_,gtrid_length_,bqual,0,bqual_length_);
-    return bqual;
-  }
-
-  //
-  // Set the branch qualifier for this Xid.
-  //
-  // @param qual a Byte array containing the branch qualifier to be set. If
-  // the size of the array exceeds MAXBQUALSIZE, only the first MAXBQUALSIZE
-  // elements of qual will be used.
-  //
-  public void setBranchQualifier( byte[] qual )
-  {
-    bqual_length_ = qual.length > MAXBQUALSIZE?MAXBQUALSIZE:qual.length;
-    System.arraycopy(qual, 0, data_, gtrid_length_, bqual_length_);
-  }
-
-  //
-  // Obtain the format identifier part of the Xid.
-  //
-  // @return Format identifier. -1 indicates a null Xid
-  //
-  public int getFormatId()
-  {
-    return formatID_;
-  }
-
-  //
-  // Set the format identifier part of the Xid.
-  //
-  // @param Format identifier. -1 indicates a null Xid.
-  //
-  public void setFormatID(int  formatID)
-  {
-    formatID_ = formatID;
-    return;
-  }
-
-  //
-  // Returns the global transaction identifier for this Xid.
-  //
-  // @return the global transaction identifier
-  //
-  public byte[] getGlobalTransactionId()
-  {
-    byte[] gtrid = new byte[gtrid_length_];
-    System.arraycopy(data_, 0, gtrid, 0, gtrid_length_);
-    return gtrid;
-  }
-
-  //
-  // return fields of Xid
-  //
-  public byte[] getData()
-  {
-    return data_;
-  }
-
-  public int getGtridLength()
-  {
-    return gtrid_length_;
-  }
-
-  public int getBqualLength()
-  {
-    return bqual_length_;
-  }
-
-  public int hashCode()
-  {
-    if ( formatID_ == (-1) )
-    {
-      return (-1);
-    }
-    return formatID_ + gtrid_length_ - bqual_length_;
-  }
-
-  public boolean equals(Object obj)
-  {
-    return org.apache.derby.client.net.NetXAResource.xidsEqual(this, 
(javax.transaction.xa.Xid)obj);
-  }
+public class ClientXid implements Xid {
+    //
+    // The format identifier for the Xid. A value of -1 indicates
+    // that the NULLXid
+    //
+    private int formatID_;
+
+    //
+    // The number of bytes in the global transaction identfier
+    //
+    private int gtrid_length_;
+
+    //
+    // The number of bytes in the branch qualifier
+    //
+    private int bqual_length_;
+
+    //
+    // The data for the Xid.
+    // <p> The Xid is made up of two contiguous parts. The first (of size
+    // <b>gtrid_length</b>) is the global transaction identfier and the second
+    // (of size <b>bqual_length</b>) is the branch qualifier.
+    // <p>If the <b>formatID</b> is -1, indicating the NULLXid, the data is
+    //    ignored.
+    //
+    private byte data_[];
+
+    //
+    // The size of <b>data</b>.
+    //
+    static private final int XidDATASIZE = 128;
+
+    //
+    // The maximum size of the global transaction identifier.
+    //
+    static public final int MAXGTRIDSIZE = 64;
+
+    //
+    // The maximum size of the branch qualifier.
+    //
+    static public final int MAXBQUALSIZE = 64;
+
+    static private final String hextab_ = "0123456789ABCDEF";
+
+
+    //
+    // Constructs a new null Xid.
+    // <p>After construction the data within the Xid should be initialized.
+    //
+    public ClientXid() {
+        data_ = new byte[XidDATASIZE];
+        gtrid_length_ = 0;
+        bqual_length_ = 0;
+        formatID_ = -1;
+    }
+
+    //
+    // another contructor
+    //
+    public ClientXid(int formatID, byte[] gtrid, byte[] bqual) {
+
+        formatID_ = formatID;
+        gtrid_length_ = gtrid.length;
+        bqual_length_ = bqual.length;
+        data_ = new byte[XidDATASIZE];
+        System.arraycopy(gtrid, 0, data_, 0, gtrid_length_);
+        System.arraycopy(bqual, 0, data_, gtrid_length_, bqual_length_);
+    }
+
+    //
+    // Return a string representing this Xid for debuging
+    //
+    // @return the string representation of this Xid
+    //
+    public String toString() {
+        StringBuffer d;             // Data String, in HeXidecimal
+        String s;             // Resultant String
+        int i;
+        int v;
+        int L;
+
+        L = gtrid_length_ + bqual_length_;
+        d = new StringBuffer(L + L);
+
+        for (i = 0; i < L; i++) {
+            // Convert data string to hex
+            v = data_[i] & 0xff;
+            d.append(hextab_.charAt(v / 16));
+            d.append(hextab_.charAt(v & 15));
+            if ((i + 1) % 4 == 0 && (i + 1) < L) {
+                d.append(" ");
+            }
+        }
+
+        s = "{ClientXid: " +
+                "formatID(" + formatID_ + "), " +
+                "gtrid_length(" + gtrid_length_ + "), " +
+                "bqual_length(" + bqual_length_ + "), " +
+                "data(" + d.toString() + ")" +
+                "}";
+        return s;
+    }
+
+    //
+    // Returns the branch qualifier for this Xid.
+    //
+    // @return the branch qualifier
+    //
+    public byte[] getBranchQualifier() {
+        byte[] bqual = new byte[bqual_length_];
+        System.arraycopy(data_, gtrid_length_, bqual, 0, bqual_length_);
+        return bqual;
+    }
+
+    //
+    // Set the branch qualifier for this Xid.
+    //
+    // @param qual a Byte array containing the branch qualifier to be set. If
+    // the size of the array exceeds MAXBQUALSIZE, only the first MAXBQUALSIZE
+    // elements of qual will be used.
+    //
+    public void setBranchQualifier(byte[] qual) {
+        bqual_length_ = qual.length > MAXBQUALSIZE ? MAXBQUALSIZE : 
qual.length;
+        System.arraycopy(qual, 0, data_, gtrid_length_, bqual_length_);
+    }
+
+    //
+    // Obtain the format identifier part of the Xid.
+    //
+    // @return Format identifier. -1 indicates a null Xid
+    //
+    public int getFormatId() {
+        return formatID_;
+    }
+
+    //
+    // Set the format identifier part of the Xid.
+    //
+    // @param Format identifier. -1 indicates a null Xid.
+    //
+    public void setFormatID(int formatID) {
+        formatID_ = formatID;
+        return;
+    }
+
+    //
+    // Returns the global transaction identifier for this Xid.
+    //
+    // @return the global transaction identifier
+    //
+    public byte[] getGlobalTransactionId() {
+        byte[] gtrid = new byte[gtrid_length_];
+        System.arraycopy(data_, 0, gtrid, 0, gtrid_length_);
+        return gtrid;
+    }
+
+    //
+    // return fields of Xid
+    //
+    public byte[] getData() {
+        return data_;
+    }
+
+    public int getGtridLength() {
+        return gtrid_length_;
+    }
+
+    public int getBqualLength() {
+        return bqual_length_;
+    }
+
+    public int hashCode() {
+        if (formatID_ == (-1)) {
+            return (-1);
+        }
+        return formatID_ + gtrid_length_ - bqual_length_;
+    }
+
+    public boolean equals(Object obj) {
+        return org.apache.derby.client.net.NetXAResource.xidsEqual(this, 
(javax.transaction.xa.Xid) obj);
+    }
 } // class Xid

Modified: 
incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Agent.java
URL: 
http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Agent.java?rev=165585&r1=165584&r2=165585&view=diff
==============================================================================
--- 
incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Agent.java 
(original)
+++ 
incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Agent.java 
Sun May  1 23:25:59 2005
@@ -20,275 +20,260 @@
 
 package org.apache.derby.client.am;
 
-public abstract class Agent
-{
-  public SqlException accumulatedReadExceptions_ = null;
-
-  private boolean enableBatchedExceptionTracking_;
-  private int batchedExceptionLabelIndex_;
-  private boolean[] batchedExceptionGenerated_;
-
-  Connection connection_; // made friendly for lobs only, refactor !!
-  public SectionManager sectionManager_ = null; // temporarily public, make 
friendly at least !!
-
-  public LogWriter logWriter_ = null;
-
-  final CrossConverters crossConverters_;
-
-  // Exceptions that occur on dnc's implementation of 
SqlException.getMessage() via stored proc
-  // cannot be thrown on the getMessage() invocation because the signature of 
getMessage() does not
-  // allow for throwing an exception.
-  // Therefore, we must save the exception and throw it at our very first 
opportunity.
-  SqlException deferredException_;
-  void checkForDeferredExceptions() throws SqlException
-  {
-    if (deferredException_ != null) {
-      SqlException temp = deferredException_;
-      deferredException_ = null;
-      throw temp;
-    }
-  }
-  public void accumulateDeferredException (SqlException e)
-  {
-    if (deferredException_ == null)
-      deferredException_ = e;
-    else
-      deferredException_.setNextException (e);
-  }
-
-  protected Agent (Connection connection, LogWriter logWriter)
-  {
-    connection_ = connection;
-    logWriter_ = logWriter;
-    crossConverters_ = new CrossConverters (this);
-  }
-
-  protected void resetAgent ( LogWriter logWriter)
-  {
-    // sectionManager_ is set elsewhere
-    accumulatedReadExceptions_ = null;
-    enableBatchedExceptionTracking_ = false;
-    batchedExceptionLabelIndex_ = 0;
-    batchedExceptionGenerated_ = null;
-    logWriter_ = logWriter;
-    deferredException_ = null;
-  }
-
-  public void resetAgent (Connection connection, LogWriter logWriter, int 
loginTimeout, String server,int port) throws SqlException
-  {
-       resetAgent (logWriter);
-       resetAgent_ (logWriter, loginTimeout, server, port);
-  }
-
-  abstract protected void resetAgent_(LogWriter logWriter, int loginTimeout, 
String server,int port) throws SqlException;
-
-  //-------------------- entry points 
------------------------------------------
-
-  public final boolean loggingEnabled () { return 
!org.apache.derby.client.am.Configuration.traceSuspended__ && logWriter_ != 
null; }
-
-  public final void setLogWriter (LogWriter logWriter)
-  {
-    synchronized (connection_) {
-      if (logWriter_ != null) logWriter_.close();
-      logWriter_ = logWriter;
-    }
-  }
-
-  public final java.io.PrintWriter getLogWriter ()
-  { return (logWriter_ == null) ? null : logWriter_.printWriter_; }
-
-  abstract public LogWriter newLogWriter_ (java.io.PrintWriter printWriter, 
int traceLevel);
-
-  
//----------------------------------------------------------------------------
-
-
-  public final void accumulateReadException (SqlException e)
-  {
-    if (enableBatchedExceptionTracking_) {
-      batchedExceptionGenerated_ [batchedExceptionLabelIndex_] = true;
-      labelAsBatchedException (e, batchedExceptionLabelIndex_);
-    }
-    if (accumulatedReadExceptions_ == null)
-      accumulatedReadExceptions_ = e;
-    else
-      accumulatedReadExceptions_.setNextException (e);
-  }
-
-  // Called only for disconnect event
-  public final void accumulateDisconnectException (DisconnectException e)
-  {
-    if (enableBatchedExceptionTracking_) {
-      batchedExceptionGenerated_ [batchedExceptionLabelIndex_] = true;
-      labelAsBatchedException (e, batchedExceptionLabelIndex_);
-    }
-    if (accumulatedReadExceptions_ != null)
-      e.setNextException (accumulatedReadExceptions_);
-
-    accumulatedReadExceptions_ = null;
-  }
-
-  // For now, it looks like the only time we accumulate chain breaking 
exceptions
-  // is for disconnect exceptions.
-  public final void accumulateChainBreakingReadExceptionAndThrow 
(DisconnectException e) throws DisconnectException
-  {
-    accumulateDisconnectException (e); // tacks disconnect exc to end of chain
-    markChainBreakingException_(); // sets a severity code in the NET agent
-    throw e; // disconnect will be caught in Reply classes, and front of 
original chain thrown
-  }
-
-  abstract protected void markChainBreakingException_ (); 
-  abstract public void checkForChainBreakingException_ () throws SqlException;
-
-  private final void enableBatchedExceptionTracking (int batchSize)
-  {
-    enableBatchedExceptionTracking_ = true;
-    batchedExceptionGenerated_ = new boolean[batchSize];
-    batchedExceptionLabelIndex_ = 0;
-  }
-
-  final void disableBatchedExceptionTracking()
-  {
-    enableBatchedExceptionTracking_ = false;
-  }
-
-  public final void setBatchedExceptionLabelIndex (int index)
-  {
-    batchedExceptionLabelIndex_ = index;
-  }
-
-  private final SqlException labelAsBatchedException (SqlException e, int 
index)
-  {
-    SqlException firstInChain = e;
-    while (e != null) {
-      e.setBatchPositionLabel (index);
-      e = (SqlException) e.getNextException();
-    }
-    return firstInChain;
-  }
-
-  protected final void checkForExceptions () throws SqlException
-  {
-    if (accumulatedReadExceptions_ != null) {
-      SqlException e = accumulatedReadExceptions_;
-      accumulatedReadExceptions_ = null;
-      throw e;
-    }
-  }
-
-  // precondition: all batch execute reads have occurred
-  final boolean batchUpdateExceptionGenerated ()
-  {
-    return batchedExceptionGenerated_ [batchedExceptionLabelIndex_];
-  }
-
-  public final void flow (Statement statement) throws SqlException
-  {
-    endWriteChain();
-    flush_();
-    beginReadChain (statement);
-  }
-
-  public final void flowBatch (Statement statement, int batchSize) throws 
SqlException
-  {
-    endBatchedWriteChain();
-    flush_();
-    beginBatchedReadChain (statement, batchSize);
-  }
-
-  public final void flowOutsideUOW () throws SqlException
-  {
-    endWriteChain();
-    flush_();
-    beginReadChainOutsideUOW ();
-  }
-
-  // flush() means to send all chained requests.
-  abstract public void flush_() throws DisconnectException;
-
-  // Close client resources associated with this agent, such as socket and 
streams for the net.
-  abstract public void close_ () throws SqlException;
-
-  public void close () throws SqlException
-  {
-    close_();
-    if (logWriter_ != null) logWriter_.close();
-  }
-
-  public final void disconnectEvent ()
-  {
-    // closes client-side resources associated with database connection
-    try { close(); } catch (SqlException doNothing) {}
-    connection_.completeChainBreakingDisconnect ();
-  }
-
-  public void beginWriteChainOutsideUOW () throws SqlException
-  {
-  }
-
-  public void beginWriteChain (Statement statement) throws SqlException
-  {
-    connection_.writeTransactionStart(statement);
-  }
-
-  public final void beginBatchedWriteChain (Statement statement) throws 
SqlException
-  {
-    beginWriteChain (statement);
-  }
-
-  protected void endWriteChain ()
-  {
-  }
-
-  protected final void endBatchedWriteChain ()
-  {
-  }
-
-  protected void beginReadChain (Statement statement) throws SqlException
-  {
-    connection_.readTransactionStart();
-  }
-
-  protected final void beginBatchedReadChain (Statement statement, int 
batchSize) throws SqlException
-  {
-    enableBatchedExceptionTracking (batchSize);
-    beginReadChain (statement);
-  }
-
-  protected void beginReadChainOutsideUOW () throws SqlException
-  {
-  }
-
-  public void endReadChain () throws SqlException
-  {
-    checkForExceptions();
-  }
-
-  public final void endBatchedReadChain (int[] updateCounts, SqlException 
accumulatedExceptions) throws BatchUpdateException
-  {
-    disableBatchedExceptionTracking();
-    for (int i=0; i < batchedExceptionGenerated_.length; i++) {
-      if (batchedExceptionGenerated_[i])
-        updateCounts[i] = -3;
-    }
-    if (accumulatedExceptions == null) {
-      try {
-        endReadChain();
-      }
-      catch (SqlException e) {
-        accumulatedExceptions = e;
-      }
-    }
-    if (accumulatedExceptions != null) {
-      BatchUpdateException bue =
-          new BatchUpdateException (logWriter_,
-          "Non-atomic batch failure.  The batch was submitted, but " +
-          "at least one exception occurred on an individual member of the 
batch. " +
-          "Use getNextException() to retrieve the exceptions for specific 
batched elements.",
-          updateCounts);
-      bue.setNextException (accumulatedExceptions);
-      throw bue;
+public abstract class Agent {
+    public SqlException accumulatedReadExceptions_ = null;
+
+    private boolean enableBatchedExceptionTracking_;
+    private int batchedExceptionLabelIndex_;
+    private boolean[] batchedExceptionGenerated_;
+
+    Connection connection_; // made friendly for lobs only, refactor !!
+    public SectionManager sectionManager_ = null; // temporarily public, make 
friendly at least !!
+
+    public LogWriter logWriter_ = null;
+
+    final CrossConverters crossConverters_;
+
+    // Exceptions that occur on dnc's implementation of 
SqlException.getMessage() via stored proc
+    // cannot be thrown on the getMessage() invocation because the signature 
of getMessage() does not
+    // allow for throwing an exception.
+    // Therefore, we must save the exception and throw it at our very first 
opportunity.
+    SqlException deferredException_;
+
+    void checkForDeferredExceptions() throws SqlException {
+        if (deferredException_ != null) {
+            SqlException temp = deferredException_;
+            deferredException_ = null;
+            throw temp;
+        }
+    }
+
+    public void accumulateDeferredException(SqlException e) {
+        if (deferredException_ == null) {
+            deferredException_ = e;
+        } else {
+            deferredException_.setNextException(e);
+        }
+    }
+
+    protected Agent(Connection connection, LogWriter logWriter) {
+        connection_ = connection;
+        logWriter_ = logWriter;
+        crossConverters_ = new CrossConverters(this);
+    }
+
+    protected void resetAgent(LogWriter logWriter) {
+        // sectionManager_ is set elsewhere
+        accumulatedReadExceptions_ = null;
+        enableBatchedExceptionTracking_ = false;
+        batchedExceptionLabelIndex_ = 0;
+        batchedExceptionGenerated_ = null;
+        logWriter_ = logWriter;
+        deferredException_ = null;
+    }
+
+    public void resetAgent(Connection connection, LogWriter logWriter, int 
loginTimeout, String server, int port) throws SqlException {
+        resetAgent(logWriter);
+        resetAgent_(logWriter, loginTimeout, server, port);
+    }
+
+    abstract protected void resetAgent_(LogWriter logWriter, int loginTimeout, 
String server, int port) throws SqlException;
+
+    //-------------------- entry points 
------------------------------------------
+
+    public final boolean loggingEnabled() {
+        return !org.apache.derby.client.am.Configuration.traceSuspended__ && 
logWriter_ != null;
+    }
+
+    public final void setLogWriter(LogWriter logWriter) {
+        synchronized (connection_) {
+            if (logWriter_ != null) {
+                logWriter_.close();
+            }
+            logWriter_ = logWriter;
+        }
+    }
+
+    public final java.io.PrintWriter getLogWriter() {
+        return (logWriter_ == null) ? null : logWriter_.printWriter_;
+    }
+
+    abstract public LogWriter newLogWriter_(java.io.PrintWriter printWriter, 
int traceLevel);
+
+    
//----------------------------------------------------------------------------
+
+
+    public final void accumulateReadException(SqlException e) {
+        if (enableBatchedExceptionTracking_) {
+            batchedExceptionGenerated_[batchedExceptionLabelIndex_] = true;
+            labelAsBatchedException(e, batchedExceptionLabelIndex_);
+        }
+        if (accumulatedReadExceptions_ == null) {
+            accumulatedReadExceptions_ = e;
+        } else {
+            accumulatedReadExceptions_.setNextException(e);
+        }
+    }
+
+    // Called only for disconnect event
+    public final void accumulateDisconnectException(DisconnectException e) {
+        if (enableBatchedExceptionTracking_) {
+            batchedExceptionGenerated_[batchedExceptionLabelIndex_] = true;
+            labelAsBatchedException(e, batchedExceptionLabelIndex_);
+        }
+        if (accumulatedReadExceptions_ != null) {
+            e.setNextException(accumulatedReadExceptions_);
+        }
+
+        accumulatedReadExceptions_ = null;
+    }
+
+    // For now, it looks like the only time we accumulate chain breaking 
exceptions
+    // is for disconnect exceptions.
+    public final void 
accumulateChainBreakingReadExceptionAndThrow(DisconnectException e) throws 
DisconnectException {
+        accumulateDisconnectException(e); // tacks disconnect exc to end of 
chain
+        markChainBreakingException_(); // sets a severity code in the NET agent
+        throw e; // disconnect will be caught in Reply classes, and front of 
original chain thrown
+    }
+
+    abstract protected void markChainBreakingException_();
+
+    abstract public void checkForChainBreakingException_() throws SqlException;
+
+    private final void enableBatchedExceptionTracking(int batchSize) {
+        enableBatchedExceptionTracking_ = true;
+        batchedExceptionGenerated_ = new boolean[batchSize];
+        batchedExceptionLabelIndex_ = 0;
+    }
+
+    final void disableBatchedExceptionTracking() {
+        enableBatchedExceptionTracking_ = false;
+    }
+
+    public final void setBatchedExceptionLabelIndex(int index) {
+        batchedExceptionLabelIndex_ = index;
+    }
+
+    private final SqlException labelAsBatchedException(SqlException e, int 
index) {
+        SqlException firstInChain = e;
+        while (e != null) {
+            e.setBatchPositionLabel(index);
+            e = (SqlException) e.getNextException();
+        }
+        return firstInChain;
+    }
+
+    protected final void checkForExceptions() throws SqlException {
+        if (accumulatedReadExceptions_ != null) {
+            SqlException e = accumulatedReadExceptions_;
+            accumulatedReadExceptions_ = null;
+            throw e;
+        }
+    }
+
+    // precondition: all batch execute reads have occurred
+    final boolean batchUpdateExceptionGenerated() {
+        return batchedExceptionGenerated_[batchedExceptionLabelIndex_];
+    }
+
+    public final void flow(Statement statement) throws SqlException {
+        endWriteChain();
+        flush_();
+        beginReadChain(statement);
+    }
+
+    public final void flowBatch(Statement statement, int batchSize) throws 
SqlException {
+        endBatchedWriteChain();
+        flush_();
+        beginBatchedReadChain(statement, batchSize);
+    }
+
+    public final void flowOutsideUOW() throws SqlException {
+        endWriteChain();
+        flush_();
+        beginReadChainOutsideUOW();
+    }
+
+    // flush() means to send all chained requests.
+    abstract public void flush_() throws DisconnectException;
+
+    // Close client resources associated with this agent, such as socket and 
streams for the net.
+    abstract public void close_() throws SqlException;
+
+    public void close() throws SqlException {
+        close_();
+        if (logWriter_ != null) {
+            logWriter_.close();
+        }
+    }
+
+    public final void disconnectEvent() {
+        // closes client-side resources associated with database connection
+        try {
+            close();
+        } catch (SqlException doNothing) {
+        }
+        connection_.completeChainBreakingDisconnect();
+    }
+
+    public void beginWriteChainOutsideUOW() throws SqlException {
+    }
+
+    public void beginWriteChain(Statement statement) throws SqlException {
+        connection_.writeTransactionStart(statement);
+    }
+
+    public final void beginBatchedWriteChain(Statement statement) throws 
SqlException {
+        beginWriteChain(statement);
+    }
+
+    protected void endWriteChain() {
+    }
+
+    protected final void endBatchedWriteChain() {
+    }
+
+    protected void beginReadChain(Statement statement) throws SqlException {
+        connection_.readTransactionStart();
+    }
+
+    protected final void beginBatchedReadChain(Statement statement, int 
batchSize) throws SqlException {
+        enableBatchedExceptionTracking(batchSize);
+        beginReadChain(statement);
+    }
+
+    protected void beginReadChainOutsideUOW() throws SqlException {
+    }
+
+    public void endReadChain() throws SqlException {
+        checkForExceptions();
+    }
+
+    public final void endBatchedReadChain(int[] updateCounts, SqlException 
accumulatedExceptions) throws BatchUpdateException {
+        disableBatchedExceptionTracking();
+        for (int i = 0; i < batchedExceptionGenerated_.length; i++) {
+            if (batchedExceptionGenerated_[i]) {
+                updateCounts[i] = -3;
+            }
+        }
+        if (accumulatedExceptions == null) {
+            try {
+                endReadChain();
+            } catch (SqlException e) {
+                accumulatedExceptions = e;
+            }
+        }
+        if (accumulatedExceptions != null) {
+            BatchUpdateException bue =
+                    new BatchUpdateException(logWriter_,
+                            "Non-atomic batch failure.  The batch was 
submitted, but " +
+                    "at least one exception occurred on an individual member 
of the batch. " +
+                    "Use getNextException() to retrieve the exceptions for 
specific batched elements.",
+                            updateCounts);
+            bue.setNextException(accumulatedExceptions);
+            throw bue;
+        }
     }
-  }
 }
 
 

Modified: 
incubator/derby/code/trunk/java/client/org/apache/derby/client/am/AsciiStream.java
URL: 
http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/am/AsciiStream.java?rev=165585&r1=165584&r2=165585&view=diff
==============================================================================
--- 
incubator/derby/code/trunk/java/client/org/apache/derby/client/am/AsciiStream.java
 (original)
+++ 
incubator/derby/code/trunk/java/client/org/apache/derby/client/am/AsciiStream.java
 Sun May  1 23:25:59 2005
@@ -19,30 +19,28 @@
 */
 package org.apache.derby.client.am;
 
-public class AsciiStream extends java.io.InputStream
-{
-  private java.io.Reader reader_;
-  private String materializedString_; 
-  private int charsRead_ = 0;
+public class AsciiStream extends java.io.InputStream {
+    private java.io.Reader reader_;
+    private String materializedString_;
+    private int charsRead_ = 0;
 
-  public AsciiStream (String materializedString, java.io.Reader reader)
-  {
-    reader_ = reader;
-    materializedString_ = materializedString;
-  }
+    public AsciiStream(String materializedString, java.io.Reader reader) {
+        reader_ = reader;
+        materializedString_ = materializedString;
+    }
 
-   public int read () throws java.io.IOException
-  {
-    int oneChar = reader_.read();
-    ++charsRead_;
-    if (oneChar != -1) // if not eos
-      return 0x00ff & oneChar;
-    else
-      return -1; // end of stream
-  }
+    public int read() throws java.io.IOException {
+        int oneChar = reader_.read();
+        ++charsRead_;
+        if (oneChar != -1) // if not eos
+        {
+            return 0x00ff & oneChar;
+        } else {
+            return -1; // end of stream
+        }
+    }
 
-  public int available()
-  {
-    return materializedString_.length() - charsRead_;
-  }
+    public int available() {
+        return materializedString_.length() - charsRead_;
+    }
 }

Modified: 
incubator/derby/code/trunk/java/client/org/apache/derby/client/am/BatchUpdateException.java
URL: 
http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/am/BatchUpdateException.java?rev=165585&r1=165584&r2=165585&view=diff
==============================================================================
--- 
incubator/derby/code/trunk/java/client/org/apache/derby/client/am/BatchUpdateException.java
 (original)
+++ 
incubator/derby/code/trunk/java/client/org/apache/derby/client/am/BatchUpdateException.java
 Sun May  1 23:25:59 2005
@@ -23,69 +23,74 @@
 import org.apache.derby.client.resources.ResourceKeys;
 
 
-public class BatchUpdateException extends java.sql.BatchUpdateException
-{
+public class BatchUpdateException extends java.sql.BatchUpdateException {
 
- //-----------------constructors-----------------------------------------------
+    
//-----------------constructors-----------------------------------------------
 
-  public BatchUpdateException (LogWriter logWriter, ErrorKey errorKey, int[] 
updateCounts)
-  {
-    super (ResourceUtilities.getResource 
(ResourceKeys.driverOriginationIndicator) +
-           ResourceUtilities.getResource (errorKey.getResourceKey()),
-           errorKey.getSQLState(),
-           errorKey.getErrorCode(),
-           updateCounts);
-    if (logWriter != null) logWriter.traceDiagnosable (this);
-  }
-
-  public BatchUpdateException (LogWriter logWriter, ErrorKey errorKey, 
Object[] args, int[] updateCounts)
-  {
-    super (ResourceUtilities.getResource 
(ResourceKeys.driverOriginationIndicator) +
-           ResourceUtilities.getResource (errorKey.getResourceKey(), args),
-           errorKey.getSQLState(),
-           errorKey.getErrorCode(),
-           updateCounts);
-    if (logWriter != null) logWriter.traceDiagnosable (this);
-  }
-
-  public BatchUpdateException (LogWriter logWriter, ErrorKey errorKey, Object 
arg, int[] updateCounts)
-  {
-    this (logWriter, errorKey, new Object[] {arg}, updateCounts);
-  }
-
-  // Temporary constructor until all error keys are defined.
-  public BatchUpdateException (LogWriter logWriter)
-  {
-    super (null, null, -99999, null);
-    if (logWriter != null) logWriter.traceDiagnosable (this);
-  }
-
-  // Temporary constructor until all error keys are defined.
-  public BatchUpdateException (LogWriter logWriter, int[] updateCounts)
-  {
-    super (null, null, -99999, updateCounts);
-    if (logWriter != null) logWriter.traceDiagnosable (this);
-  }
-
-  // Temporary constructor until all error keys are defined.
-  public BatchUpdateException (LogWriter logWriter, String reason, int[] 
updateCounts)
-  {
-    super (reason, null, -99999, updateCounts);
-    if (logWriter != null) logWriter.traceDiagnosable (this);
-  }
-
-  // Temporary constructor until all error keys are defined.
-  public BatchUpdateException (LogWriter logWriter, String reason, String 
sqlState, int[] updateCounts)
-  {
-    super (reason, sqlState, -99999, updateCounts);
-    if (logWriter != null) logWriter.traceDiagnosable (this);
-  }
-
-  // Temporary constructor until all error keys are defined.
-  public BatchUpdateException (LogWriter logWriter, String reason, String 
sqlState, int errorCode, int[] updateCounts)
-  {
-    super (reason, sqlState, errorCode, updateCounts);
-    if (logWriter != null) logWriter.traceDiagnosable (this);
-  }
+    public BatchUpdateException(LogWriter logWriter, ErrorKey errorKey, int[] 
updateCounts) {
+        
super(ResourceUtilities.getResource(ResourceKeys.driverOriginationIndicator) +
+                ResourceUtilities.getResource(errorKey.getResourceKey()),
+                errorKey.getSQLState(),
+                errorKey.getErrorCode(),
+                updateCounts);
+        if (logWriter != null) {
+            logWriter.traceDiagnosable(this);
+        }
+    }
+
+    public BatchUpdateException(LogWriter logWriter, ErrorKey errorKey, 
Object[] args, int[] updateCounts) {
+        
super(ResourceUtilities.getResource(ResourceKeys.driverOriginationIndicator) +
+                ResourceUtilities.getResource(errorKey.getResourceKey(), args),
+                errorKey.getSQLState(),
+                errorKey.getErrorCode(),
+                updateCounts);
+        if (logWriter != null) {
+            logWriter.traceDiagnosable(this);
+        }
+    }
+
+    public BatchUpdateException(LogWriter logWriter, ErrorKey errorKey, Object 
arg, int[] updateCounts) {
+        this(logWriter, errorKey, new Object[]{arg}, updateCounts);
+    }
+
+    // Temporary constructor until all error keys are defined.
+    public BatchUpdateException(LogWriter logWriter) {
+        super(null, null, -99999, null);
+        if (logWriter != null) {
+            logWriter.traceDiagnosable(this);
+        }
+    }
+
+    // Temporary constructor until all error keys are defined.
+    public BatchUpdateException(LogWriter logWriter, int[] updateCounts) {
+        super(null, null, -99999, updateCounts);
+        if (logWriter != null) {
+            logWriter.traceDiagnosable(this);
+        }
+    }
+
+    // Temporary constructor until all error keys are defined.
+    public BatchUpdateException(LogWriter logWriter, String reason, int[] 
updateCounts) {
+        super(reason, null, -99999, updateCounts);
+        if (logWriter != null) {
+            logWriter.traceDiagnosable(this);
+        }
+    }
+
+    // Temporary constructor until all error keys are defined.
+    public BatchUpdateException(LogWriter logWriter, String reason, String 
sqlState, int[] updateCounts) {
+        super(reason, sqlState, -99999, updateCounts);
+        if (logWriter != null) {
+            logWriter.traceDiagnosable(this);
+        }
+    }
+
+    // Temporary constructor until all error keys are defined.
+    public BatchUpdateException(LogWriter logWriter, String reason, String 
sqlState, int errorCode, int[] updateCounts) {
+        super(reason, sqlState, errorCode, updateCounts);
+        if (logWriter != null) {
+            logWriter.traceDiagnosable(this);
+        }
+    }
 }
 

Modified: 
incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java
URL: 
http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java?rev=165585&r1=165584&r2=165585&view=diff
==============================================================================
--- incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java 
(original)
+++ incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java 
Sun May  1 23:25:59 2005
@@ -20,257 +20,283 @@
 
 package org.apache.derby.client.am;
 
-public class Blob extends Lob implements java.sql.Blob
-{
-  
//-----------------------------state------------------------------------------
-
-  byte[] binaryString_ = null;
-
-  // Only used for input purposes.  For output, each getBinaryStream call
-  // must generate an independent stream.
-  java.io.InputStream binaryStream_ = null;
-  int dataOffset_;
-
-  
//---------------------constructors/finalizer---------------------------------
-
-  public Blob (byte[] binaryString,
-               Agent agent,
-               int dataOffset)
-  {
-    super (agent);
-    binaryString_ = binaryString;
-    dataType_ |= BINARY_STRING;
-    sqlLength_ =  binaryString.length - dataOffset;
-    lengthObtained_ = true;
-    dataOffset_ = dataOffset;
-  }
-
-  // CTOR for input:
-  public Blob (Agent agent,
-               java.io.InputStream binaryStream,
-               int length)
-  {
-    super (agent);
-    binaryStream_ = binaryStream;
-    dataType_ |= BINARY_STREAM;
-    sqlLength_ = length;
-    lengthObtained_ = true;
-  }
-
-  // ---------------------------jdbc 
2------------------------------------------
-
-  public long length () throws SqlException
-  {
-    synchronized (agent_.connection_) {
-      if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, 
"length");
-      long retVal = super.sqlLength();
-      if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, 
"length", retVal);
-      return retVal;
-    }
-  }
-
-  // can return an array that may be have a length shorter than the supplied
-  // length (no padding occurs)
-  public byte[] getBytes (long pos, int length) throws SqlException
-  {
-    synchronized (agent_.connection_) {
-      if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, 
"getBytes", (int) pos, length);
-      if ((pos <= 0) || (length < 0))
-        throw new SqlException (agent_.logWriter_, "Invalid position " + pos 
+" or length " + length);
-      byte[] retVal = getBytesX (pos, length);
-      if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, 
"getBytes", retVal);
-      return retVal;
-    }
-  }
-
-  private byte[] getBytesX (long pos, int length) throws SqlException
-  {
-    checkForClosedConnection ();
-
-    // we may need to check for overflow on this cast
-    long actualLength = Math.min (this.length() - pos + 1, (long) length);
-
-    byte[] retVal = new byte[(int) actualLength];
-    System.arraycopy (binaryString_, (int) pos + dataOffset_ - 1, retVal, 0, 
(int) actualLength);
-    return retVal;
-  }
-
-
-  public java.io.InputStream getBinaryStream () throws SqlException
-  {
-    synchronized (agent_.connection_) {
-      if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, 
"getBinaryStream");
-      java.io.InputStream retVal = getBinaryStreamX();
-      if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, 
"getBinaryStream", retVal);
-      return retVal;
-    }
-  }
-
-  private java.io.InputStream getBinaryStreamX () throws SqlException
-  {
-    checkForClosedConnection ();
-
-    if (isBinaryStream ())    // this Lob is used for input
-      return binaryStream_;
-
-    return  new java.io.ByteArrayInputStream (binaryString_, dataOffset_, 
binaryString_.length - dataOffset_);
-  }
-
-  public long position (byte[] pattern, long start) throws SqlException
-  {
-    synchronized (agent_.connection_) {
-      if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, 
"position(byte[], long)", pattern, start);
-      if (pattern == null) throw new SqlException (agent_.logWriter_, "Search 
pattern cannot be null.");
-      long pos = positionX (pattern, start);
-      if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, 
"position(byte[], long)", pos);
-      return pos;
-    }
-  }
-
-  private long positionX (byte[] pattern, long start) throws SqlException
-  {
-    checkForClosedConnection ();
-
-    return binaryStringPosition(pattern, start);
-  }
-
-  public long position (java.sql.Blob pattern, long start) throws SqlException
-  {
-    synchronized (agent_.connection_) {
-      if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, 
"position(Blob, long)", pattern, start);
-      if (pattern == null) throw new SqlException (agent_.logWriter_, "Search 
pattern cannot be null.");
-      long pos = positionX (pattern, start);
-      if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, 
"position(Blob, long)", pos);
-      return pos;
-    }
-  }
-
-  private long positionX (java.sql.Blob pattern, long start) throws 
SqlException
-  {
-    checkForClosedConnection ();
+public class Blob extends Lob implements java.sql.Blob {
+    
//-----------------------------state------------------------------------------
 
-    try {
-      return binaryStringPosition(pattern.getBytes(1L, (int)pattern.length()) 
, start);
-    }
-    catch (java.sql.SQLException e) {
-      throw new SqlException (agent_.logWriter_, e.getMessage());
+    byte[] binaryString_ = null;
+
+    // Only used for input purposes.  For output, each getBinaryStream call
+    // must generate an independent stream.
+    java.io.InputStream binaryStream_ = null;
+    int dataOffset_;
+
+    
//---------------------constructors/finalizer---------------------------------
+
+    public Blob(byte[] binaryString,
+                Agent agent,
+                int dataOffset) {
+        super(agent);
+        binaryString_ = binaryString;
+        dataType_ |= BINARY_STRING;
+        sqlLength_ = binaryString.length - dataOffset;
+        lengthObtained_ = true;
+        dataOffset_ = dataOffset;
+    }
+
+    // CTOR for input:
+    public Blob(Agent agent,
+                java.io.InputStream binaryStream,
+                int length) {
+        super(agent);
+        binaryStream_ = binaryStream;
+        dataType_ |= BINARY_STREAM;
+        sqlLength_ = length;
+        lengthObtained_ = true;
+    }
+
+    // ---------------------------jdbc 
2------------------------------------------
+
+    public long length() throws SqlException {
+        synchronized (agent_.connection_) {
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceEntry(this, "length");
+            }
+            long retVal = super.sqlLength();
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceExit(this, "length", retVal);
+            }
+            return retVal;
+        }
+    }
+
+    // can return an array that may be have a length shorter than the supplied
+    // length (no padding occurs)
+    public byte[] getBytes(long pos, int length) throws SqlException {
+        synchronized (agent_.connection_) {
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceEntry(this, "getBytes", (int) pos, 
length);
+            }
+            if ((pos <= 0) || (length < 0)) {
+                throw new SqlException(agent_.logWriter_, "Invalid position " 
+ pos + " or length " + length);
+            }
+            byte[] retVal = getBytesX(pos, length);
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceExit(this, "getBytes", retVal);
+            }
+            return retVal;
+        }
+    }
+
+    private byte[] getBytesX(long pos, int length) throws SqlException {
+        checkForClosedConnection();
+
+        // we may need to check for overflow on this cast
+        long actualLength = Math.min(this.length() - pos + 1, (long) length);
+
+        byte[] retVal = new byte[(int) actualLength];
+        System.arraycopy(binaryString_, (int) pos + dataOffset_ - 1, retVal, 
0, (int) actualLength);
+        return retVal;
+    }
+
+
+    public java.io.InputStream getBinaryStream() throws SqlException {
+        synchronized (agent_.connection_) {
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceEntry(this, "getBinaryStream");
+            }
+            java.io.InputStream retVal = getBinaryStreamX();
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceExit(this, "getBinaryStream", retVal);
+            }
+            return retVal;
+        }
+    }
+
+    private java.io.InputStream getBinaryStreamX() throws SqlException {
+        checkForClosedConnection();
+
+        if (isBinaryStream())    // this Lob is used for input
+        {
+            return binaryStream_;
+        }
+
+        return new java.io.ByteArrayInputStream(binaryString_, dataOffset_, 
binaryString_.length - dataOffset_);
+    }
+
+    public long position(byte[] pattern, long start) throws SqlException {
+        synchronized (agent_.connection_) {
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceEntry(this, "position(byte[], long)", 
pattern, start);
+            }
+            if (pattern == null) {
+                throw new SqlException(agent_.logWriter_, "Search pattern 
cannot be null.");
+            }
+            long pos = positionX(pattern, start);
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceExit(this, "position(byte[], long)", 
pos);
+            }
+            return pos;
+        }
+    }
+
+    private long positionX(byte[] pattern, long start) throws SqlException {
+        checkForClosedConnection();
+
+        return binaryStringPosition(pattern, start);
+    }
+
+    public long position(java.sql.Blob pattern, long start) throws 
SqlException {
+        synchronized (agent_.connection_) {
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceEntry(this, "position(Blob, long)", 
pattern, start);
+            }
+            if (pattern == null) {
+                throw new SqlException(agent_.logWriter_, "Search pattern 
cannot be null.");
+            }
+            long pos = positionX(pattern, start);
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceExit(this, "position(Blob, long)", pos);
+            }
+            return pos;
+        }
+    }
+
+    private long positionX(java.sql.Blob pattern, long start) throws 
SqlException {
+        checkForClosedConnection();
+
+        try {
+            return binaryStringPosition(pattern.getBytes(1L, (int) 
pattern.length()), start);
+        } catch (java.sql.SQLException e) {
+            throw new SqlException(agent_.logWriter_, e.getMessage());
+        }
     }
-  }
 
     // -------------------------- JDBC 3.0 -----------------------------------
 
 
-    public int setBytes (long pos, byte[] bytes) throws SqlException
-    {
-      synchronized (agent_.connection_) {
-        if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, 
"setBytes", (int) pos, bytes);
-        int length = setBytesX (pos, bytes, 0, bytes.length);
-        if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, 
"setBytes", length);
-        return length;
-      }
-    }
-
-    public int setBytes (long pos, byte[] bytes, int offset, int len) throws 
SqlException
-    {
-      synchronized (agent_.connection_) {
-        if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, 
"setBytes", (int) pos, bytes, offset, len);
-        int length = setBytesX (pos, bytes, offset, len);
-        if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, 
"setBytes", length);
-        return length;
-      }
-    }
+    public int setBytes(long pos, byte[] bytes) throws SqlException {
+        synchronized (agent_.connection_) {
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceEntry(this, "setBytes", (int) pos, 
bytes);
+            }
+            int length = setBytesX(pos, bytes, 0, bytes.length);
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceExit(this, "setBytes", length);
+            }
+            return length;
+        }
+    }
+
+    public int setBytes(long pos, byte[] bytes, int offset, int len) throws 
SqlException {
+        synchronized (agent_.connection_) {
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceEntry(this, "setBytes", (int) pos, 
bytes, offset, len);
+            }
+            int length = setBytesX(pos, bytes, offset, len);
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceExit(this, "setBytes", length);
+            }
+            return length;
+        }
+    }
+
+    public int setBytesX(long pos, byte[] bytes, int offset, int len) throws 
SqlException {
+        int length = 0;
+        if ((int) pos <= 0 || pos > binaryString_.length - dataOffset_) {
+            throw new SqlException(agent_.logWriter_, "Invalid position " + pos
+                    + " , offset " + offset + " or length " + len);
+        }
+        if ((offset < 0) || offset > bytes.length || len < 0) {
+            throw new SqlException(agent_.logWriter_, "Invalid position " + pos
+                    + " , offset " + offset + " or length " + len);
+        }
+        if (len == 0) {
+            return 0;
+        }
+        length = Math.min((bytes.length - offset), len);
+        if ((binaryString_.length - dataOffset_ - (int) pos + 1) < length) {
+            byte newbuf[] = new byte[(int) pos + length + dataOffset_ - 1];
+            System.arraycopy(binaryString_, 0, newbuf, 0, 
binaryString_.length);
+            binaryString_ = newbuf;
+        }
 
-    public int setBytesX (long pos, byte[] bytes, int offset, int len) throws 
SqlException
-    {
-      int length = 0;
-      if ( (int) pos <= 0 || pos > binaryString_.length - dataOffset_)
-        throw new SqlException(agent_.logWriter_, "Invalid position " + pos 
-                     + " , offset " + offset + " or length " + len);
-      if ( (offset < 0) || offset > bytes.length || len < 0 )
-        throw new SqlException(agent_.logWriter_, "Invalid position " + pos 
-                             + " , offset " + offset + " or length " + len);
-      if (len == 0)
-        return 0;
-      length = Math.min((bytes.length  - offset ),len);
-      if ((binaryString_.length - dataOffset_ - (int) pos + 1) < length) {
-        byte newbuf[] = new byte[(int)pos + length + dataOffset_-1];
-        System.arraycopy(binaryString_,0,newbuf,0,binaryString_.length);
-        binaryString_ = newbuf;
-      }
-
-      System.arraycopy(bytes,offset, binaryString_, (int)pos + dataOffset_ -1, 
length);
-      binaryStream_ = new java.io.ByteArrayInputStream(binaryString_);
-      sqlLength_ = binaryString_.length - dataOffset_;
-      return length;
-    }
-
-    public java.io.OutputStream setBinaryStream (long pos) throws SqlException
-    {
-      synchronized (agent_.connection_) {
-          if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, 
"setBinaryStream", (int) pos);
-          BlobOutputStream outStream = new BlobOutputStream (this,pos);
-
-          if (agent_.loggingEnabled()) agent_.logWriter_.traceExit (this, 
"setBinaryStream", outStream);
-          return outStream;
-      }
-    }
-
-    public void truncate (long len) throws SqlException
-    {
-      synchronized (agent_.connection_) {
-        if (agent_.loggingEnabled()) agent_.logWriter_.traceEntry (this, " 
truncate", (int) len);
-        if (len < 0 || len > this.length())
-          throw new SqlException (agent_.logWriter_, "Invalid length " + len);
-        if (len == this.length())
-          return;
-        long newLength = (int)len + dataOffset_;
-        byte newbuf[] = new byte[(int)len + dataOffset_];
-        System.arraycopy(binaryString_,0,newbuf,0,(int)newLength);
-        binaryString_ = newbuf;
+        System.arraycopy(bytes, offset, binaryString_, (int) pos + dataOffset_ 
- 1, length);
         binaryStream_ = new java.io.ByteArrayInputStream(binaryString_);
         sqlLength_ = binaryString_.length - dataOffset_;
-      }
+        return length;
     }
 
-  //------------------ Material layer event callback methods 
-------------------
+    public java.io.OutputStream setBinaryStream(long pos) throws SqlException {
+        synchronized (agent_.connection_) {
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceEntry(this, "setBinaryStream", (int) 
pos);
+            }
+            BlobOutputStream outStream = new BlobOutputStream(this, pos);
+
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceExit(this, "setBinaryStream", 
outStream);
+            }
+            return outStream;
+        }
+    }
+
+    public void truncate(long len) throws SqlException {
+        synchronized (agent_.connection_) {
+            if (agent_.loggingEnabled()) {
+                agent_.logWriter_.traceEntry(this, " truncate", (int) len);
+            }
+            if (len < 0 || len > this.length()) {
+                throw new SqlException(agent_.logWriter_, "Invalid length " + 
len);
+            }
+            if (len == this.length()) {
+                return;
+            }
+            long newLength = (int) len + dataOffset_;
+            byte newbuf[] = new byte[(int) len + dataOffset_];
+            System.arraycopy(binaryString_, 0, newbuf, 0, (int) newLength);
+            binaryString_ = newbuf;
+            binaryStream_ = new java.io.ByteArrayInputStream(binaryString_);
+            sqlLength_ = binaryString_.length - dataOffset_;
+        }
+    }
+
+    //------------------ Material layer event callback methods 
-------------------
+
+    //---------------------------- helper methods 
--------------------------------
+    public boolean isBinaryString() {
+        return ((dataType_ & BINARY_STRING) == BINARY_STRING);
+    }
+
+    public boolean isBinaryStream() {
+        return ((dataType_ & BINARY_STREAM) == BINARY_STREAM);
+    }
+
+    public byte[] getBinaryString() {
+        return binaryString_;
+    }
+
+    protected long binaryStringPosition(byte[] pattern, long start) {
+        // perform a local byte string search, starting at start
+        // check that the range of comparison is valid
+        int index = (int) start + dataOffset_ - 1; // api start begins at 1
+
+        while (index + pattern.length <= binaryString_.length) {
+            if (isSubString(pattern, index)) {
+                return (long) (index - dataOffset_ + 1); // readjust for api 
indexing
+            }
+            index++;
+        }
+        return -1L; // not found
+    }
+
+    // precondition: binaryString_ is long enough for the comparison
+    protected boolean isSubString(byte[] pattern, int index) {
+        for (int i = 0; i < pattern.length; i++, index++) {
+            if (pattern[i] != binaryString_[index]) {
+                return false;
+            }
+        }
 
-  //---------------------------- helper methods 
--------------------------------
-  public boolean isBinaryString ()
-  {
-    return ((dataType_ & BINARY_STRING) == BINARY_STRING);
-  }
-
-  public boolean isBinaryStream ()
-  {
-    return ((dataType_ & BINARY_STREAM) == BINARY_STREAM);
-  }
-
-  public byte[] getBinaryString ()
-  {
-    return binaryString_;
-  }
-
-  protected long binaryStringPosition (byte[] pattern, long start)
-  {
-    // perform a local byte string search, starting at start
-    // check that the range of comparison is valid
-    int index = (int) start + dataOffset_ - 1; // api start begins at 1
-
-    while (index + pattern.length <= binaryString_.length) {
-      if (isSubString (pattern, index))
-        return (long)(index - dataOffset_ + 1); // readjust for api indexing
-      index++;
-    }
-    return -1L; // not found
-  }
-
-  // precondition: binaryString_ is long enough for the comparison
-  protected boolean isSubString (byte[] pattern, int index)
-  {
-    for (int i = 0; i < pattern.length; i++, index++)
-      if (pattern[i] != binaryString_[index])
-        return false;
-
-    return true;
-  }
+        return true;
+    }
 }


Reply via email to