1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.jdbc.EmbedConnectionContext |
4 | |
5 | Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | //depot/main/java/org.apache.derby.impl.jdbc/EmbedConnectionContext.java#24 - edit change 16899 (text) |
22 | package org.apache.derby.impl.jdbc; |
23 | |
24 | // This is the recommended super-class for all contexts. |
25 | import org.apache.derby.iapi.services.context.ContextImpl; |
26 | import org.apache.derby.iapi.services.context.ContextManager; |
27 | import org.apache.derby.iapi.sql.conn.StatementContext; |
28 | import org.apache.derby.iapi.jdbc.ConnectionContext; |
29 | import org.apache.derby.iapi.error.StandardException; |
30 | import org.apache.derby.iapi.sql.ResultSet; |
31 | |
32 | import org.apache.derby.iapi.error.ExceptionSeverity; |
33 | import java.sql.SQLException; |
34 | import java.util.Vector; |
35 | import java.util.Enumeration; |
36 | /** |
37 | @author djd |
38 | */ |
39 | class EmbedConnectionContext extends ContextImpl |
40 | implements ConnectionContext |
41 | { |
42 | |
43 | /** |
44 | We hold a soft reference to the connection so that when the application |
45 | releases its reference to the Connection without closing it, its finalize |
46 | method will be called, which will then close the connection. If a direct |
47 | reference is used here, such a Connection will never be closed or garbage |
48 | collected as modules hold onto the ContextManager and thus there would |
49 | be a direct reference through this object. |
50 | */ |
51 | private java.lang.ref.SoftReference connRef; |
52 | |
53 | |
54 | EmbedConnectionContext(ContextManager cm, EmbedConnection conn) { |
55 | super(cm, ConnectionContext.CONTEXT_ID); |
56 | |
57 | connRef = new java.lang.ref.SoftReference(conn); |
58 | } |
59 | |
60 | public void cleanupOnError(Throwable error) { |
61 | |
62 | if (connRef == null) |
63 | return; |
64 | |
65 | EmbedConnection conn = (EmbedConnection) connRef.get(); |
66 | |
67 | if (error instanceof StandardException) { |
68 | |
69 | StandardException se = (StandardException) error; |
70 | if (se.getSeverity() < ExceptionSeverity.SESSION_SEVERITY) { |
71 | |
72 | // any error in auto commit mode that does not close the |
73 | // session will cause a rollback, thus remvoing the need |
74 | // for any commit. We could check this flag under autoCommit |
75 | // being true but the flag is ignored when autoCommit is false |
76 | // so why add the extra check |
77 | if (conn != null) { |
78 | conn.needCommit = false; |
79 | } |
80 | return; |
81 | } |
82 | } |
83 | |
84 | // This may be a transaction without connection. |
85 | if (conn != null) |
86 | conn.setInactive(); // make the connection inactive & empty |
87 | |
88 | connRef = null; |
89 | popMe(); |
90 | } |
91 | |
92 | //public java.sql.Connection getEmbedConnection() |
93 | //{ |
94 | /// return conn; |
95 | //} |
96 | |
97 | /** |
98 | Get a connection equivalent to the call |
99 | <PRE> |
100 | DriverManager.getConnection("jdbc:default:connection"); |
101 | </PRE> |
102 | */ |
103 | public java.sql.Connection getNestedConnection(boolean internal) throws SQLException { |
104 | |
105 | EmbedConnection conn = (EmbedConnection) connRef.get(); |
106 | |
107 | if ((conn == null) || conn.isClosed()) |
108 | throw Util.noCurrentConnection(); |
109 | |
110 | if (!internal) { |
111 | StatementContext sc = conn.getLanguageConnection().getStatementContext(); |
112 | if ((sc == null) || (sc.getSQLAllowed() < org.apache.derby.catalog.types.RoutineAliasInfo.MODIFIES_SQL_DATA)) |
113 | throw Util.noCurrentConnection(); |
114 | } |
115 | |
116 | return conn.getLocalDriver().getNewNestedConnection(conn); |
117 | } |
118 | |
119 | /** |
120 | * Get a jdbc ResultSet based on the execution ResultSet. |
121 | * |
122 | * @param executionResultSet a result set as gotten from execution |
123 | * |
124 | */ |
125 | public java.sql.ResultSet getResultSet |
126 | ( |
127 | ResultSet executionResultSet |
128 | ) throws SQLException |
129 | { |
130 | EmbedConnection conn = (EmbedConnection) connRef.get(); |
131 | |
132 | EmbedResultSet rs = conn.getLocalDriver().newEmbedResultSet(conn, executionResultSet, |
133 | false, (EmbedStatement) null, true); |
134 | return rs; |
135 | } |
136 | } |