KickJava   Java API By Example, From Geeks To Geeks.

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


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.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 /**
26  * Simple implementation of the NativeJdbcExtractor interface.
27  * Assumes a pool that wraps Connection handles but not DatabaseMetaData:
28  * In this case, the underlying native Connection can be retrieved by simply
29  * calling <code>conHandle.getMetaData().getConnection()</code>.
30  * All other JDBC objects will be returned as passed in.
31  *
32  * <p>This extractor should work with any pool that does not wrap DatabaseMetaData,
33  * and will also work with any plain JDBC driver. Note that a pool can still wrap
34  * Statements, PreparedStatements, etc: The only requirement of this extractor is
35  * that <code>java.sql.DatabaseMetaData</code> does not get wrapped, returning the
36  * native Connection of the JDBC driver on <code>metaData.getConnection()</code>.
37  *
38  * <p>Customize this extractor by setting the "nativeConnectionNecessaryForXxx"
39  * flags accordingly: If Statements, PreparedStatements, and/or CallableStatements
40  * are wrapped by your pool, set the corresponding "nativeConnectionNecessaryForXxx"
41  * flags to "true". If none of the statement types is wrapped - or you solely need
42  * Connection unwrapping in the first place -, the defaults are fine.
43  *
44  * <p>SimpleNativeJdbcExtractor is a common choice for use with OracleLobHandler,
45  * which just needs Connection unwrapping via the
46  * <code>getNativeConnectionFromStatement</code> method. This usage will work
47  * with almost any connection pool. Known to work are, for example:
48  * <ul>
49  * <li>Caucho Resin 2.1.x, 3.0.x
50  * <li>Sun Java System Application Server 8
51  * <li>Oracle OC4J 9.0.3, 9.0.4
52  * <li>C3P0 0.8.x
53  * <li>Jakarta Commons DBCP 1.0, 1.1, 1.2 (used in Tomcat 4.1.x, 5.0.x)
54  * <li>JBoss 3.2.x
55  * </ul>
56  *
57  * <p>For full usage with JdbcTemplate, i.e. to also provide Statement unwrapping:
58  * <ul>
59  * <li>Use a default SimpleNativeJdbcExtractor for Resin and SJSAS (no JDBC
60  * Statement objects are wrapped, therefore no special unwrapping is necessary).
61  * <li>Use a SimpleNativeJdbcExtractor with all "nativeConnectionNecessaryForXxx"
62  * flags set to "true" for OC4J and C3P0 (all JDBC Statement objects are wrapped,
63  * but none of the wrappers allow for unwrapping).
64  * <li>Use a CommonsDbcpNativeJdbcExtractor for Jakarta Commons DBCP respectively
65  * a JBossNativeJdbcExtractor for JBoss (all JDBC Statement objects are wrapped,
66  * but all of them can be extracted by casting to implementation classes).
67  * </ul>
68  *
69  * @author Juergen Hoeller
70  * @since 05.12.2003
71  * @see java.sql.DatabaseMetaData#getConnection
72  * @see #getNativeConnection
73  * @see #setNativeConnectionNecessaryForNativeStatements
74  * @see #setNativeConnectionNecessaryForNativePreparedStatements
75  * @see #setNativeConnectionNecessaryForNativeCallableStatements
76  * @see CommonsDbcpNativeJdbcExtractor
77  * @see JBossNativeJdbcExtractor
78  * @see org.springframework.jdbc.core.JdbcTemplate#setNativeJdbcExtractor
79  * @see org.springframework.jdbc.support.lob.OracleLobHandler#setNativeJdbcExtractor
80  */

81 public class SimpleNativeJdbcExtractor extends NativeJdbcExtractorAdapter {
82
83     private boolean nativeConnectionNecessaryForNativeStatements = false;
84
85     private boolean nativeConnectionNecessaryForNativePreparedStatements = false;
86
87     private boolean nativeConnectionNecessaryForNativeCallableStatements = false;
88
89
90     /**
91      * Set whether it is necessary to work on the native Connection to
92      * receive native Statements. Default is "false". If true, the Connection
93      * will be unwrapped first to create a Statement.
94      * <p>This makes sense if you need to work with native Statements from
95      * a pool that does not allow to extract the native JDBC objects from its
96      * wrappers but returns the native Connection on DatabaseMetaData.getConnection.
97      * <p>The standard SimpleNativeJdbcExtractor is unable to unwrap statements,
98      * so set this to true if your connection pool wraps Statements.
99      * @see java.sql.Connection#createStatement
100      * @see java.sql.DatabaseMetaData#getConnection
101      */

102     public void setNativeConnectionNecessaryForNativeStatements(boolean nativeConnectionNecessaryForNativeStatements) {
103         this.nativeConnectionNecessaryForNativeStatements = nativeConnectionNecessaryForNativeStatements;
104     }
105
106     public boolean isNativeConnectionNecessaryForNativeStatements() {
107         return nativeConnectionNecessaryForNativeStatements;
108     }
109
110     /**
111      * Set whether it is necessary to work on the native Connection to
112      * receive native PreparedStatements. Default is "false". If true,
113      * the Connection will be unwrapped first to create a PreparedStatement.
114      * <p>This makes sense if you need to work with native PreparedStatements from
115      * a pool that does not allow to extract the native JDBC objects from its
116      * wrappers but returns the native Connection on Statement.getConnection.
117      * <p>The standard SimpleNativeJdbcExtractor is unable to unwrap statements,
118      * so set this to true if your connection pool wraps PreparedStatements.
119      * @see java.sql.Connection#prepareStatement
120      * @see java.sql.DatabaseMetaData#getConnection
121      */

122     public void setNativeConnectionNecessaryForNativePreparedStatements(boolean nativeConnectionNecessary) {
123         this.nativeConnectionNecessaryForNativePreparedStatements = nativeConnectionNecessary;
124     }
125
126     public boolean isNativeConnectionNecessaryForNativePreparedStatements() {
127         return nativeConnectionNecessaryForNativePreparedStatements;
128     }
129
130     /**
131      * Set whether it is necessary to work on the native Connection to
132      * receive native CallableStatements. Default is "false". If true,
133      * the Connection will be unwrapped first to create a CallableStatement.
134      * <p>This makes sense if you need to work with native CallableStatements from
135      * a pool that does not allow to extract the native JDBC objects from its
136      * wrappers but returns the native Connection on Statement.getConnection.
137      * <p>The standard SimpleNativeJdbcExtractor is unable to unwrap statements,
138      * so set this to true if your connection pool wraps CallableStatements.
139      * @see java.sql.Connection#prepareCall
140      * @see java.sql.DatabaseMetaData#getConnection
141      */

142     public void setNativeConnectionNecessaryForNativeCallableStatements(boolean nativeConnectionNecessary) {
143         this.nativeConnectionNecessaryForNativeCallableStatements = nativeConnectionNecessary;
144     }
145
146     public boolean isNativeConnectionNecessaryForNativeCallableStatements() {
147         return nativeConnectionNecessaryForNativeCallableStatements;
148     }
149
150 }
151
Popular Tags