FileDocCategorySizeDatePackage
DataSource.javaAPI DocGlassfish v2 API13192Fri May 04 22:36:04 BST 2007com.sun.gjc.spi

DataSource

public class DataSource extends Object implements com.sun.appserv.jdbc.DataSource, DataSource, Serializable, javax.resource.Referenceable
Holds the java.sql.Connection object, which is to be passed to the application program.
version
1.0, 02/07/31
author
Binod P.G

Fields Summary
private javax.resource.spi.ManagedConnectionFactory
mcf
private javax.resource.spi.ConnectionManager
cm
private int
loginTimeout
private PrintWriter
logWriter
private String
description
private Reference
reference
private com.sun.gjc.spi.ConnectionHolder.ConnectionType
conType_
private static Logger
_logger
private boolean
debug
Constructors Summary
public DataSource(javax.resource.spi.ManagedConnectionFactory mcf, javax.resource.spi.ConnectionManager cm)
Constructs DataSource object. This is created by the ManagedConnectionFactory object.

param
mcf ManagedConnectionFactory object creating this object.
param
cm ConnectionManager object either associated with Application server or Resource Adapter.


                		  			   		   			          
          
    	this.mcf = mcf;
    	if (cm == null) {
    	    this.cm = (ConnectionManager) new com.sun.gjc.spi.ConnectionManager();
    	} else {
    	    this.cm = cm;
            conType_ = findConnectionType();
    	}    	
    
Methods Summary
private com.sun.gjc.spi.ConnectionHolder.ConnectionTypefindConnectionType()

        ConnectionType cmType = ConnectionType.STANDARD;
        
        if ( cm instanceof javax.resource.spi.LazyAssociatableConnectionManager ) {
            if (! ((com.sun.enterprise.connectors.ConnectionManagerImpl)cm).
                    getJndiName().endsWith( ConnectorConstants.PM_JNDI_SUFFIX ) ) {
                cmType = ConnectionType.LAZY_ASSOCIATABLE;
            }
        } else if ( cm instanceof 
                javax.resource.spi.LazyEnlistableConnectionManager ) {
            if (! ((com.sun.enterprise.connectors.ConnectionManagerImpl)cm).
                    getJndiName().endsWith( ConnectorConstants.PM_JNDI_SUFFIX) &&
                ! ((com.sun.enterprise.connectors.ConnectionManagerImpl)cm).
                    getJndiName().endsWith( ConnectorConstants.NON_TX_JNDI_SUFFIX)) {
                cmType = ConnectionType.LAZY_ENLISTABLE;
            }
        }

        return cmType;
    
public java.sql.ConnectiongetConnection()
Retrieves the Connection object.

return
Connection object.
throws
SQLException In case of an error.

    	try {
            ConnectionHolder con = (ConnectionHolder) 
                cm.allocateConnection( mcf, null );
            setConnectionType( con );

    	    return (Connection) con;
    	} catch (ResourceException re) {
	    _logger.log(Level.WARNING, "jdbc.exc_get_conn", re.getMessage());
    	    throw new SQLException (re.getMessage());
    	}
    
public java.sql.ConnectiongetConnection(java.lang.String user, java.lang.String pwd)
Retrieves the Connection object.

param
user User name for the Connection.
param
pwd Password for the Connection.
return
Connection object.
throws
SQLException In case of an error.

    	try {
    	    ConnectionRequestInfo info = new ConnectionRequestInfo (user, pwd);
    	    ConnectionHolder con = (ConnectionHolder)
                cm.allocateConnection(mcf,info);
            setConnectionType( con );
            return (Connection) con;
    	} catch (ResourceException re) {
	    _logger.log(Level.WARNING, "jdbc.exc_get_conn", re.getMessage());
    	    throw new SQLException (re.getMessage());
    	}
    
public java.sql.ConnectiongetConnection(java.sql.Connection con)
Retrieves the actual SQLConnection from the Connection wrapper implementation of SunONE application server. If an actual connection is supplied as argument, then it will be just returned.

param
con Connection obtained from Datasource.getConnection()
return
java.sql.Connection implementation of the driver.
throws
java.sql.SQLException If connection cannot be obtained.


        Connection driverCon = con; 
        if (con instanceof com.sun.gjc.spi.ConnectionHolder) {
           driverCon = ((com.sun.gjc.spi.ConnectionHolder) con).getConnection(); 
        } 

        return driverCon;
    
public java.lang.StringgetDescription()
Retrieves the description.

return
Description about the DataSource.

    	return description;
    
public java.io.PrintWritergetLogWriter()
Get the logwriter object.

return
PrintWriter object.
throws
SQLException If a database error occurs.

    	return	logWriter;
    
public intgetLoginTimeout()
Get the login timeout

return
login timeout.
throws
SQLException If a database error occurs.

    	return	loginTimeout;
    
public java.sql.ConnectiongetNonTxConnection()
Gets a connection that is not in the scope of any transaction. This can be used to save performance overhead incurred on enlisting/delisting each connection got, irrespective of whether its required or not. Note here that this meethod does not fit in the connector contract per se.

return
java.sql.Connection
throws
java.sql.SQLException If connection cannot be obtained

        try {
            ConnectionHolder con = (ConnectionHolder)
                ((com.sun.enterprise.connectors.ConnectionManagerImpl)
                cm).allocateNonTxConnection(mcf, null);
            setConnectionType( con, true );
            
            return (Connection) con;
        } catch( ResourceException re ) {
            _logger.log(Level.WARNING, "jdbc.exc_get_conn", re.getMessage());
            throw new SQLException( re.getMessage() );
        }
    
public java.sql.ConnectiongetNonTxConnection(java.lang.String user, java.lang.String password)
Gets a connection that is not in the scope of any transaction. This can be used to save performance overhead incurred on enlisting/delisting each connection got, irrespective of whether its required or not. Note here that this meethod does not fit in the connector contract per se.

param
user User name for authenticating the connection
param
password Password for authenticating the connection
return
java.sql.Connection
throws
java.sql.SQLException If connection cannot be obtained

        try {
            ConnectionRequestInfo cxReqInfo = new ConnectionRequestInfo(user, password);
            ConnectionHolder con = (ConnectionHolder)
                ((com.sun.enterprise.connectors.ConnectionManagerImpl)
	        cm).allocateNonTxConnection( mcf, cxReqInfo);

            setConnectionType( con, true );
            
            return (Connection) con;
        } catch( ResourceException re ) {
            _logger.log(Level.WARNING, "jdbc.exc_get_conn", re.getMessage());
            throw new SQLException( re.getMessage() );
        }
    
public javax.naming.ReferencegetReference()
Get the reference.

return
Referenceobject.

    	return reference;
    
public voidmarkConnectionAsBad(java.sql.Connection conn)
API to mark a connection as bad. If the application can determine that the connection is bad, using this api, it can notify the resource-adapter which inturn will notify the connection-pool. Connection-pool will drop and create a new connection. eg:
com.sun.appserv.jdbc.DataSource ds=
(com.sun.appserv.jdbc.DataSource)context.lookup("dataSource");
Connection con = ds.getConnection();
Statement stmt = null;
try{
stmt = con.createStatement();
stmt.executeUpdate("Update");
}catch(BadConnectionException e){
dataSource.markConnectionAsBad(con) //marking it as bad for removal
}finally{
stmt.close();
con.close(); //Connection will be destroyed while close or Tx completion
}

param
conn java.sql.Connection

        if (conn instanceof ConnectionHolder){
            ConnectionHolder userConn = ((ConnectionHolder)conn);
            userConn.getManagedConnection().markForRemoval(true);
        }
    
private voidsetConnectionType(ConnectionHolder con)

        this.setConnectionType( con, false);
    
private voidsetConnectionType(ConnectionHolder con, boolean isNonTx)

        con.setConnectionType( conType_ );
        if ( conType_ == ConnectionType.LAZY_ASSOCIATABLE ) {
            con.setLazyAssociatableConnectionManager( 
                (javax.resource.spi.LazyAssociatableConnectionManager) cm);
        } else if (conType_ == ConnectionType.LAZY_ENLISTABLE) {
            if ( isNonTx ) {
                //if this is a getNonTxConnection call on the DataSource, we
                //should not LazyEnlist
                con.setConnectionType( ConnectionType.STANDARD );
            } else {
                con.setLazyEnlistableConnectionManager(
                    (javax.resource.spi.LazyEnlistableConnectionManager) cm );
            }
        }
    
public voidsetDescription(java.lang.String description)
Set the description.

param
description Description about the DataSource.

    	this.description = description;
    
public voidsetLogWriter(java.io.PrintWriter logWriter)
Set the logwriter on this object.

param
PrintWriter object.
throws
SQLException If a database error occurs.

    	this.logWriter = logWriter;
    
public voidsetLoginTimeout(int loginTimeout)
Set the login timeout

param
loginTimeout Login timeout.
throws
SQLException If a database error occurs.

    	this.loginTimeout = loginTimeout;
    
public voidsetReference(javax.naming.Reference reference)
Get the reference.

param
reference Reference object.

    	this.reference = reference;