KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > store > impl > rdbms > JDBCAwareInputStream


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/impl/rdbms/JDBCAwareInputStream.java,v 1.6.2.1 2004/10/30 21:47:25 unico Exp $
3  * $Revision: 1.6.2.1 $
4  * $Date: 2004/10/30 21:47:25 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.store.impl.rdbms;
25
26 import java.io.FilterInputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.sql.Statement JavaDoc;
33
34 /**
35  * Wrapper for an input stream that has come from a JDBC connection. This
36  * wrapper also closes the underlying JDBC result set - freeing precious system
37  * resources - or at least Oracle cursors ;-)
38  *
39  * @version $Revision: 1.6.2.1 $
40  */

41 class JDBCAwareInputStream
42     extends FilterInputStream JavaDoc {
43     
44     
45     // ---------------------------------------------------------- Instance Data
46

47     
48     /**
49      * The JDBC statement that will be closed along with its result set when the
50      * input stream is told to close itself.
51      */

52     private Statement JavaDoc stmt = null;
53     private ResultSet JavaDoc rs = null;
54     private Connection JavaDoc connection = null;
55     
56     
57     // ----------------------------------------------------------- Constructors
58

59     
60     /**
61      * Creates an input stream that closes a statmenet, a resultset and a connection
62      * when the stream itself is closed.
63      *
64      */

65     public JDBCAwareInputStream(InputStream JavaDoc in, Statement JavaDoc stmt, ResultSet JavaDoc rs, Connection JavaDoc connection) {
66         super(in);
67         
68         this.stmt = stmt;
69         this.rs = rs;
70         this.connection = connection;
71     }
72     
73     
74     // --------------------------------------------- InputStream Implementation
75

76     
77     /**
78      * Overridden to close the associated JDBC statement, result set and connection together with the
79      * input stream.
80      */

81     public void close() throws IOException JavaDoc {
82         try {
83             if (rs != null) {
84                 rs.close();
85             }
86         } catch (SQLException JavaDoc e) {
87             throw new IOException JavaDoc(e.getMessage());
88         } finally {
89             try {
90                 if (stmt != null) {
91                     stmt.close();
92                 }
93             } catch (SQLException JavaDoc e) {
94                 throw new IOException JavaDoc(e.getMessage());
95             } finally {
96                 try {
97                     if (connection != null) {
98                         try {
99                             connection.commit();
100                         } catch (SQLException JavaDoc e) {
101                             throw new IOException JavaDoc(e.getMessage());
102                         } finally {
103                             try {
104                                 connection.close();
105                             } catch (SQLException JavaDoc e) {
106                                 throw new IOException JavaDoc(e.getMessage());
107                             }
108                         }
109                     }
110                 } finally {
111                     super.close();
112                 }
113             }
114         }
115     }
116 }
117
118
Popular Tags