001: /*
002:
003: Derby - Class org.apache.derby.impl.jdbc.EmbedConnectionContext
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: //depot/main/java/org.apache.derby.impl.jdbc/EmbedConnectionContext.java#24 - edit change 16899 (text)
023: package org.apache.derby.impl.jdbc;
024:
025: // This is the recommended super-class for all contexts.
026: import org.apache.derby.iapi.services.context.ContextImpl;
027: import org.apache.derby.iapi.services.context.ContextManager;
028: import org.apache.derby.iapi.sql.conn.StatementContext;
029: import org.apache.derby.iapi.jdbc.ConnectionContext;
030: import org.apache.derby.iapi.error.StandardException;
031: import org.apache.derby.iapi.sql.ResultSet;
032:
033: import org.apache.derby.iapi.error.ExceptionSeverity;
034: import java.sql.SQLException;
035: import java.util.Vector;
036: import java.util.Enumeration;
037:
038: /**
039: @author djd
040: */
041: class EmbedConnectionContext extends ContextImpl implements
042: ConnectionContext {
043:
044: /**
045: We hold a soft reference to the connection so that when the application
046: releases its reference to the Connection without closing it, its finalize
047: method will be called, which will then close the connection. If a direct
048: reference is used here, such a Connection will never be closed or garbage
049: collected as modules hold onto the ContextManager and thus there would
050: be a direct reference through this object.
051: */
052: private java.lang.ref.SoftReference connRef;
053:
054: EmbedConnectionContext(ContextManager cm, EmbedConnection conn) {
055: super (cm, ConnectionContext.CONTEXT_ID);
056:
057: connRef = new java.lang.ref.SoftReference(conn);
058: }
059:
060: public void cleanupOnError(Throwable error) {
061:
062: if (connRef == null)
063: return;
064:
065: EmbedConnection conn = (EmbedConnection) connRef.get();
066:
067: if (error instanceof StandardException) {
068:
069: StandardException se = (StandardException) error;
070: if (se.getSeverity() < ExceptionSeverity.SESSION_SEVERITY) {
071:
072: // any error in auto commit mode that does not close the
073: // session will cause a rollback, thus remvoing the need
074: // for any commit. We could check this flag under autoCommit
075: // being true but the flag is ignored when autoCommit is false
076: // so why add the extra check
077: if (conn != null) {
078: conn.needCommit = false;
079: }
080: return;
081: }
082: }
083:
084: // This may be a transaction without connection.
085: if (conn != null)
086: conn.setInactive(); // make the connection inactive & empty
087:
088: connRef = null;
089: popMe();
090: }
091:
092: //public java.sql.Connection getEmbedConnection()
093: //{
094: /// return conn;
095: //}
096:
097: /**
098: Get a connection equivalent to the call
099: <PRE>
100: DriverManager.getConnection("jdbc:default:connection");
101: </PRE>
102: */
103: public java.sql.Connection getNestedConnection(boolean internal)
104: throws SQLException {
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()
113: .getStatementContext();
114: if ((sc == null)
115: || (sc.getSQLAllowed() < org.apache.derby.catalog.types.RoutineAliasInfo.MODIFIES_SQL_DATA))
116: throw Util.noCurrentConnection();
117: }
118:
119: return conn.getLocalDriver().getNewNestedConnection(conn);
120: }
121:
122: /**
123: * Get a jdbc ResultSet based on the execution ResultSet.
124: *
125: * @param executionResultSet a result set as gotten from execution
126: *
127: */
128: public java.sql.ResultSet getResultSet(ResultSet executionResultSet)
129: throws SQLException {
130: EmbedConnection conn = (EmbedConnection) connRef.get();
131:
132: EmbedResultSet rs = conn.getLocalDriver().newEmbedResultSet(
133: conn, executionResultSet, false, (EmbedStatement) null,
134: true);
135: return rs;
136: }
137: }
|