KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > jdbc > EmbedConnectionContext


1 /*
2
3    Derby - Class org.apache.derby.impl.jdbc.EmbedConnectionContext
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 //depot/main/java/org.apache.derby.impl.jdbc/EmbedConnectionContext.java#24 - edit change 16899 (text)
23
package org.apache.derby.impl.jdbc;
24
25 // This is the recommended super-class for all contexts.
26
import org.apache.derby.iapi.services.context.ContextImpl;
27 import org.apache.derby.iapi.services.context.ContextManager;
28 import org.apache.derby.iapi.sql.conn.StatementContext;
29 import org.apache.derby.iapi.jdbc.ConnectionContext;
30 import org.apache.derby.iapi.error.StandardException;
31 import org.apache.derby.iapi.sql.ResultSet;
32
33 import org.apache.derby.iapi.error.ExceptionSeverity;
34 import java.sql.SQLException JavaDoc;
35 import java.util.Vector JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 /**
38     @author djd
39  */

40 class EmbedConnectionContext extends ContextImpl
41         implements ConnectionContext
42 {
43
44     /**
45         We hold a soft reference to the connection so that when the application
46         releases its reference to the Connection without closing it, its finalize
47         method will be called, which will then close the connection. If a direct
48         reference is used here, such a Connection will never be closed or garbage
49         collected as modules hold onto the ContextManager and thus there would
50         be a direct reference through this object.
51     */

52     private java.lang.ref.SoftReference JavaDoc connRef;
53
54
55     EmbedConnectionContext(ContextManager cm, EmbedConnection conn) {
56         super(cm, ConnectionContext.CONTEXT_ID);
57
58         connRef = new java.lang.ref.SoftReference JavaDoc(conn);
59     }
60
61     public void cleanupOnError(Throwable JavaDoc error) {
62
63         if (connRef == null)
64             return;
65
66         EmbedConnection conn = (EmbedConnection) connRef.get();
67
68         if (error instanceof StandardException) {
69
70             StandardException se = (StandardException) error;
71             if (se.getSeverity() < ExceptionSeverity.SESSION_SEVERITY) {
72
73                 // any error in auto commit mode that does not close the
74
// session will cause a rollback, thus remvoing the need
75
// for any commit. We could check this flag under autoCommit
76
// being true but the flag is ignored when autoCommit is false
77
// so why add the extra check
78
if (conn != null) {
79                     conn.needCommit = false;
80                 }
81                 return;
82             }
83         }
84
85         // This may be a transaction without connection.
86
if (conn != null)
87             conn.setInactive(); // make the connection inactive & empty
88

89         connRef = null;
90         popMe();
91     }
92
93     //public java.sql.Connection getEmbedConnection()
94
//{
95
/// return conn;
96
//}
97

98     /**
99         Get a connection equivalent to the call
100         <PRE>
101         DriverManager.getConnection("jdbc:default:connection");
102         </PRE>
103     */

104     public java.sql.Connection JavaDoc getNestedConnection(boolean internal) throws SQLException JavaDoc {
105
106         EmbedConnection conn = (EmbedConnection) connRef.get();
107
108         if ((conn == null) || conn.isClosed())
109             throw Util.noCurrentConnection();
110
111         if (!internal) {
112             StatementContext sc = conn.getLanguageConnection().getStatementContext();
113             if ((sc == null) || (sc.getSQLAllowed() < org.apache.derby.catalog.types.RoutineAliasInfo.MODIFIES_SQL_DATA))
114                 throw Util.noCurrentConnection();
115         }
116
117         return conn.getLocalDriver().getNewNestedConnection(conn);
118     }
119
120     /**
121      * Get a jdbc ResultSet based on the execution ResultSet.
122      *
123      * @param executionResultSet a result set as gotten from execution
124      *
125      */

126     public java.sql.ResultSet JavaDoc getResultSet
127     (
128         ResultSet executionResultSet
129     ) throws SQLException JavaDoc
130     {
131         EmbedConnection conn = (EmbedConnection) connRef.get();
132
133         EmbedResultSet rs = conn.getLocalDriver().newEmbedResultSet(conn, executionResultSet,
134                             false, (EmbedStatement) null, true);
135         return rs;
136     }
137 }
138
Popular Tags