FileDocCategorySizeDatePackage
ConnectionHolder40.javaAPI DocGlassfish v2 API24805Fri May 04 22:36:06 BST 2007com.sun.gjc.spi.jdbc40

ConnectionHolder40

public class ConnectionHolder40 extends com.sun.gjc.spi.base.ConnectionHolder
Holds the java.sql.Connection object, which is to be passed to the application program.
author
Jagadish Ramu
version
1.0, 25-Aug-2006

Fields Summary
protected Properties
defaultClientInfo
protected static final com.sun.enterprise.util.i18n.StringManager
localStrings
protected static final Logger
_logger
protected boolean
jdbc30Connection
Constructors Summary
public ConnectionHolder40(Connection con, com.sun.gjc.spi.ManagedConnection mc, javax.resource.spi.ConnectionRequestInfo cxRequestInfo, boolean jdbc30Connection)
Connection wrapper given to application program

param
con Connection that is wrapped
param
mc ManagedConnection
param
cxRequestInfo Connection Request Information

     
        _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER);
    
        super(con, mc, cxRequestInfo);
        this.jdbc30Connection = jdbc30Connection;
        if(!jdbc30Connection)
            init();
    
Methods Summary
public voidclose()
Closes the logical connection.
Cleans up client specific details

throws
SQLException In case of a database error.

        if (isClosed) {
            if(_logger.isLoggable(Level.FINE)){
                _logger.log(Level.FINE,"jdbc.duplicate_close_connection",this);
            }
            return;
        }
        if (!jdbc30Connection) {
            try {
                checkValidity();
                if (defaultClientInfo == null) {
                    setClientInfo(new Properties());
                } else {
                    setClientInfo(defaultClientInfo);
                }
            } catch (SQLClientInfoException e) {
                _logger.log(Level.WARNING, "jdbc.unable_to_set_client_info", e);
            }
        }
        super.close();
    
public java.sql.ArraycreateArrayOf(java.lang.String typeName, java.lang.Object[] elements)
Factory method for creating Array objects.

param
typeName the SQL name of the type the elements of the array map to. The typeName is a database-specific name which may be the name of a built-in type, a user-defined type or a standard SQL type supported by this database. This is the value returned by Array.getBaseTypeName
param
elements the elements that populate the returned object
return
an Array object whose elements map to the specified SQL type
throws
java.sql.SQLException if a database error occurs, the typeName is null or this method is called on a closed connection
throws
java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support this data type
since
1.6

        checkValidity();
        jdbcPreInvoke();
        return con.createArrayOf(typeName, elements);
    
public java.sql.BlobcreateBlob()
Constructs an object that implements the Blob interface. The object returned initially contains no data. The setBinaryStream and setBytes methods of the Blob interface may be used to add data to the Blob.

return
An object that implements the Blob interface
throws
java.sql.SQLException if an object that implements the Blob interface can not be constructed, this method is called on a closed connection or a database access error occurs.
throws
java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support this data type
since
1.6

        checkValidity();
        jdbcPreInvoke();
        return con.createBlob();
    
public java.sql.ClobcreateClob()
Constructs an object that implements the Clob interface. The object returned initially contains no data. The setAsciiStream, setCharacterStream and setString methods of the Clob interface may be used to add data to the Clob.

return
An object that implements the Clob interface
throws
java.sql.SQLException if an object that implements the Clob interface can not be constructed, this method is called on a closed connection or a database access error occurs.
throws
java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support this data type
since
1.6

        checkValidity();
        jdbcPreInvoke();
        return con.createClob();
    
public java.sql.NClobcreateNClob()
Constructs an object that implements the NClob interface. The object returned initially contains no data. The setAsciiStream, setCharacterStream and setString methods of the NClob interface may be used to add data to the NClob.

return
An object that implements the NClob interface
throws
java.sql.SQLException if an object that implements the NClob interface can not be constructed, this method is called on a closed connection or a database access error occurs.
throws
java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support this data type
since
1.6

        checkValidity();
        jdbcPreInvoke();
        return con.createNClob();
    
public java.sql.SQLXMLcreateSQLXML()
Constructs an object that implements the SQLXML interface. The object returned initially contains no data. The createXmlStreamWriter object and setString method of the SQLXML interface may be used to add data to the SQLXML object.

return
An object that implements the SQLXML interface
throws
java.sql.SQLException if an object that implements the SQLXML interface can not be constructed, this method is called on a closed connection or a database access error occurs.
throws
java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support this data type
since
1.6

        checkValidity();
        jdbcPreInvoke();
        return con.createSQLXML();
    
public java.sql.StructcreateStruct(java.lang.String typeName, java.lang.Object[] attributes)
Factory method for creating Struct objects.

param
typeName the SQL type name of the SQL structured type that this Struct object maps to. The typeName is the name of a user-defined type that has been defined for this database. It is the value returned by Struct.getSQLTypeName.
param
attributes the attributes that populate the returned object
return
a Struct object that maps to the given SQL type and is populated with the given attributes
throws
java.sql.SQLException if a database error occurs, the typeName is null or this method is called on a closed connection
throws
java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support this data type
since
1.6

        checkValidity();
        jdbcPreInvoke();
        return con.createStruct(typeName, attributes);
    
public java.lang.StringgetClientInfo(java.lang.String name)
Returns the value of the client info property specified by name. This method may return null if the specified client info property has not been set and does not have a default value. This method will also return null if the specified client info property name is not supported by the driver.

Applications may use the DatabaseMetaData.getClientInfoProperties method to determine the client info properties supported by the driver.

param
name The name of the client info property to retrieve

return
The value of the client info property specified

throws
java.sql.SQLException if the database server returns an error when fetching the client info value from the database or this method is called on a closed connection

see
java.sql.DatabaseMetaData#getClientInfoProperties
since
1.6

        checkValidity();
        return con.getClientInfo(name);
    
public java.util.PropertiesgetClientInfo()
Returns a list containing the name and current value of each client info property supported by the driver. The value of a client info property may be null if the property has not been set and does not have a default value.

return
A Properties object that contains the name and current value of each of the client info properties supported by the driver.

throws
java.sql.SQLException if the database server returns an error when fetching the client info values from the database or this method is called on a closed connection

since
1.6

        checkValidity();
        return con.getClientInfo();
    
private TgetProxyObject(java.lang.Object actualObject, java.lang.Class[] ifaces)

param
actualObject Object from jdbc vendor connection's unwrap
param
ifaces Interfaces for which proxy is needed
return
Proxy class implmenting the interfaces
throws
SQLException

        T result ;
        InvocationHandler ih;
        try {
            ih = new InvocationHandler() {
                public Object invoke(Object proxy, Method method, Object[] args) throws SQLException,
                        IllegalAccessException, InvocationTargetException {
                    // When close() is called on proxy object, call close() on resource adapter's
                    // Connection Holder instead of physical connection.
                    if (method.getName().equals("close")
                            && method.getParameterTypes().length == 0) {
                        String msg = localStrings.getString("jdbc.close_called_on_proxy_object", actualObject);
                        _logger.log(Level.FINE, msg);
                        ConnectionHolder40.this.close();
                        return null;
                    }

                    // default
                    return method.invoke(actualObject, args);
                }
            };
        } catch (Exception e) {
            throw new SQLException(e.fillInStackTrace());
        }
        result = (T) Proxy.newProxyInstance(actualObject.getClass().getClassLoader(), ifaces, ih);
        return result;
    
protected voidinit()
cache the default client info which can will set back during close()
as this connection may be re-used by connection pool of application server

        try {
            defaultClientInfo = getClientInfo();
        } catch (SQLException e) {
            _logger.log(Level.WARNING, "jdbc.unable_to_get_client_info", e);
        }
    
public booleanisValid(int timeout)
Returns true if the connection has not been closed and is still valid. The driver shall submit a query on the connection or use some other mechanism that positively verifies the connection is still valid when this method is called.

The query submitted by the driver to validate the connection shall be executed in the context of the current transaction.

param
timeout - The time in seconds to wait for the database operation used to validate the connection to complete. If the timeout period expires before the operation completes, this method returns false. A value of 0 indicates a timeout is not applied to the database operation.

return
true if the connection is valid, false otherwise
throws
java.sql.SQLException if the value supplied for timeout is less then 0
see
java.sql.DatabaseMetaData#getClientInfoProperties
since
1.6

        checkValidity();
        return con.isValid(timeout);
    
public booleanisWrapperFor(java.lang.Class iface)
Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does. Returns false otherwise. If this implements the interface then return true, else if this is a wrapper then return the result of recursively calling isWrapperFor on the wrapped object. If this does not implement the interface and is not a wrapper, return false. This method should be implemented as a low-cost operation compared to unwrap so that callers can use this method to avoid expensive unwrap calls that may fail. If this method returns true then calling unwrap with the same argument should succeed.

param
iface a Class defining an interface.
return
true if this implements the interface or directly or indirectly wraps an object that does.
throws
java.sql.SQLException if an error occurs while determining whether this is a wrapper for an object with the given interface.
since
1.6

        checkValidity();
        boolean result ;
        if (iface.isInstance(this)) {
            result = true;
        }else{
            result = con.isWrapperFor(iface);
        }
        return result;
    
public voidsetClientInfo(java.lang.String name, java.lang.String value)
Sets the value of the client info property specified by name to the value specified by value.

Applications may use the DatabaseMetaData.getClientInfoProperties method to determine the client info properties supported by the driver and the maximum length that may be specified for each property.

The driver stores the value specified in a suitable location in the database. For example in a special register, session parameter, or system table column. For efficiency the driver may defer setting the value in the database until the next time a statement is executed or prepared. Other than storing the client information in the appropriate place in the database, these methods shall not alter the behavior of the connection in anyway. The values supplied to these methods are used for accounting, diagnostics and debugging purposes only.

The driver shall generate a warning if the client info name specified is not recognized by the driver.

If the value specified to this method is greater than the maximum length for the property the driver may either truncate the value and generate a warning or generate a SQLClientInfoException. If the driver generates a SQLClientInfoException, the value specified was not set on the connection.

The following are standard client info properties. Drivers are not required to support these properties however if the driver supports a client info property that can be described by one of the standard properties, the standard property name should be used.

  • ApplicationName - The name of the application currently utilizing the connection
  • ClientUser - The name of the user that the application using the connection is performing work for. This may not be the same as the user name that was used in establishing the connection.
  • ClientHostname - The hostname of the computer the application using the connection is running on.

param
name The name of the client info property to set
param
value The value to set the client info property to. If the value is null, the current value of the specified property is cleared.

throws
java.sql.SQLClientInfoException if the database server returns an error while setting the client info value on the database server or this method is called on a closed connection

since
1.6

        try {
            checkValidity();
        } catch (SQLException sqe) {
            SQLClientInfoException sce = new SQLClientInfoException();
            sce.setStackTrace(sqe.getStackTrace());
            throw sce;
        }
        con.setClientInfo(name, value);
    
public voidsetClientInfo(java.util.Properties properties)
Sets the value of the connection's client info properties. The Properties object contains the names and values of the client info properties to be set. The set of client info properties contained in the properties list replaces the current set of client info properties on the connection. If a property that is currently set on the connection is not present in the properties list, that property is cleared. Specifying an empty properties list will clear all of the properties on the connection. See setClientInfo (String, String) for more information.

If an error occurs in setting any of the client info properties, a SQLClientInfoException is thrown. The SQLClientInfoException contains information indicating which client info properties were not set. The state of the client information is unknown because some databases do not allow multiple client info properties to be set atomically. For those databases, one or more properties may have been set before the error occurred.

param
properties the list of client info properties to set

throws
java.sql.SQLClientInfoException if the database server returns an error while setting the clientInfo values on the database server or this method is called on a closed connection

see
java.sql.Connection#setClientInfo(String,String) setClientInfo(String, String)
since
1.6

        try {
            checkValidity();
        } catch (SQLException sqe) {
            SQLClientInfoException sce = new SQLClientInfoException();
            sce.setStackTrace(sqe.getStackTrace());
            throw sce;
        }
        con.setClientInfo(properties);
    
public Tunwrap(java.lang.Class iface)
Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy.

If the receiver implements the interface then the result is the receiver or a proxy for the receiver. If the receiver is a wrapper and the wrapped object implements the interface then the result is the wrapped object or a proxy for the wrapped object. Otherwise return the the result of calling unwrap recursively on the wrapped object or a proxy for that result. If the receiver is not a wrapper and does not implement the interface, then an SQLException is thrown.

param
iface A Class defining an interface that the result must implement.
return
an object that implements the interface. May be a proxy for the actual implementing object.
throws
java.sql.SQLException If no object found that implements the interface
since
1.6

        checkValidity();
        T result = null;
        if (iface.isInstance(this)) { //if iface is "java.sql.Connection"
            result = iface.cast(this); 
        } else if (iface.isInstance(con)) { 
	  //if iface is not "java.sql.Connection" & implemented by native Connection
            Class<T> listIntf[] = new Class[]{iface};
            result = getProxyObject(con, listIntf);
        } else {
            //probably a proxy, delegating to native connection
            result = con.unwrap(iface);
            if(Connection.class.isInstance(result)){
                // rare case : returned object implements both iface & java.sql.Connection
                Class<T> listIntf[] = new Class[]{iface, Connection.class};
                result = getProxyObject(result, listIntf);
            }
        }
        return result;