001    package org.apache.ojb.broker.core;
002    
003    /* Copyright 2003-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.ojb.broker.PersistenceBrokerException;
019    import org.apache.ojb.broker.PersistenceBrokerInternal;
020    
021    public class PersistenceBrokerHandle extends DelegatingPersistenceBroker
022    {
023        /**
024         * Constructor for the handle, set itself in
025         * {@link PersistenceBrokerThreadMapping#setCurrentPersistenceBroker}
026         */
027        public PersistenceBrokerHandle(final PersistenceBrokerInternal broker)
028        {
029            super(broker);
030            PersistenceBrokerThreadMapping.setCurrentPersistenceBroker(broker.getPBKey(), this);
031        }
032    
033        public boolean isClosed()
034        {
035            return super.isClosed();
036        }
037    
038        public boolean isInTransaction() throws PersistenceBrokerException
039        {
040            return !isClosed() && super.isInTransaction();
041        }
042    
043        /**
044         * Destroy this handle and return the underlying (wrapped) PB instance
045         * to pool (when using default implementation of PersistenceBrokerFactory),
046         * unset this instance from {@link PersistenceBrokerThreadMapping}.
047         */
048        public boolean close()
049        {
050            if (getDelegate() == null) return true;
051            try
052            {
053                PersistenceBrokerThreadMapping.unsetCurrentPersistenceBroker(getPBKey(), this);
054                super.close();
055            }
056            finally
057            {
058                /*
059                here we destroy the handle. set the
060                underlying PB instance 'null', thus it's not
061                possible to corrupt a closed PB instance.
062                */
063                setDelegate(null);
064            }
065            return true;
066        }
067    }