FileDocCategorySizeDatePackage
ConnectionImpl.javaAPI DocGlassfish v2 API25817Fri May 04 22:35:02 BST 2007com.sun.jdo.spi.persistence.support.sqlstore.connection

ConnectionImpl

public class ConnectionImpl extends Object implements Connection, com.sun.jdo.spi.persistence.utility.Linkable
This class implements the java.sql.Connection interface, which is part of the JDBC API. You should use the java.sql.Connection interface as an object type instead of this class.

Fields Summary
private Connection
connection
private String
url
private String
userName
com.sun.jdo.spi.persistence.utility.Linkable
previous
com.sun.jdo.spi.persistence.utility.Linkable
next
private boolean
pooled
private com.sun.jdo.api.persistence.support.Transaction
transaction
boolean
freePending
ConnectionManager
connectionManager
private static com.sun.jdo.spi.persistence.utility.logging.Logger
logger
The logger
Constructors Summary
public ConnectionImpl(Connection conn, String url, String userName, ConnectionManager connMgr)
Create a new ConnectionImpl object and keep a reference to the corresponding JDBC Connection.

param
conn Connection



                            
          
                            
        super();
        this.connection = conn;
        this.url = url;
        this.userName = userName;
        this.previous = null;
        this.next = null;
        this.pooled = false;
        this.transaction = null;
        this.freePending = false;
        //		this.resource = null;
        this.connectionManager = connMgr;
    
Methods Summary
synchronized voidcheckXact()
Check the TransactionContext. If the current thread is in a transaction, and the Connection is not participating in a transaction; the Connection should register a resource with the current thread's transaction. If the current thread is in a transaction, and the Connection is participating in a transaction; the thread's transaction and the Connection's transaction must be the same transaction. Anything else is an error.

exception
SQLException if the current thread's transaction and the Connection's transaction do not match.
ForteInternal

        /*	RESOLVE: Need to reimplement this
	         * 	for CMP environment if any

		Transaction		tran;

		try
		{
   			tran = ThreadContext.transactionContext().getTransaction();
		}
		catch (SystemException ex)
		{
			tran = null;
		}

		if (tran == null)
		{
			if (this.transaction != null)
			{
				throw new SQLException("Thread is no longer in transaction."); // NOI18N
			}
			else
			{
				// No transaction.
				return;
			}
		}
		else if (this.transaction == null)
		{
			// This is a new transaction.
			this.transaction = tran;
			this.resource = new ForteJDBCResource(this);
			try
			{
				TranManager tm = ThreadContext.partition().getTranManager();
				tm.enlistResource(this.resource);
				this.setAutoCommit(false);
			}
			catch (SQLException ex)
			{
			}
			catch (Throwable ex)
			{
				// XXX This shouldn't happen. XXX
			}
			this.connectionManager.associateXact(tran, this);
		}
		else if (!tran.equals(this.transaction))
		{
			throw new SQLException("Wrong Transaction."); // NOI18N
		}
		else
		{
			// Connection and thread are in the same transaction.
			return;
		}
		*/
    
public synchronized voidclearWarnings()

        try {
            this.connection.clearWarnings();
        } catch (SQLException se) {
            throw se;
        }
    
private voidclearXact()
Clear this ConnectionImpl of any knowledge of a transaction. Also informs the parent ConnectionManager to clear its knowledge of the transaction as well.

        logger.finest("sqlstore.connectionimpl.clearxact"); // NOI18N

        try {
            if (this.freePending) {
                this.freePending = false;
                if (this.pooled) {
                    this.connectionManager.disassociateXact(this.transaction, this, true);
                    logger.finest("sqlstore.connectionimpl.pendingdisassocxact"); // NOI18N
                } else {
                    this.connectionManager.disassociateXact(this.transaction, this, false);
                    // Make sure the last things done are the only things
                    // that can throw exceptions.
                    this.connection.close();

                    logger.finest("sqlstore.connectionimpl.clearxact.close"); // NOI18N
                }
            } else {
                this.connectionManager.disassociateXact(this.transaction, this, false);
                logger.finest("sqlstore.connectionimpl.clearxact.disassocxact"); // NOI18N
            }
            this.connection.setAutoCommit(true);
        } catch (SQLException ex) {
            // XXX Need to recover from a bad connection. XXX
        } finally {
            //			this.resource = null;
            this.transaction = null;
        }
    
public synchronized voidclose()

        if (EJBHelper.isManaged()) {
            logger.finest("sqlstore.connectionimpl.close"); // NOI18N

            // ignore - this can happen in test 'ejb' only
            return;
        }

        closeInternal();
    
private synchronized voidcloseInternal()


        boolean debug = logger.isLoggable(Logger.FINEST);

        ConnectionImpl conn = (ConnectionImpl) this;

        if (debug) {
            logger.finest("sqlstore.connectionimpl.close_arg",conn); // NOI18N
        }

        try {
            conn.connectionManager.busyList.removeFromList((Linkable) conn);
            if (conn.xactPending() == true) {
                conn.setFreePending(true);
                if (debug) {
                    logger.finest("sqlstore.connectionimpl.close.freepending"); // NOI18N
                }
            } else if ((conn.getPooled() == true) && (conn.connectionManager.shutDownPending == false)) {
                conn.connectionManager.freeList.insertAtTail((Linkable) conn);
                if (debug) {
                    logger.finest("sqlstore.connectionimpl.close.putfreelist"); // NOI18N
                }
            } else {
                if (EJBHelper.isManaged()) {
                    // RESOLVE: do we need it here?
                    this.connection.close();
                    if (debug) {
                        logger.finest("sqlstore.connectionimpl.close.exit"); // NOI18N
                    }
                } else {
                    // Save reference to this connection and close it only when
                    // another free becomes available. This reduces time to
                    // get a new connection.
                    this.connectionManager.replaceFreeConnection(this);
                    if (debug) {
                        logger.finest("sqlstore.connectionimpl.close.replaced"); // NOI18N
                    }
                }
            }

        } catch (SQLException se) {
            throw se;
        }
    
public synchronized voidcommit()


        try {
            this.connection.commit();
            if (this.freePending) {
                if (this.connectionManager.shutDownPending) {
                    try {
                        this.connection.close();
                        logger.finest("sqlstore.connectionimpl.commit"); // NOI18N
                    } catch (SQLException se) {
                        ;
                    }
                } else {
                    this.freePending = false;
                    this.connectionManager.freeList.insertAtTail(this);
                }
            }
            if (EJBHelper.isManaged()) {
                // this is for use with test 'ejb' only.
                closeInternal();
            }
        } catch (SQLException se) {
            throw se;
        }
    
public synchronized java.sql.StatementcreateStatement()

        StatementImpl fstmt;

        try {
            this.checkXact();
            fstmt = new StatementImpl(this, this.connection.createStatement());
        } catch (SQLException se) {
            throw se;
        }

        return ((Statement) fstmt);
    
public java.sql.StatementcreateStatement(int resultSetType, int resultSetConcurrency)

        return (null);
    
public synchronized java.sql.StatementcreateStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)


        throw new UnsupportedOperationException();
    
protected voidfinalize()

        try {
            this.connection.close();
            logger.finest("sqlstore.connectionimpl.finalize"); // NOI18N
        } catch (SQLException se) {
            ;
        }
    
public synchronized booleangetAutoCommit()

        try {
            return (this.connection.getAutoCommit());
        } catch (SQLException se) {
            throw se;
        }
    
public synchronized java.lang.StringgetCatalog()

        try {
            return (this.connection.getCatalog());
        } catch (SQLException se) {
            throw se;
        }
    
synchronized booleangetFreePending()
Get the value of the free-pending attribute.

        return (this.freePending);
    
public synchronized intgetHoldability()


        throw new UnsupportedOperationException();
    
public synchronized java.sql.DatabaseMetaDatagetMetaData()

        try {
            return (this.connection.getMetaData());
        } catch (SQLException se) {
            throw se;
        }
    
public com.sun.jdo.spi.persistence.utility.LinkablegetNext()
Get the next ConnectionImpl in a chain.

return
The next ConnectionImpl in a chain.

        return (this.next);
    
synchronized booleangetPooled()
Indicates whether this ConnectionImpl is pooled.

return
TRUE if this ConnectionImpl is pooled.

        return (this.pooled);
    
public com.sun.jdo.spi.persistence.utility.LinkablegetPrevious()
Get the previous ConnectionImpl in a chain.

return
The previous ConnectionImpl in a chain.

        return (this.previous);
    
public synchronized intgetTransactionIsolation()

        try {
            return (this.connection.getTransactionIsolation());
        } catch (SQLException se) {
            throw se;
        }
    
public synchronized java.util.MapgetTypeMap()

        try {
            return (this.connection.getTypeMap());
        } catch (SQLException se) {
            throw se;
        }
    
synchronized java.lang.StringgetURL()
Get the url for this ConnectionImpl object.

return
String containing the url for this ConnectionImpl object.

        return this.url;
    
synchronized java.lang.StringgetUserName()
Get the user name for this ConnectionImpl object.

return
String containing the user name for this ConnectionImpl object.

        return this.userName;
    
public synchronized java.sql.SQLWarninggetWarnings()

        try {
            return (this.connection.getWarnings());
        } catch (SQLException se) {
            throw se;
        }
    
public synchronized voidinternalCommit()
Used by TransactionImpl to commit the transaction on this Connection. Also disassociates this Connection from the transaction. Throws ... (javax.transaction.xa.XAException(XA_RBROLLBACK)) on commit error.

        logger.finest("sqlstore.connectionimpl.internalcommit"); // NOI18N

        try {
            this.connection.commit();
        } catch (Exception e1) {
            try {
                this.connection.rollback();
            } catch (Exception e2) {
                // XXX Try to recover from bad connection. XXX
            } finally {
                this.clearXact();
            }
            throw new JDODataStoreException(null, e1); //XAException(XAException.XA_RBROLLBACK);
        } finally {
            this.clearXact();
        }
    
public synchronized voidinternalRollback()
Used by TransactionImpl to rollback the transaction on this Connection. Also disassociates this Connection from the transaction.

        logger.finest("sqlstore.connectionimpl.internalrollback"); // NOI18N
        try {
            this.connection.rollback();
        } catch (Exception e1) {
            // XXX Try to recover from bad connection. XXX
        } finally {
            this.clearXact();
        }
    
public synchronized booleanisClosed()

        try {
            return (this.connection.isClosed());
        } catch (SQLException se) {
            throw se;
        }
    
public synchronized booleanisReadOnly()

        try {
            return (this.connection.isReadOnly());
        } catch (SQLException se) {
            throw se;
        }
    
public synchronized java.lang.StringnativeSQL(java.lang.String sql)

        try {
            return (this.connection.nativeSQL(sql));
        } catch (SQLException se) {
            throw se;
        }
    
public synchronized java.sql.CallableStatementprepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)


        throw new UnsupportedOperationException();
    
public java.sql.CallableStatementprepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency)

        return (null);
    
public synchronized java.sql.CallableStatementprepareCall(java.lang.String sql)

        CallableStatementImpl fcstmt;

        try {
            this.checkXact();
            fcstmt = new CallableStatementImpl(this,
                    this.connection.prepareCall(sql));
            return ((CallableStatement) fcstmt);
        } catch (SQLException se) {
            throw se;
        }
    
public synchronized java.sql.PreparedStatementprepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)


        throw new UnsupportedOperationException();
    
public synchronized java.sql.PreparedStatementprepareStatement(java.lang.String sql, int autoGeneratedKeys)


        throw new UnsupportedOperationException();
    
public synchronized java.sql.PreparedStatementprepareStatement(java.lang.String sql, int[] columnIndexes)


        throw new UnsupportedOperationException();
    
public synchronized java.sql.PreparedStatementprepareStatement(java.lang.String sql, java.lang.String[] columnNames)

        throw new UnsupportedOperationException();
    
public java.sql.PreparedStatementprepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency)

        return (null);
    
public synchronized java.sql.PreparedStatementprepareStatement(java.lang.String sql)

        PreparedStatementImpl fpstmt;

        try {
            this.checkXact();
            fpstmt = new PreparedStatementImpl(this,
                    this.connection.prepareStatement(sql));
        } catch (SQLException se) {
            throw se;
        }

        return ((PreparedStatement) fpstmt);
    
protected voidrelease()
Called by ConnectionManager to close old connection when a new free connection becomes available

        try {
            this.connection.close();
        } catch (SQLException se) {
            // ignore
        }
        logger.finest("sqlstore.connectionimpl.close.connrelease"); // NOI18N
    
public synchronized voidreleaseSavepoint(java.sql.Savepoint savepoint)


       throw new UnsupportedOperationException();
    
public synchronized voidrollback()

        logger.finest("sqlstore.connectionimpl.rollback"); // NOI18N

        try {
            this.connection.rollback();
            if (this.freePending) {
                if (this.connectionManager.shutDownPending) {
                    this.connection.close();
                    logger.finest("sqlstore.connectionimpl.rollback.close"); // NOI18N
                } else {
                    this.freePending = false;
                    this.connectionManager.freeList.insertAtTail(this);
                }
            }
            if (EJBHelper.isManaged()) {
                // this is for use with test 'ejb' only.
                closeInternal();
            }
        } catch (SQLException se) {
            throw se;
        }
    
public synchronized voidrollback(java.sql.Savepoint savepoint)


       throw new UnsupportedOperationException();

    
public synchronized voidsetAutoCommit(boolean autoCommit)

        try {
            this.connection.setAutoCommit(autoCommit);
        } catch (SQLException se) {
            throw se;
        }
    
public synchronized voidsetCatalog(java.lang.String catalog)

        try {
            this.connection.setCatalog(catalog);
        } catch (SQLException se) {
            throw se;
        }
    
synchronized voidsetFreePending(boolean freePending)
Mark the Connection free-pending. A Connection gets into this state only if it is freed while still participating in a transaction.

        this.freePending = freePending;
    
public synchronized voidsetHoldability(int holdability)


        throw new UnsupportedOperationException();
    
public voidsetNext(com.sun.jdo.spi.persistence.utility.Linkable conn)
Hook a ConnectionImpl to a chain.

param
conn The ConnectionImpl to hook on the chain.

        this.next = conn;
    
synchronized voidsetPooled(boolean flag)
Mark this ConnectionImpl as pooled.

        this.pooled = flag;
    
public voidsetPrevious(com.sun.jdo.spi.persistence.utility.Linkable conn)
Hook a ConnectionImpl to a chain.

param
conn The ConnectionImpl to hook on the chain.

        this.previous = conn;
    
public synchronized voidsetReadOnly(boolean readOnly)

        try {
            this.connection.setReadOnly(readOnly);
        } catch (SQLException se) {
            throw se;
        }
    
public synchronized java.sql.SavepointsetSavepoint()


        throw new UnsupportedOperationException();
    
public synchronized java.sql.SavepointsetSavepoint(java.lang.String name)


        throw new UnsupportedOperationException();
    
public synchronized voidsetTransactionIsolation(int level)

        try {
            this.connection.setTransactionIsolation(level);
        } catch (SQLException se) {
            throw se;
        }
    
public synchronized voidsetTypeMap(java.util.Map map)

        try {
            this.connection.setTypeMap(map);
        } catch (SQLException se) {
            throw se;
        }
    
public synchronized java.lang.StringtoString()
Return a string representation of this ConnectionImpl object.

return
String describing contents of this ConnectionImpl object.

        int xactIsolation = 0;
        String buffer = "Connect@"; // NOI18N

        String strTran = (this.transaction == null) ?
                "  NULL" : this.transaction.toString(); // NOI18N
        int hash = this.hashCode();

        try {
            xactIsolation = this.getTransactionIsolation();
        } catch (SQLException ex) {
            xactIsolation = -1;
        }

        buffer = buffer + hash + "\n" + // NOI18N
                "  pooled = " + this.pooled + "\n" + // NOI18N
                "  freePending = " + this.freePending + "\n" + // NOI18N
                "  xactIsolation = " + xactIsolation + "\n" + // NOI18N
                "  Tran = " + strTran + "\n"; // NOI18N

        return buffer;
    
synchronized booleanxactPending()
Indicates whether this Connection is participating in a transaction.

return
True if this Connection is participating in a transaction; false, otherwise.

        return ((this.transaction != null) ? true : false);