managedconnectionimpl.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 903 行 · 第 1/2 页
JAVA
903 行
hasItem = ! item.isRemoved(); } } PreparedStatement pStmt; pStmt = conn.prepareStatement(sql); if (hasItem) return pStmt; key = new PreparedStatementKey(sql); PreparedStatementCacheItem item; item = new PreparedStatementCacheItem(key, pStmt, this); UserPreparedStatement upStmt = item.toActive(uConn); if (upStmt == null) throw new IllegalStateException("preparedStatement can't activate"); _preparedStatementCache.put(key, item); return upStmt; } /** * Removes a cached item. */ void remove(PreparedStatementKey key) { _preparedStatementCache.remove(key); } public void connectionClosed(javax.sql.ConnectionEvent event) { sendFatalEvent(new SQLException(L.l("unexpected close event from pool"))); closeEvent(null); } public void connectionErrorOccurred(javax.sql.ConnectionEvent event) { sendFatalEvent(event.getSQLException()); } /** * Sends the close event. */ public void closeEvent(UserConnection userConn) { if (_listener != null) { if (_connException != null) { sendFatalEvent(_connException); } ConnectionEvent evt; synchronized (this) { evt = _connClosedEvent; _connClosedEvent = null; } if (evt == null) evt = new ConnectionEvent(this, ConnectionEvent.CONNECTION_CLOSED); evt.setConnectionHandle(userConn); _listener.connectionClosed(evt); evt.setConnectionHandle(null); _connClosedEvent = evt; _lastEventTime = Alarm.getCurrentTime(); } } /** * Sends the fatal event. */ public void fatalEvent() { fatalEvent(new ResourceException("fatal event")); } /** * Sends the fatal event. */ public void fatalEvent(Exception e) { if (_pooledConnection != null) { } else if (e instanceof ResourceException) _connException = (ResourceException) e; else _connException = new ResourceException(e); } /** * Sends the fatal event. */ public void sendFatalEvent(Exception e) { if (_listener != null) { ConnectionEvent event; event = new ConnectionEvent(this, ConnectionEvent.CONNECTION_ERROR_OCCURRED, e); _listener.connectionErrorOccurred(event); } } /** * When closed, the item is not put into the idle pool. */ void killPool() { if (_listener != null) { ConnectionEvent event; event = new ConnectionEvent(this, ConnectionEvent.CONNECTION_ERROR_OCCURRED); _listener.connectionErrorOccurred(event); } } /** * Sets the auto-commit. */ public void setAutoCommit(boolean autoCommit) throws SQLException { try { _autoCommit = autoCommit; _driverConnection.setAutoCommit(autoCommit); } catch (SQLException e) { fatalEvent(); throw e; } } /** * Sets the read only attribute. */ public void setReadOnly(boolean readOnly) throws SQLException { try { _readOnly = readOnly; _driverConnection.setReadOnly(readOnly); } catch (SQLException e) { fatalEvent(); throw e; } } /** * Sets the JDBC catalog. */ public void setCatalog(String catalog) throws SQLException { try { if (! _hasCatalog) { _hasCatalog = true; _catalogOrig = _driverConnection.getCatalog(); _catalog = _catalogOrig; } if (catalog == null || catalog.length() == 0) { // Clear the current catalog name but don't invoke setCatalog() // on the driver. _catalog = null; } else if (_catalog != null && _catalog.equals(catalog)) { // No-op when setting to the currently selected catalog name } else { _driverConnection.setCatalog(catalog); _catalog = catalog; } } catch (SQLException e) { fatalEvent(); throw e; } } /** * Sets the connection's type map. */ public void setTypeMap(Map map) throws SQLException { try { if (_typeMap == null) _typeMap = _driverConnection.getTypeMap(); _driverConnection.setTypeMap(map); } catch (SQLException e) { throw e; } } /** * Sets the connection's isolation. */ public void setTransactionIsolation(int isolation) throws SQLException { try { _oldIsolation = _driverConnection.getTransactionIsolation(); _isolation = isolation; _driverConnection.setTransactionIsolation(isolation); } catch (SQLException e) { throw e; } } /** * Cleans up the instance. */ public void cleanup() throws ResourceException { Connection conn = _driverConnection; if (conn == null) return; try { /* // If there's a pooled connection, it can cleanup itself if (_pooledConnection != null) { _autoCommit = true; _driverConnection = _pooledConnection.getConnection(); _isolation = _oldIsolation = -1; return; } */ if (_readOnly) conn.setReadOnly(false); _readOnly = false; if (_catalog != null && ! _catalog.equals(_catalogOrig) && _catalogOrig != null && ! "".equals(_catalogOrig)) { conn.setCatalog(_catalogOrig); } _catalog = null; if (_typeMap != null) conn.setTypeMap(_typeMap); _typeMap = null; // Oracle requires a rollback after a reset of // the transaction isolation, since setting the isolation // starts a new transaction boolean needsRollback = ! _autoCommit; if (_isolation != _oldIsolation) { needsRollback = true; conn.setTransactionIsolation(_oldIsolation); } _isolation = _oldIsolation; if (needsRollback) conn.rollback(); if (! _autoCommit) { conn.setAutoCommit(true); } _autoCommit = true; conn.clearWarnings(); } catch (SQLException e) { throw new ResourceException(e); } } /** * Returns true if the connection is valid. */ boolean isValid() { try { return ping(); } catch (Throwable e) { log.log(Level.FINE, e.toString(), e); return false; } } /** * Checks the validity with ping. */ private boolean ping() throws ResourceException { DBPoolImpl dbPool = _factory.getDBPool(); long now = Alarm.getCurrentTime(); if (now < _lastEventTime + 1000) return true; if (! dbPool.isPing()) return true; long pingInterval = dbPool.getPingInterval(); if (pingInterval > 0 && now < _lastEventTime + pingInterval) return true; Connection conn = _driverConnection; try { if (conn == null || conn.isClosed()) { return false; } String pingQuery = dbPool.getPingQuery(); if (pingQuery == null) return true; Statement stmt = conn.createStatement(); try { ResultSet rs = stmt.executeQuery(pingQuery); rs.close(); return true; } finally { stmt.close(); } } catch (SQLException e) { throw new ResourceException(e); } } /** * Destroys the physical connection. */ public void destroy() throws ResourceException { log.finer("destroy " + this); PooledConnection poolConn = _pooledConnection; _pooledConnection = null; Connection driverConn = _driverConnection; _driverConnection = null; if (_preparedStatementCache != null) { Iterator<PreparedStatementCacheItem> iter; iter = _preparedStatementCache.values(); while (iter.hasNext()) { PreparedStatementCacheItem item = iter.next(); item.destroy(); } } try { if (poolConn != null) { poolConn.close(); driverConn = null; } } catch (SQLException e) { throw new ResourceException(e); } try { if (driverConn != null) driverConn.close(); } catch (SQLException e) { log.log(Level.WARNING, e.toString(), e); } } public String toString() { return "ManagedConnectionImpl[" + _id + "]"; } class LocalTransactionImpl implements LocalTransaction { private boolean _oldAutoCommit; public void begin() throws ResourceException { try { _oldAutoCommit = _autoCommit; setAutoCommit(false); } catch (SQLException e) { throw new ResourceException(e) ; } } public void commit() throws ResourceException { Connection conn = _driverConnection; if (conn == null) throw new ResourceException(L.l("connection is closed")); try { conn.commit(); } catch (SQLException e) { throw new ResourceException(e) ; } try { setAutoCommit(_oldAutoCommit); } catch (SQLException e) { throw new ResourceException(e) ; } } public void rollback() throws ResourceException { Connection conn = _driverConnection; if (conn == null) throw new ResourceException(L.l("connection is closed")); try { conn.rollback(); } catch (SQLException e) { throw new ResourceException(e) ; } try { setAutoCommit(_oldAutoCommit); } catch (SQLException e) { throw new ResourceException(e) ; } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?