userconnection.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 978 行 · 第 1/2 页

JAVA
978
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.sql;import com.caucho.log.Log;import com.caucho.util.L10N;import java.sql.*;import java.util.ArrayList;import java.util.Map;import java.util.Properties;import java.util.logging.Level;import java.util.logging.Logger;/** * Adapts the JDBC 2.0 connection pooling and XA support for database drivers * which don't understand it. * * <p>Closing the connection will return the real connection to the pool * and close any statements. */public class UserConnection implements java.sql.Connection {  protected static final Logger log = Log.open(UserConnection.class);  protected static final L10N L = new L10N(UserConnection.class);  // The parent XAConnectionAdapter  private ManagedConnectionImpl _mConn;  // Maximum statements saved for automatic closing  private Statement _statement;  // ArrayList of all the statements created by this connection  private ArrayList<Statement> _statements;  /**   * Creates a new PooledConnection.   *   * @param pool the pool the connection belongs to.   * @param conn the underlying connection.   */  UserConnection(ManagedConnectionImpl mConn)  {    if (mConn == null || mConn.getDriverConnection() == null)      throw new NullPointerException();    _mConn = mConn;  }  /**   * Returns the underlying connection.   */  public Connection getConnection()    throws SQLException  {    Connection conn = getDriverConnection();    if (conn instanceof com.caucho.sql.spy.SpyConnection)      conn = ((com.caucho.sql.spy.SpyConnection)conn).getConnection();    return conn;  }  public Class getDriverClass()  {    return getMConn().getDriverClass();  }    public String getURL()  {    return getMConn().getDBPool().getURL();  }  /**   * Associates with a different mConn.   */  void associate(ManagedConnectionImpl mConn)  {    _mConn = mConn;  }  /**   * JDBC api to create a new statement.  Any SQL exception thrown here   * will make the connection invalid, i.e. it can't be put back into   * the pool.   *   * @return a new JDBC statement.   */  public Statement createStatement()    throws SQLException  {    Statement stmt;    Connection conn = getDriverConnection();    try {      stmt = conn.createStatement();    } catch (SQLException e) {      fatalEvent();      throw e;    }    addStatement(stmt);    if (_mConn.isWrapStatements())      return new UserStatement(this, stmt);    else      return stmt;  }  /**   * JDBC api to create a new statement.  Any SQL exception thrown here   * will make the connection invalid, i.e. it can't be put back into   * the pool.   *   * @return a new JDBC statement.   */  public Statement createStatement(int resultSetType, int resultSetConcurrency)    throws SQLException  {    Statement stmt;    Connection conn = getDriverConnection();    try {      stmt = conn.createStatement(resultSetType, resultSetConcurrency);    } catch (SQLException e) {      fatalEvent();      throw e;    }    addStatement(stmt);    if (_mConn.isWrapStatements())      return new UserStatement(this, stmt);    else      return stmt;  }  /**   * Creates a statement.   */  public Statement createStatement(int resultSetType,                                   int resultSetConcurrency,                                   int resultSetHoldability)    throws SQLException  {    Statement stmt;    Connection conn = getDriverConnection();    try {      stmt = conn.createStatement(resultSetType,                                  resultSetConcurrency,                                  resultSetHoldability);    } catch (SQLException e) {      fatalEvent();      throw e;    }    addStatement(stmt);    if (_mConn.isWrapStatements())      return new UserStatement(this, stmt);    else      return stmt;  }  /**   * Returns a prepared statement with the given sql.   *   * @param sql the prepared sql.   */  public PreparedStatement prepareStatement(String sql)    throws SQLException  {    PreparedStatement stmt;    if (getDriverConnection() == null) {      fatalEvent();      throw new SQLException(L.l("can't create statement from closed connection."));    }    try {      stmt = getMConn().prepareStatement(this, sql);    } catch (SQLException e) {      getMConn().fatalEvent(e);      throw e;    }    addStatement(stmt);    if (_mConn.isWrapStatements())      return new UserPreparedStatement(this, stmt);    else      return stmt;  }  /**   * Returns a prepared statement with the given sql.   *   * @param sql the prepared sql.   */  public PreparedStatement prepareStatement(String sql,                                            int resultSetType,                                            int resultSetConcurrency)    throws SQLException  {    PreparedStatement stmt;    if (getDriverConnection() == null) {      fatalEvent();      throw new SQLException(L.l("can't create statement from closed connection."));    }    try {      stmt = getDriverConnection().prepareStatement(sql, resultSetType, resultSetConcurrency);    } catch (SQLException e) {      fatalEvent();      throw e;    }    addStatement(stmt);    if (_mConn.isWrapStatements())      return new UserPreparedStatement(this, stmt);    else      return stmt;  }  /**   * Returns a prepared statement with the given sql.   *   * @param sql the prepared sql.   */  public PreparedStatement prepareStatement(String sql,                                            int resultSetType,                                            int resultSetConcurrency,                                            int resultSetHoldability)    throws SQLException  {    PreparedStatement stmt;    if (getDriverConnection() == null) {      fatalEvent();      throw new SQLException(L.l("can't create statement from closed connection."));    }    try {      stmt = getDriverConnection().prepareStatement(sql,                                              resultSetType,                                              resultSetConcurrency,                                              resultSetHoldability);    } catch (SQLException e) {      fatalEvent();      throw e;    }    addStatement(stmt);    if (_mConn.isWrapStatements())      return new UserPreparedStatement(this, stmt);    else      return stmt;  }  /**   * Returns a prepared statement with the given sql.   *   * @param sql the prepared sql.   */  public PreparedStatement prepareStatement(String sql,                                            int resultSetType)    throws SQLException  {    PreparedStatement stmt;    if (getDriverConnection() == null) {      fatalEvent();      throw new SQLException(L.l("can't create statement from closed connection."));    }    try {      stmt = getMConn().prepareStatement(this, sql, resultSetType);    } catch (SQLException e) {      fatalEvent();      throw e;    }    if (stmt == null)      throw new IllegalStateException(L.l("prepareStatement returned empty SQL\n{0}",					  sql));    addStatement(stmt);    if (_mConn.isWrapStatements())      return new UserPreparedStatement(this, stmt);    else      return stmt;  }  /**   * Returns a prepared statement with the given sql.   *   * @param sql the prepared sql.   */  public PreparedStatement prepareStatement(String sql,                                            int []columnIndexes)    throws SQLException  {    PreparedStatement stmt;    if (getDriverConnection() == null) {      fatalEvent();      throw new SQLException(L.l("can't create statement from closed connection."));    }    try {      stmt = getDriverConnection().prepareStatement(sql, columnIndexes);    } catch (SQLException e) {      fatalEvent();      throw e;    }    addStatement(stmt);    if (_mConn.isWrapStatements())      return new UserPreparedStatement(this, stmt);    else      return stmt;  }  /**   * Returns a prepared statement with the given sql.   *   * @param sql the prepared sql.   */  public PreparedStatement prepareStatement(String sql,                                            String []columnNames)    throws SQLException  {    PreparedStatement stmt;    if (getDriverConnection() == null) {      fatalEvent();      throw new SQLException(L.l("can't create statement from closed connection."));    }    try {      stmt = getDriverConnection().prepareStatement(sql, columnNames);    } catch (SQLException e) {      fatalEvent();      throw e;    }    addStatement(stmt);    if (_mConn.isWrapStatements())      return new UserPreparedStatement(this, stmt);    else      return stmt;  }  public CallableStatement prepareCall(String sql, int resultSetType,               int resultSetConcurrency)    throws SQLException  {    CallableStatement stmt;    if (getDriverConnection() == null) {      fatalEvent();      throw new SQLException(L.l("can't create statement from closed connection."));    }    try {      stmt = getDriverConnection().prepareCall(sql, resultSetType, resultSetConcurrency);    } catch (SQLException e) {      fatalEvent();      throw e;    }    addStatement(stmt);    if (_mConn.isWrapStatements())      return new UserCallableStatement(this, stmt);    else      return stmt;  }  public CallableStatement prepareCall(String sql)    throws SQLException  {    CallableStatement stmt;    if (getDriverConnection() == null) {      fatalEvent();      throw new SQLException(L.l("can't create statement from closed connection."));    }    try {      stmt = getDriverConnection().prepareCall(sql);    } catch (SQLException e) {      fatalEvent();      throw e;    }    addStatement(stmt);    if (_mConn.isWrapStatements())      return new UserCallableStatement(this, stmt);    else      return stmt;  }  public CallableStatement prepareCall(String sql,                                       int resultSetType,                                       int resultSetConcurrency,                                       int resultSetHoldability)    throws SQLException  {    CallableStatement stmt;    if (getDriverConnection() == null) {      fatalEvent();      throw new SQLException(L.l("can't create statement from closed connection."));    }    try {      stmt = getDriverConnection().prepareCall(sql,                                         resultSetType,                                         resultSetConcurrency,                                         resultSetHoldability);    } catch (SQLException e) {      fatalEvent();      throw e;    }    addStatement(stmt);    if (_mConn.isWrapStatements())      return new UserCallableStatement(this, stmt);    else      return stmt;  }  /**   * JDBC api to return the connection's catalog.   *   * @return the JDBC catalog.   */  public String getCatalog()    throws SQLException  {    try {      return getDriverConnection().getCatalog();    } catch (SQLException e) {      fatalEvent();      throw e;    }  }  /**   * Sets the JDBC catalog.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?