KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > c3p0 > dbms > OracleUtils


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.c3p0.dbms;
25
26 import java.lang.reflect.*;
27 import java.sql.*;
28 import com.mchange.v2.c3p0.*;
29 import com.mchange.v2.sql.SqlUtils;
30 import oracle.sql.BLOB;
31 import oracle.sql.CLOB;
32 import oracle.jdbc.driver.OracleConnection;
33
34 /**
35  * A convenience class for OracleUsers who wish to use Oracle-specific Connection API
36  * without working directly with c3p0 raw connection operations.
37  */

38 public final class OracleUtils
39 {
40     final static Class JavaDoc[] CREATE_TEMP_ARGS = new Class JavaDoc[]{Connection.class, boolean.class, int.class};
41     
42     /**
43      * Uses Oracle-specific API on the raw, underlying Connection to create a temporary BLOB.
44      * <b>Users are responsible for calling freeTemporary on the returned BLOB prior to Connection close() / check-in!
45      * c3p0 will <i>not</i> automatically clean up temporary BLOBs.</b>
46      *
47      * @param c3p0ProxyCon may be a c3p0 proxy for an <tt>oracle.jdbc.driver.OracleConnection</tt>, or an
48      * <tt>oracle.jdbc.driver.OracleConnection</tt> directly.
49      */

50     public static BLOB createTemporaryBLOB(Connection c3p0ProxyCon, boolean cache, int duration) throws SQLException
51     {
52     if (c3p0ProxyCon instanceof C3P0ProxyConnection)
53         {
54         try
55             {
56             C3P0ProxyConnection castCon = (C3P0ProxyConnection) c3p0ProxyCon;
57             Method m = BLOB.class.getMethod("createTemporary", CREATE_TEMP_ARGS);
58             Object JavaDoc[] args = new Object JavaDoc[] {C3P0ProxyConnection.RAW_CONNECTION, Boolean.valueOf( cache ), new Integer JavaDoc( duration )};
59             return (BLOB) castCon.rawConnectionOperation(m, null, args);
60             }
61         catch (InvocationTargetException e)
62             {
63             if (Debug.DEBUG)
64                 e.printStackTrace();
65             throw SqlUtils.toSQLException( e.getTargetException() );
66             }
67         catch (Exception JavaDoc e)
68             {
69             if (Debug.DEBUG)
70                 e.printStackTrace();
71             throw SqlUtils.toSQLException( e );
72             }
73         }
74     else if (c3p0ProxyCon instanceof OracleConnection)
75         return BLOB.createTemporary( c3p0ProxyCon, cache, duration );
76     else
77         throw new SQLException("Cannot create an oracle BLOB from a Connection that is neither an oracle.jdbc.driver.Connection, " +
78                    "nor a C3P0ProxyConnection wrapped around an oracle.jdbc.driver.Connection.");
79     }
80     
81     /**
82      * Uses Oracle-specific API on the raw, underlying Connection to create a temporary CLOB.
83      * <b>Users are responsible for calling freeTemporary on the returned BLOB prior to Connection close() / check-in!
84      * c3p0 will <i>not</i> automatically clean up temporary CLOBs.</b>
85      *
86      * @param c3p0ProxyCon may be a c3p0 proxy for an <tt>oracle.jdbc.driver.OracleConnection</tt>, or an
87      * <tt>oracle.jdbc.driver.OracleConnection</tt> directly.
88      */

89     public static CLOB createTemporaryCLOB(Connection c3p0ProxyCon, boolean cache, int duration) throws SQLException
90     {
91     if (c3p0ProxyCon instanceof C3P0ProxyConnection)
92         {
93         try
94             {
95             C3P0ProxyConnection castCon = (C3P0ProxyConnection) c3p0ProxyCon;
96             Method m = CLOB.class.getMethod("createTemporary", CREATE_TEMP_ARGS);
97             Object JavaDoc[] args = new Object JavaDoc[] {C3P0ProxyConnection.RAW_CONNECTION, Boolean.valueOf( cache ), new Integer JavaDoc( duration )};
98             return (CLOB) castCon.rawConnectionOperation(m, null, args);
99             }
100         catch (InvocationTargetException e)
101             {
102             if (Debug.DEBUG)
103                 e.printStackTrace();
104             throw SqlUtils.toSQLException( e.getTargetException() );
105             }
106         catch (Exception JavaDoc e)
107             {
108             if (Debug.DEBUG)
109                 e.printStackTrace();
110             throw SqlUtils.toSQLException( e );
111             }
112         }
113     else if (c3p0ProxyCon instanceof OracleConnection)
114         return CLOB.createTemporary( c3p0ProxyCon, cache, duration );
115     else
116         throw new SQLException("Cannot create an oracle CLOB from a Connection that is neither an oracle.jdbc.driver.Connection, " +
117                    "nor a C3P0ProxyConnection wrapped around an oracle.jdbc.driver.Connection.");
118     }
119     
120     private OracleUtils()
121     {}
122 }
123
Popular Tags