KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > accesslayer > ConnectionFactoryNotPooledImpl


1 package org.apache.ojb.broker.accesslayer;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
19 import org.apache.ojb.broker.util.logging.Logger;
20 import org.apache.ojb.broker.util.logging.LoggerFactory;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.SQLException JavaDoc;
24
25 /**
26  * Base implementation without connection pooling.
27  *
28  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
29  * @version $Id: ConnectionFactoryNotPooledImpl.java,v 1.5.2.1 2005/04/07 13:08:57 mkalen Exp $
30  */

31 public class ConnectionFactoryNotPooledImpl extends ConnectionFactoryAbstractImpl
32 {
33     private Logger log = LoggerFactory.getLogger(ConnectionFactoryNotPooledImpl.class);
34
35     public Connection JavaDoc checkOutJdbcConnection(JdbcConnectionDescriptor jcd) throws LookupException
36     {
37         if (log.isDebugEnabled())
38         {
39             log.debug("checkOutJdbcConnection: this implementation always return a new Connection");
40         }
41         final Connection JavaDoc conn = newConnectionFromDriverManager(jcd);
42         validateConnection(conn, jcd);
43         // Connection is now guaranteed to be valid (else validateConnection must throw exception)
44
return conn;
45     }
46
47     protected void validateConnection(Connection JavaDoc conn, JdbcConnectionDescriptor jcd)
48             throws LookupException
49     {
50         if (conn == null)
51         {
52             log.error(getJcdDescription(jcd) + " failed, DriverManager returned null");
53             throw new LookupException("No Connection returned from DriverManager");
54         }
55         try
56         {
57             if (conn.isClosed())
58             {
59                 log.error(getJcdDescription(jcd) + " is invalid (closed)");
60                 throw new LookupException("Could not create valid connection, connection was " +
61                                           conn);
62             }
63         }
64         catch (SQLException JavaDoc e)
65         {
66             log.error(getJcdDescription(jcd) + " failed validation with exception");
67             throw new LookupException("Connection validation failed", e);
68         }
69     }
70
71     public void releaseJdbcConnection(JdbcConnectionDescriptor jcd, Connection JavaDoc con)
72             throws LookupException
73     {
74         try
75         {
76             con.close();
77         }
78         catch (SQLException JavaDoc e)
79         {
80             log.warn("Connection.close() failed, message was " + e.getMessage());
81         }
82     }
83
84 }
85
Popular Tags