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

📄 connection_base.java

📁 数据仓库工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
//
// Copyright 1998, 1999 CDS Networks, Inc., Medford Oregon
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 3. All advertising materials mentioning features or use of this software
//    must display the following acknowledgement:
//      This product includes software developed by CDS Networks, Inc.
// 4. The name of CDS Networks, Inc.  may not be used to endorse or promote
//    products derived from this software without specific prior
//    written permission.
//
// THIS SOFTWARE IS PROVIDED BY CDS NETWORKS, INC. ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED.  IN NO EVENT SHALL CDS NETWORKS, INC. BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//


package com.internetcds.jdbc.tds;

import java.sql.*;
import java.util.Properties;
import java.util.Vector;

class TdsInstance
{
   public static final String cvsVersion = "$Id: Connection_base.java,v 1.6 2003/12/17 14:02:30 zoran Exp $";

   public boolean inUse = false;
   public Tds     tds   = null;

   public TdsInstance(Tds tds_)
   {
      tds   = tds_;
      inUse = false;
   }
}




/**
 * <P>A Connection represents a session with a specific
 * database. Within the context of a Connection, SQL statements are
 * executed and results are returned.
 *
 * <P>A Connection's database is able to provide information
 * describing its tables, its supported SQL grammar, its stored
 * procedures, the capabilities of this connection, etc. This
 * information is obtained with the getMetaData method.
 *
 * <P><B>Note:</B> By default the Connection automatically commits
 * changes after executing each statement. If auto commit has been
 * disabled, an explicit commit must be done or database changes will
 * not be saved.
 *
 * @author Craig Spannring
 * @author Igor Petrovski
 * @author The FreeTDS project
 * @version  $Id: Connection_base.java,v 1.6 2003/12/17 14:02:30 zoran Exp $
 *
 * @see DriverManager#getConnection
 * @see Statement
 * @see ResultSet
 * @see DatabaseMetaData
 */
public class Connection_base implements ConnectionHelper
{
   public static final String cvsVersion = "$Id: Connection_base.java,v 1.6 2003/12/17 14:02:30 zoran Exp $";


   String     host                = null;
   int        serverType          = -1;   // Can be either Driver.SYBASE or Driver.SQLSERVER
   int        port                = -1;   // Port numbers are _unsigned_ 16 bit, short is too small
   String     database            = null;
   Properties initialProps        = null;


   Vector            tdsPool = null;
   DatabaseMetaData  databaseMetaData = null;
   Vector            allStatements    = null;
//   Tds 					tdsAll			  = null;

   boolean           autoCommit                = true;
   int               transactionIsolationLevel = java.sql.Connection.TRANSACTION_READ_COMMITTED;
   boolean           isClosed                  = false;

   private SQLWarningChain warningChain;

   protected void NotImplemented() throws java.sql.SQLException
      {
         throw new java.sql.SQLException("Not Implemented");
      }





  /**
   * Connect via TDS to a database server.
   *
   * @return a valid connection profile
   * @exception SQLException if a database access error occurs
   */
  public Connection_base(
     Properties props_)
     throws SQLException, com.internetcds.jdbc.tds.TdsException
   {
      host                = props_.getProperty("HOST");
      serverType          = Integer.parseInt(props_.getProperty("SERVERTYPE"));
      port                = Integer.parseInt(props_.getProperty("PORT"));
      database            = props_.getProperty("DBNAME");
      String user         = props_.getProperty("user");
      String password     = props_.getProperty("password");
      initialProps        = props_;

      warningChain = new SQLWarningChain();

      if (user == null)
      {
         user = props_.getProperty("USER");
         if (user == null)
         {
            throw new SQLException("Need a username.");
         }
         props_.put("user", user);
      }

      if (password == null)
      {
         password = props_.getProperty("PASSWORD");
         if (password == null)
         {
            throw new SQLException("Need a password.");
         }
         props_.put("password", password);
      }

      if (tdsPool == null)
      {
         tdsPool = new Vector(20);
      }

      if (allStatements == null)
      {
         allStatements = new Vector(2);
      }

      try
      {
//sinisa
      Tds tmpTds = this.allocateTds();
//		  tdsAll = this.allocateTds();
        freeTds(tmpTds);
//        freeTds(tdsAll);
      }
      catch (java.net.UnknownHostException e)
      {
         throw new SQLException("Unknown host");
      }
      catch (java.io.IOException e)
      {
         throw new SQLException("Network error- " + e.getMessage());
      }
   }

   protected String sqlStatementToInitialize()
   {
      return serverType==Tds.SYBASE ? "set quoted_identifier on set textsize 50000" : "";
   }

   protected String sqlStatementToSetTransactionIsolationLevel()
      throws SQLException
   {
      String   sql = "set transaction isolation level ";

      if (serverType == Tds.SYBASE)
      {
         switch (transactionIsolationLevel)
         {
            case java.sql.Connection.TRANSACTION_READ_UNCOMMITTED:
            {
               throw new SQLException("Bad transaction level");
            }
            case java.sql.Connection.TRANSACTION_READ_COMMITTED:
            {
               sql = sql + "1";
               break;
            }
            case java.sql.Connection.TRANSACTION_REPEATABLE_READ:
            {
               throw new SQLException("Bad transaction level");
            }
            case java.sql.Connection.TRANSACTION_SERIALIZABLE:
            {
               sql = sql + "3";
               break;
            }
            case java.sql.Connection.TRANSACTION_NONE:
            default:
            {
               throw new SQLException("Bad transaction level");
            }
         }
      }
      else
      {
         switch (transactionIsolationLevel)
         {
            case java.sql.Connection.TRANSACTION_READ_UNCOMMITTED:
            {
               sql = sql + " read uncommitted ";
               break;
            }
            case java.sql.Connection.TRANSACTION_READ_COMMITTED:
            {
               sql = sql + " read committed ";
               break;
            }
            case java.sql.Connection.TRANSACTION_REPEATABLE_READ:
            {
               sql = sql + " repeatable read ";
               break;
            }
            case java.sql.Connection.TRANSACTION_SERIALIZABLE:
            {
               throw new SQLException("SQLServer does not support " +
                                      "TRANSACTION_SERIALIZABLE");
            }
            case java.sql.Connection.TRANSACTION_NONE:
            default:
            {
               throw new SQLException("Bad transaction level");
            }
         }
      }
      return sql;
   }


   protected String sqlStatementToSetCommit()
   {
      String result;

      if (serverType == Tds.SYBASE)
      {
         if (autoCommit)
         {
            result = "set CHAINED off ";
         }
         else
         {
            result = "set CHAINED on ";
         }
      }
      else
      {
         if (autoCommit)
         {
            result = "set implicit_transactions off ";
         }
         else
         {
            result = "set implicit_transactions on ";
         }
      }
      return result;
   }

   protected String sqlStatementForSettings()
      throws SQLException
   {
//zoran
      return
         sqlStatementToInitialize() + " " +
         "set quoted_identifier,ansi_null_dflt_on,ansi_padding,ansi_warnings,ansi_nulls on set textsize 2147483647 "+
         sqlStatementToSetTransactionIsolationLevel() + " ";
//         sqlStatementToSetCommit();
   }


   public String getUrl()
   {
      // XXX Is it legal to return something that might not be
      // exactly the URL used to connect?
      return
         ("jdbc:freetds:"
          + (serverType==Tds.SYBASE?"sybase":"sqlserver")
          + "://" + host + ":" + port + "/" + database);
   }

   /**
    * allocate a tds instance to the calling thread.
    * <br>
    * The routine tries to reuse an available tds instance.  If there
    * are no tds instances that aren't in use it will create a new
    * instance.
    *
    * @exception java.sql.SQLException
    * @exception java.net.UnknownHostException
    * @exception com.internetcds.jdbc.tds.TdsException
    * @exception java.io.IOException
    *
    * @return A tds instance to use for database communications.
    */
   synchronized private Tds allocateTds()
      throws java.sql.SQLException, java.net.UnknownHostException,
      com.internetcds.jdbc.tds.TdsException, java.io.IOException
   {
      Tds   result;
      int   i;


      i = findAnAvailableTds();
      if (i == -1)
      {
         Tds   tmpTds = null;
         try
         {
            tmpTds = new Tds((java.sql.Connection)this,
                             initialProps, sqlStatementForSettings());
         }
         catch (SQLException e)
         {
            throw new SQLException(e.getMessage() + "\n" + tdsPool.size()
                                   + " connection are in use by this program");
         }
         TdsInstance tmp = new TdsInstance(tmpTds);
         tdsPool.addElement(tmp);
         i = findAnAvailableTds();
      }
      if (i == -1)
      {
         throw new TdsException("Internal Error.  Couldn't get tds instance");
      }
      if (((TdsInstance)tdsPool.elementAt(i)).inUse)
      {
         throw new TdsException("Internal Error.  tds " + i
                                + " is already allocated");
      }

⌨️ 快捷键说明

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