⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connection_base.java

📁 数据仓库工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      ((TdsInstance)tdsPool.elementAt(i)).inUse = true;
      result = ((TdsInstance)(tdsPool.elementAt(i))).tds;

      result.changeSettings(null, sqlStatementForSettings());

      return result;
   }

   /**
    *  Find a tds in the TdsPool that is not in use.
    *
    * @return -1 if none were found, otherwise return the index a tds
    */
   private int findAnAvailableTds()
   {
      int   i;

      for(i=tdsPool.size()-1;
          i>=0 && ((TdsInstance)tdsPool.elementAt(i)).inUse;
          i--)
      {
         // nop
      }
      return i;
   }



   public void markAsClosed(java.sql.Statement stmt) throws TdsException
   {   	
      if (!allStatements.removeElement((com.internetcds.jdbc.tds.Statement)stmt))
      {
//commented, if call close() more than once from application.      	
         //throw new TdsException("Statement was not known by the connection");
      }
   }

   /**
    * return a tds instance back to the tds pool for reuse.
    *
    * @see allocateTds
    */
   private void freeTds(Tds tds)
      throws TdsException
   {
      int   i;

      i = -1;
      do
      {
         i++;
      } while(i<tdsPool.size()
              && tds != ((TdsInstance)tdsPool.elementAt(i)).tds);

      if (i<tdsPool.size())
      {
         ((TdsInstance)tdsPool.elementAt(i)).inUse = false;

         // XXX Should also send a cancel to the server and throw out any
         // data that has already been sent.
      }
      else
      {
         throw new TdsException("Tried to free a tds that wasn't in use");
      }
   }

   /**
    * return a tds instance back to the tds pool for reuse.
    *
    * A thread that is using a tds instance should return the
    * instance back to the tds pool when it is finished using it.
    *
    * @see allocateTds
    */
   public void relinquish(Tds tds)
      throws TdsException
   {
      freeTds(tds);
   }


   /**
    * SQL statements without parameters are normally executed using
    * Statement objects.  If the same SQL statement is executed many
    * times, it is more efficient to use a PreparedStatement
    *
    * JDBC 2.0
    *
    * Result sets created using the returned Statement will have
    * forward-only type, and read-only concurrency, by default.
    *
    * @return a new Statement object
    * @exception SQLException passed through from the constructor
    */
   public java.sql.Statement createStatement() throws SQLException
   {
//sinisa
      Tds  tmpTds = null;
      try
      {
         tmpTds = this.allocateTds();
      }
      catch(com.internetcds.jdbc.tds.TdsException e)
      {
         throw new SQLException(e.getMessage());
      }
      catch(java.io.IOException e)
      {
         throw new SQLException(e.getMessage());
      }
//sinisa
      com.internetcds.jdbc.tds.Statement  result;
      result = new com.internetcds.jdbc.tds.Statement(this, tmpTds);
//      result = new com.internetcds.jdbc.tds.Statement(this, tdsAll);

      allStatements.addElement(result);
      return result;
   }



   /**
    * A SQL statement with or without IN parameters can be
    * pre-compiled and stored in a PreparedStatement object. This
    * object can then be used to efficiently execute this statement
    * multiple times.
    *
    * <P><B>Note:</B> This method is optimized for handling
    * parametric SQL statements that benefit from precompilation. If
    * the driver supports precompilation, prepareStatement will send
    * the statement to the database for precompilation. Some drivers
    * may not support precompilation. In this case, the statement may
    * not be sent to the database until the PreparedStatement is
    * executed.  This has no direct affect on users; however, it does
    * affect which method throws certain SQLExceptions.
    *
    * JDBC 2.0
    *
    * Result sets created using the returned PreparedStatement will have
    * forward-only type and read-only concurrency, by default.
    *
    * @param sql a SQL statement that may contain one or more '?' IN
    * parameter placeholders
    * @return a new PreparedStatement object containing the
    * pre-compiled statement
    * @exception SQLException if a database-access error occurs.
    */
   public java.sql.PreparedStatement prepareStatement(String sql)
      throws SQLException
   {
      java.sql.PreparedStatement   result;

//sinisa
      Tds tmpTds = null;
      try
      {
         tmpTds  = this.allocateTds();
      }
      catch (java.io.IOException e)
      {
         throw new SQLException(e.getMessage());
      }
      catch (com.internetcds.jdbc.tds.TdsException e)
      {
         throw new SQLException(e.getMessage());
      }

      result = Constructors.newPreparedStatement(this, tmpTds, sql);
//      result = Constructors.newPreparedStatement(this, tdsAll, sql);

      allStatements.addElement(result);

      return result;
   }

  /**
   * A SQL stored procedure call statement is handled by creating a
   * CallableStatement for it.  The CallableStatement provides methods
   * for setting up its IN and OUT parameters and methods for executing
   * it.
   *
   * <B>Note:</B> This method is optimised for handling stored procedure
   * call statements.  Some drivers may send the call statement to the
   * database when the prepareCall is done; others may wait until the
   * CallableStatement is executed.  This has no direct effect on users;
   * however, it does affect which method throws certain SQLExceptions
   *
   * JDBC 2.0
   *
   * Result sets created using the returned CallableStatement will have
   * forward-only type and read-only concurrency, by default.
   *
   * @param sql a SQL statement that may contain one or more '?' parameter
   *    placeholders.  Typically this statement is a JDBC function call
   *    escape string.
   * @return a new CallableStatement object containing the pre-compiled
   *    SQL statement
   * @exception SQLException if a database access error occurs
   */
  public java.sql.CallableStatement prepareCall(String sql) throws SQLException
   {
      java.sql.CallableStatement   result;

//sinisa
      Tds tmpTds = null;
      try
      {
         tmpTds  = this.allocateTds();
      }
      catch (java.io.IOException e)
      {
         throw new SQLException(e.getMessage());
      }
      catch (com.internetcds.jdbc.tds.TdsException e)
      {
         throw new SQLException(e.getMessage());
      }

      result = Constructors.newCallableStatement(this, tmpTds, sql);
//      result = Constructors.newCallableStatement(this, tdsAll, sql);
      allStatements.addElement(result);

      return result;
   }

   /**
    * A driver may convert the JDBC sql grammar into its system's
    * native SQL grammar prior to sending it; nativeSQL returns the
    * native form of the statement that the driver would have sent.
    *
    * @param sql a SQL statement that may contain one or more '?'
    *    parameter placeholders
    * @return the native form of this statement
    * @exception SQLException if a database access error occurs
    */

   public String nativeSQL(String sql) throws SQLException
   {
      return Tds.toNativeSql(sql, serverType);
   }


   /**
    * If a connection is in auto-commit mode, then all its SQL
    * statements will be executed and committed as individual
    * transactions.  Otherwise, its SQL statements are grouped into
    * transactions that are terminated by either commit() or
    * rollback().  By default, new connections are in auto-commit
    * mode.
    *
    * The commit occurs when the statement completes or the next
    * execute occurs, whichever comes first. In the case of
    * statements returning a ResultSet, the statement completes when
    * the last row of the ResultSet has been retrieved or the
    * ResultSet has been closed. In advanced cases, a single
    * statement may return multiple results as well as output
    * parameter values. Here the commit occurs when all results and
    * output param values have been retrieved.
    *
    * @param value true enables auto-commit; false disables
    * auto-commit.
    * @exception SQLException if a database-access error occurs.
    */
   public void setAutoCommit(boolean value) throws SQLException
   {
      int    i;
      String sql = null;

      autoCommit = value;
      sql = sqlStatementToSetCommit();
//sinisa
      for(i=0; i<allStatements.size(); i++)
      {
         Statement  stmt = (Statement)allStatements.elementAt(i);
//         Statement  stmt = (com.internetcds.jdbc.tds.Statement)this.createStatement();

         {
            // Note-  stmt.execute implicitly eats the END_TOKEN
            // that will come back from the commit command
            stmt.execute(sql);
            stmt.execute("BEGIN TRAN");
         }
      }
   }


   /**
    * Get the current auto-commit state.
    *
    * @return Current state of auto-commit mode.
    * @exception SQLException if a database-access error occurs.
    * @see #setAutoCommit
    */
   public boolean getAutoCommit() throws SQLException
   {
      return autoCommit;
   }


   /**
    * Commit makes all changes made since the previous
    * commit/rollback permanent and releases any database locks
    * currently held by the Connection. This method should only be
    * used when auto commit has been disabled.
    *
    * @exception SQLException if a database-access error occurs.
    * @see #setAutoCommit
    */
   public void commit() throws SQLException
   {
      commitOrRollback(true);
   }


   /**
    * Rollback drops all changes made since the previous
    * commit/rollback and releases any database locks currently held
    * by the Connection. This method should only be used when auto
    * commit has been disabled.
    *
    * @exception SQLException if a database-access error occurs.
    * @see #setAutoCommit
    */
   public void rollback() throws SQLException
   {
      commitOrRollback(false);
   }

   private void commitOrRollback(boolean commit)
      throws SQLException
   {
      int          i;
      SQLException exception = null;

      if (autoCommit)
      {
         throw new SQLException("This method should only be " +
                                " used when auto commit has been disabled.");
      }


      // XXX race condition here.  It is possible that a statement could
      // close while running this for loop.
//sinisa
      for(i=0; i<allStatements.size(); i++)
      {
         Statement  stmt = (Statement)allStatements.elementAt(i);
//String sql = "IF @@TRANCOUNT > 0 COMMIT TRAN";
//String sqlRollback = "IF @@TRANCOUNT > 0 ROLLBACK TRAN ";
//Statement  stmt = (com.internetcds.jdbc.tds.Statement)this.createStatement();

         try
         {
            if (commit)

⌨️ 快捷键说明

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