KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > support > nativejdbc > NativeJdbcExtractorAdapter


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

16
17 package org.springframework.jdbc.support.nativejdbc;
18
19 import java.sql.CallableStatement JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.DatabaseMetaData JavaDoc;
22 import java.sql.PreparedStatement JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.Statement JavaDoc;
26
27 import org.springframework.jdbc.datasource.DataSourceUtils;
28
29 /**
30  * Abstract adapter class for the NativeJdbcExtractor interface,
31  * for simplified implementation of basic extractors.
32  * Basically returns the passed-in JDBC objects on all methods.
33  *
34  * <p><code>getNativeConnection</code> checks for a ConnectionProxy chain,
35  * for example from a TransactionAwareDataSourceProxy, before delegating to
36  * <code>doGetNativeConnection</code> for actual unwrapping. You can override
37  * either of the two for a specific connection pool, but the latter is
38  * recommended to participate in ConnectionProxy unwrapping.
39  *
40  * <p><code>getNativeConnection</code> also applies a fallback if the first
41  * native extraction process failed, that is, returned the same Connection as
42  * passed in. It assumes that some additional proxying is going in this case:
43  * Hence, it retrieves the underlying native Connection from the DatabaseMetaData
44  * via <code>conHandle.getMetaData().getConnection()</code> and retries the native
45  * extraction process based on that Connection handle. This works, for example,
46  * for the Connection proxies exposed by Hibernate 3.1's <code>Session.connection()</code>.
47  *
48  * <p>The <code>getNativeConnectionFromStatement</code> method is implemented
49  * to simply delegate to <code>getNativeConnection</code> with the Statement's
50  * Connection. This is what most extractor implementations will stick to,
51  * unless there's a more efficient version for a specific pool.
52  *
53  * @author Juergen Hoeller
54  * @since 1.1
55  * @see #getNativeConnection
56  * @see #getNativeConnectionFromStatement
57  * @see org.springframework.jdbc.datasource.ConnectionProxy
58  */

59 public abstract class NativeJdbcExtractorAdapter implements NativeJdbcExtractor {
60
61     /**
62      * Return <code>false</code> by default.
63      */

64     public boolean isNativeConnectionNecessaryForNativeStatements() {
65         return false;
66     }
67
68     /**
69      * Return <code>false</code> by default.
70      */

71     public boolean isNativeConnectionNecessaryForNativePreparedStatements() {
72         return false;
73     }
74
75     /**
76      * Return <code>false</code> by default.
77      */

78     public boolean isNativeConnectionNecessaryForNativeCallableStatements() {
79         return false;
80     }
81
82     /**
83      * Check for a ConnectionProxy chain, then delegate to doGetNativeConnection.
84      * <p>ConnectionProxy is used by Spring's TransactionAwareDataSourceProxy
85      * and LazyConnectionDataSourceProxy. The target connection behind it is
86      * typically one from a local connection pool, to be unwrapped by the
87      * doGetNativeConnection implementation of a concrete subclass.
88      * @see #doGetNativeConnection
89      * @see org.springframework.jdbc.datasource.ConnectionProxy
90      * @see org.springframework.jdbc.datasource.DataSourceUtils#getTargetConnection
91      * @see org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy
92      * @see org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
93      */

94     public Connection JavaDoc getNativeConnection(Connection JavaDoc con) throws SQLException JavaDoc {
95         if (con == null) {
96             return null;
97         }
98         Connection JavaDoc targetCon = DataSourceUtils.getTargetConnection(con);
99         Connection JavaDoc nativeCon = doGetNativeConnection(targetCon);
100         if (nativeCon == targetCon) {
101             // We haven't received a different Connection, so we'll assume that there's
102
// some additional proxying going on. Let's check whether we get something
103
// different back from the DatabaseMetaData.getConnection() call.
104
DatabaseMetaData JavaDoc metaData = targetCon.getMetaData();
105             // The following check is only really there for mock Connections
106
// which might not carry a DatabaseMetaData instance.
107
if (metaData != null) {
108                 Connection JavaDoc metaCon = metaData.getConnection();
109                 if (metaCon != targetCon) {
110                     // We've received a different Connection there:
111
// Let's retry the native extraction process with it.
112
nativeCon = doGetNativeConnection(metaCon);
113                 }
114             }
115         }
116         return nativeCon;
117     }
118
119     /**
120      * Not able to unwrap: return passed-in Connection.
121      */

122     protected Connection JavaDoc doGetNativeConnection(Connection JavaDoc con) throws SQLException JavaDoc {
123         return con;
124     }
125
126     /**
127      * Retrieve the Connection via the Statement's Connection.
128      * @see #getNativeConnection
129      * @see Statement#getConnection
130      */

131     public Connection JavaDoc getNativeConnectionFromStatement(Statement JavaDoc stmt) throws SQLException JavaDoc {
132         if (stmt == null) {
133             return null;
134         }
135         return getNativeConnection(stmt.getConnection());
136     }
137
138     /**
139      * Not able to unwrap: return passed-in Statement.
140      */

141     public Statement JavaDoc getNativeStatement(Statement JavaDoc stmt) throws SQLException JavaDoc {
142         return stmt;
143     }
144
145     /**
146      * Not able to unwrap: return passed-in PreparedStatement.
147      */

148     public PreparedStatement JavaDoc getNativePreparedStatement(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
149         return ps;
150     }
151
152     /**
153      * Not able to unwrap: return passed-in CallableStatement.
154      */

155     public CallableStatement JavaDoc getNativeCallableStatement(CallableStatement JavaDoc cs) throws SQLException JavaDoc {
156         return cs;
157     }
158
159     /**
160      * Not able to unwrap: return passed-in ResultSet.
161      */

162     public ResultSet JavaDoc getNativeResultSet(ResultSet JavaDoc rs) throws SQLException JavaDoc {
163         return rs;
164     }
165
166 }
167
Popular Tags