01: package org.apache.ojb.broker.accesslayer;
02:
03: /* Copyright 2002-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * 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;
23: import java.sql.SQLException;
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
32: ConnectionFactoryAbstractImpl {
33: private Logger log = LoggerFactory
34: .getLogger(ConnectionFactoryNotPooledImpl.class);
35:
36: public Connection checkOutJdbcConnection(
37: JdbcConnectionDescriptor jcd) throws LookupException {
38: if (log.isDebugEnabled()) {
39: log
40: .debug("checkOutJdbcConnection: this implementation always return a new Connection");
41: }
42: final Connection conn = newConnectionFromDriverManager(jcd);
43: validateConnection(conn, jcd);
44: // Connection is now guaranteed to be valid (else validateConnection must throw exception)
45: return conn;
46: }
47:
48: protected void validateConnection(Connection conn,
49: JdbcConnectionDescriptor jcd) throws LookupException {
50: if (conn == null) {
51: log.error(getJcdDescription(jcd)
52: + " failed, DriverManager returned null");
53: throw new LookupException(
54: "No Connection returned from DriverManager");
55: }
56: try {
57: if (conn.isClosed()) {
58: log.error(getJcdDescription(jcd)
59: + " is invalid (closed)");
60: throw new LookupException(
61: "Could not create valid connection, connection was "
62: + conn);
63: }
64: } catch (SQLException e) {
65: log.error(getJcdDescription(jcd)
66: + " failed validation with exception");
67: throw new LookupException("Connection validation failed", e);
68: }
69: }
70:
71: public void releaseJdbcConnection(JdbcConnectionDescriptor jcd,
72: Connection con) throws LookupException {
73: try {
74: con.close();
75: } catch (SQLException e) {
76: log.warn("Connection.close() failed, message was "
77: + e.getMessage());
78: }
79: }
80:
81: }
|