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

📄 tdsconnection.java

📁 Java写的TDS协议(JDBC/ODBC)实现
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        throws SQLException
    {
        checkClosed();
        Statement result = new TdsStatement(this, type, concurrency);
        allStatements.addElement(result);
        return result;
    }

    /**
     * JDBC 2.0 Creates a <code>PreparedStatement</code> object that will
     * generate <code>ResultSet</code> objects with the given type and
     * concurrency. This method is the same as the <code>prepareStatement
     * </code>method above, but it allows the default result set type and
     * result set concurrency type to be overridden.
     *
     * @param resultSetType        a result set type; see ResultSet.TYPE_XXX
     * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
     * @param sql                  Description of Parameter
     * @return                     a new PreparedStatement object containing
     *     the pre-compiled SQL statement
     * @exception SQLException     if a database access error occurs
     */
    public synchronized java.sql.PreparedStatement prepareStatement(
        String sql,
        int resultSetType,
        int resultSetConcurrency)
        throws SQLException
    {
        checkClosed();
        java.sql.PreparedStatement result = new PreparedStatement_base(
            this, sql, resultSetType, resultSetConcurrency);
        allStatements.addElement(result);
        return result;
    }

    /**
     * JDBC 2.0 Creates a <code>CallableStatement</code> object that will
     * generate <code>ResultSet</code> objects with the given type and
     * concurrency. This method is the same as the <code>prepareCall</code>
     * method above, but it allows the default result set type and result set
     * concurrency type to be overridden.
     *
     * @param resultSetType        a result set type; see ResultSet.TYPE_XXX
     * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
     * @param sql                  Description of Parameter
     * @return                     a new CallableStatement object containing
     *     the pre-compiled SQL statement
     * @exception SQLException     if a database access error occurs
     */
    public synchronized java.sql.CallableStatement prepareCall(
        String sql,
        int resultSetType,
        int resultSetConcurrency) throws SQLException
    {
        checkClosed();
        java.sql.CallableStatement result = new CallableStatement_base(
            this, sql, resultSetType, resultSetConcurrency);
        allStatements.addElement(result);
        return result;
    }

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

    private void checkClosed() throws SQLException
    {
        if( isClosed )
            throw new java.sql.SQLException("Connection closed");
    }

    /**
     * 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.
     *
     * @return    A Tds instance to use for database communications.
     * @exception SQLException
     */
    synchronized Tds allocateTds(boolean mainTds) throws java.sql.SQLException
    {
        Tds result;
        int i;

        try
        {
            if( mainTds && haveMainTds )
                i = 0;
            else
            {
                i = findAnAvailableTds();
                if( i == -1 )
                {
                    Tds tmpTds = new Tds(this, initialProps);
                    TdsInstance tmp = new TdsInstance(tmpTds);
                    tdsPool.addElement(tmp);
                    i = findAnAvailableTds();
                }

                if( i == -1 )
                    throw new TdsException("Internal Error. Couldn't get Tds instance.");

                if( mainTds )
                {
                    Object o = tdsPool.remove(i);
                    tdsPool.insertElementAt(o, 0);
                    haveMainTds = true;
                }
            }

            TdsInstance inst = (TdsInstance)tdsPool.elementAt(i);

            // This also means that i==0 and haveMainTds==true
            if( mainTds )
                synchronized( inst.tds )
                {
                    // SAfe Do nothing, just wait for it to be released (if it's in use).
                }

            if( inst.inUse )
                throw new TdsException("Internal Error. Tds "+i+" is already allocated.");

            inst.inUse = true;
            result = inst.tds;

            result.changeSettings(autoCommit, transactionIsolationLevel);
        }
        catch( net.sourceforge.jtds.jdbc.TdsException e )
        {
            throw new SQLException(e.getMessage());
        }
        catch( java.io.IOException e )
        {
            throw new SQLException(e.getMessage());
        }

        return result;
    }

    /**
     * Find a <code>Tds</code> in the <code>TdsPool</code> that is not in use.
     * <p>
     * Note: This is not synchronized because it's only supposed to be called
     *       by synchronized methods.
     *
     * @return -1 if none was found, otherwise return the index of a free tds
     */
    private int findAnAvailableTds()
    {
        int i, min = haveMainTds ? 1 : 0;

        for( i=tdsPool.size()-1; i>=min && ((TdsInstance)tdsPool.elementAt(i)).inUse; i-- );

        return i==0 && haveMainTds ? -1 : i;
    }

    /**
     * Return a <code>Tds</code> instance back to the <code>Tds</code> pool for reuse.
     * <p>
     * Note: This is not synchronized because it's only supposed to be called by synchronized methods.
     *
     * @param  tds               Description of Parameter
     * @exception  TdsException  Description of Exception
     * @see                      #allocateTds
     */
    void freeTds(Tds tds) throws TdsException
    {
        int i;
        TdsInstance inst;

        i = tdsPool.size();
        do
            inst = (TdsInstance)tdsPool.elementAt(--i);
        while( i>=0 && tds!=inst.tds );

        if( i >= 0 && inst.inUse )
        {
            inst.inUse = false;
            inst.tds.setStatement(null);

            // XXX Should also send a cancel to the server and throw out any data that has already been sent.

            // SAfe Not so sure about that. I think that if you cancel the execution of multiple statements sent at the
            //      same time, the last ones will simply not be executed. We don't want that. If the user explicitly
            //      cancels thir execution, it's his business. We could, however, consume the data.
        }
        else
            throw new TdsException("Tried to free a tds that wasn't in use.");
    }

    /**
     * Implementation for both <code>commit()</code> and
     * <code>rollback()</code> methods.
     * <p>
     * Note: This is not synchronized because it's only supposed to be called
     *       by synchronized methods.
     *
     * @param commit if <code>true</code> commit, else rollback all executing
     *     <code>Statement</code>s
     */
    private void commitOrRollback(boolean commit) throws SQLException
    {
        int i;
        SQLException exception = null;

        // SAfe Completely wrong! If in manual commit mode, the transaction is
        //      not commited when going into auto commit. It has to be commited
        //      manually (even after the commit mode switch), otherwise it will
        //      be rolled back when the connection is closed.
//        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.
        // SAfe Release all Tds instances first.
        for( i=0; i<allStatements.size(); i++ )
        {
            TdsStatement stmt = (TdsStatement) allStatements.elementAt(i);
            try
            {
                // SAfe Need to consume all outstanding input...
                stmt.skipToEnd();
                // SAfe ...then release the Tds
                stmt.releaseTds();
            }
            catch( SQLException ex )
            {
                ex.setNextException(exception);
                exception = ex;
            }
        }

        // MJH  Commit or Rollback Tds connections directly rather than mess with TdsStatement
        // SAfe We'll have to consume all outstanding data before we do this or we risk to crash. We also need
        //      some synchronization here.
        for( i=0; i<tdsPool.size(); i++ )
        {
            try
            {
                if( commit )
                    ((TdsInstance)tdsPool.elementAt(i)).tds.commit();
                else
                    ((TdsInstance)tdsPool.elementAt(i)).tds.rollback();
            }
            // XXX need to put all of these into the warning chain.
            //
            // Don't think so, the warnings would belong to Statement anyway -- SB
            catch( java.sql.SQLException e )
            {
                exception = e;
            }
        }

        if( exception != null )
            throw exception;
    }

    public java.sql.Statement createStatement(int param, int param1, int param2) throws java.sql.SQLException
    {
        throw new UnsupportedOperationException();
    }

    public int getHoldability() throws java.sql.SQLException
    {
        throw new UnsupportedOperationException();
    }

    public java.sql.CallableStatement prepareCall(String str, int param, int param2, int param3) throws java.sql.SQLException
    {
        throw new UnsupportedOperationException();
    }

    public java.sql.PreparedStatement prepareStatement(String str, int param) throws java.sql.SQLException
    {
        throw new UnsupportedOperationException();
    }

    public java.sql.PreparedStatement prepareStatement(String str, int[] values) throws java.sql.SQLException
    {
        throw new UnsupportedOperationException();
    }

    public java.sql.PreparedStatement prepareStatement(String str, String[] str1) throws java.sql.SQLException
    {
        throw new UnsupportedOperationException();
    }

    public java.sql.PreparedStatement prepareStatement(String str, int param, int param2, int param3) throws java.sql.SQLException
    {
        throw new UnsupportedOperationException();
    }

    public void releaseSavepoint(java.sql.Savepoint savepoint) throws java.sql.SQLException
    {
        throw new UnsupportedOperationException();
    }

    public void rollback(java.sql.Savepoint savepoint) throws java.sql.SQLException
    {
        throw new UnsupportedOperationException();
    }

    public void setHoldability(int param) throws java.sql.SQLException
    {
        throw new UnsupportedOperationException();
    }

    public java.sql.Savepoint setSavepoint() throws java.sql.SQLException
    {
        throw new UnsupportedOperationException();
    }

    public java.sql.Savepoint setSavepoint(String str) throws java.sql.SQLException
    {
        throw new UnsupportedOperationException();
    }

}

⌨️ 快捷键说明

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