001    package org.apache.ojb.broker.accesslayer;
002    
003    /* Copyright 2002-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.metadata.JdbcConnectionDescriptor;
019    import org.apache.ojb.broker.util.logging.Logger;
020    import org.apache.ojb.broker.util.logging.LoggerFactory;
021    
022    import java.sql.Connection;
023    import java.sql.SQLException;
024    
025    /**
026     * Base implementation without connection pooling.
027     *
028     * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
029     * @version $Id: ConnectionFactoryNotPooledImpl.java,v 1.1 2007-08-24 22:17:30 ewestfal Exp $
030     */
031    public class ConnectionFactoryNotPooledImpl extends ConnectionFactoryAbstractImpl
032    {
033        private Logger log = LoggerFactory.getLogger(ConnectionFactoryNotPooledImpl.class);
034    
035        public Connection checkOutJdbcConnection(JdbcConnectionDescriptor jcd) throws LookupException
036        {
037            if (log.isDebugEnabled())
038            {
039                log.debug("checkOutJdbcConnection: this implementation always return a new Connection");
040            }
041            final Connection conn = newConnectionFromDriverManager(jcd);
042            validateConnection(conn, jcd);
043            // Connection is now guaranteed to be valid (else validateConnection must throw exception)
044            return conn;
045        }
046    
047        protected void validateConnection(Connection conn, JdbcConnectionDescriptor jcd)
048                throws LookupException
049        {
050            if (conn == null)
051            {
052                log.error(getJcdDescription(jcd) + " failed, DriverManager returned null");
053                throw new LookupException("No Connection returned from DriverManager");
054            }
055            try
056            {
057                if (conn.isClosed())
058                {
059                    log.error(getJcdDescription(jcd) + " is invalid (closed)");
060                    throw new LookupException("Could not create valid connection, connection was " +
061                                              conn);
062                }
063            }
064            catch (SQLException e)
065            {
066                log.error(getJcdDescription(jcd) + " failed validation with exception");
067                throw new LookupException("Connection validation failed", e);
068            }
069        }
070    
071        public void releaseJdbcConnection(JdbcConnectionDescriptor jcd, Connection con)
072                throws LookupException
073        {
074            try
075            {
076                con.close();
077            }
078            catch (SQLException e)
079            {
080                log.warn("Connection.close() failed, message was " + e.getMessage());
081            }
082        }
083    
084    }