KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Method JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.SQLException JavaDoc;
22
23 import com.mchange.v2.c3p0.C3P0ProxyConnection;
24
25 import org.springframework.util.ReflectionUtils;
26
27 /**
28  * Implementation of the NativeJdbcExtractor interface for the C3P0 connection pool.
29  *
30  * <p>Returns underlying native Connections to application code instead of C3P0's
31  * wrapper implementations; unwraps the Connection for native Statements.
32  * The returned JDBC classes can then safely be cast, e.g. to
33  * <code>oracle.jdbc.OracleConnection</code>.
34  *
35  * <p>This NativeJdbcExtractor can be set just to <i>allow</i> working with
36  * a C3P0 DataSource: If a given object is not a C3P0 wrapper, it will be
37  * returned as-is.
38  *
39  * <p>Note that this class requires C3P0 0.8.5 or later; for earlier C3P0 versions,
40  * use SimpleNativeJdbcExtractor (which won't work for C3P0 0.8.5 or later).
41  *
42  * @author Juergen Hoeller
43  * @since 1.1.5
44  * @see com.mchange.v2.c3p0.C3P0ProxyConnection#rawConnectionOperation
45  * @see SimpleNativeJdbcExtractor
46  */

47 public class C3P0NativeJdbcExtractor extends NativeJdbcExtractorAdapter {
48
49     private final Method JavaDoc getRawConnectionMethod;
50
51
52     /**
53      * This method is not meant to be used directly; it rather serves
54      * as callback method for C3P0's "rawConnectionOperation" API.
55      * @param con a native Connection handle
56      * @return the native Connection handle, as-is
57      */

58     public static Connection JavaDoc getRawConnection(Connection JavaDoc con) {
59         return con;
60     }
61
62
63     public C3P0NativeJdbcExtractor() {
64         try {
65             this.getRawConnectionMethod = getClass().getMethod("getRawConnection", new Class JavaDoc[] {Connection JavaDoc.class});
66         }
67         catch (NoSuchMethodException JavaDoc ex) {
68             throw new IllegalStateException JavaDoc("Internal error in C3P0NativeJdbcExtractor: " + ex.getMessage());
69         }
70     }
71
72
73     public boolean isNativeConnectionNecessaryForNativeStatements() {
74         return true;
75     }
76
77     public boolean isNativeConnectionNecessaryForNativePreparedStatements() {
78         return true;
79     }
80
81     public boolean isNativeConnectionNecessaryForNativeCallableStatements() {
82         return true;
83     }
84
85     /**
86      * Retrieve the Connection via C3P0's <code>rawConnectionOperation</code> API,
87      * using the <code>getRawConnection</code> as callback to get access to the
88      * raw Connection (which is otherwise not directly supported by C3P0).
89      * @see #getRawConnection
90      */

91     protected Connection JavaDoc doGetNativeConnection(Connection JavaDoc con) throws SQLException JavaDoc {
92         if (con instanceof C3P0ProxyConnection) {
93             C3P0ProxyConnection cpCon = (C3P0ProxyConnection) con;
94             try {
95                 return (Connection JavaDoc) cpCon.rawConnectionOperation(
96                         this.getRawConnectionMethod, null, new Object JavaDoc[] {C3P0ProxyConnection.RAW_CONNECTION});
97             }
98             catch (SQLException JavaDoc ex) {
99                 throw ex;
100             }
101             catch (Exception JavaDoc ex) {
102                 ReflectionUtils.handleReflectionException(ex);
103             }
104         }
105         return con;
106     }
107
108 }
109
Popular Tags