Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * $Id: BaseGDSImpl.java 58723 2013-10-26 14:51:28Z mrotteveel $
    * 
    * Firebird Open Source J2ee connector - jdbc driver
    *
    * Distributable under LGPL license.
    * You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
    *
    * This program 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.  See the
   * LGPL License for more details.
   *
   * This file was created by members of the firebird development team.
   * All individual contributions remain the Copyright (C) of those
   * individuals.  Contributors to this file are either listed here or
   * can be obtained from a CVS history command.
   *
   * All rights reserved.
   */
  package org.firebirdsql.gds.impl.jni;
  
  
  
  public abstract class BaseGDSImpl extends AbstractGDS {
  
      private static Logger log = LoggerFactory.getLogger(BaseGDSImpl.class,
              false);
      
      private static final String WARNING_CONNECT_TIMEOUT_NATIVE = 
              "WARNING: The native driver does not apply connectTimeout for establishing the socket connection (only for protocol negotiation with the Firebird server), " + 
              "it will not detect unreachable hosts within the specified timeout";
     
      protected static final byte[] DESCRIBE_DATABASE_INFO_BLOCK = new byte[] {
          .
          .,
          . };
  
  
      private static byte[] stmtInfo = new byte[] {
                  .,
                  ..};
  
      private static int INFO_SIZE = 128;
  
      public int isc_api_handle;
      
      public BaseGDSImpl() {
          super();
      }
  
      public BaseGDSImpl(GDSType gdsType) {
          super(gdsType);
      }
  
      protected abstract String getServerUrl(String file_name)
              throws GDSException;
  
          return new BlobParameterBufferImp();
      }
  
          return new DatabaseParameterBufferImp();
      }
  
      public IscBlobHandle createIscBlobHandle() {
          return new isc_blob_handle_impl();
      }
  
      // Handle declaration methods
      public IscDbHandle createIscDbHandle() {
          return new isc_db_handle_impl();
      }
  
      public IscStmtHandle createIscStmtHandle() {
          return new isc_stmt_handle_impl();
      }
  
      public IscSvcHandle createIscSvcHandle() {
          return new isc_svc_handle_impl();
      }
  
      public IscTrHandle createIscTrHandle() {
          return new isc_tr_handle_impl();
     }
 
     // GDS Implementation
     // ----------------------------------------------------------------------------------------------
 
         return new ServiceParameterBufferImp();
     }
 
     public ServiceRequestBuffer createServiceRequestBuffer(int taskIdentifier) {
         return new ServiceRequestBufferImp(taskIdentifier);
     }
 
     // isc_attach_database
     // ---------------------------------------------------------------------------------------------
     public void iscAttachDatabase(String file_nameIscDbHandle db_handle,
             DatabaseParameterBuffer databaseParameterBuffer)
             throws GDSException {
         validateHandle(db_handle);
 
         final byte[] dpbBytes;
         final String filenameCharset;
         if (databaseParameterBuffer != null) {
             DatabaseParameterBuffer cleanDPB = ((DatabaseParameterBufferExtension)databaseParameterBuffer).removeExtensionParams();
             if (cleanDPB.hasArgument(.)) {
                 // For the native driver isc_dpb_connect_timeout is not a socket connect timeout
                 // It only applies to the steps for op_accept (negotiating protocol, etc)
                 if ( != null) {
                     .warn();
                 }
                 db_handle.addWarning(new GDSWarning());
             }
             if (!cleanDPB.hasArgument(.)) {
             }
             
             dpbBytes = ((DatabaseParameterBufferImpcleanDPB).getBytesForNativeCode();
             filenameCharset = databaseParameterBuffer.getArgumentAsString(.);
         } else {
             dpbBytes = null;
             filenameCharset = null;
         }
 
         String serverUrl = getServerUrl(file_name);
         
         byte[] urlData;
         try {
             if (filenameCharset != null)
                 urlData = serverUrl.getBytes(filenameCharset);
             else
                 urlData = serverUrl.getBytes();
             
             byte[] nullTerminated = new byte[urlData.length + 1];
             System.arraycopy(urlData, 0, nullTerminated, 0, urlData.length);
             urlData = nullTerminated;
         } catch(UnsupportedEncodingException ex) {
             throw new GDSException(.);
         }
         
         synchronized (db_handle) {
             native_isc_attach_database(urlDatadb_handledpbBytes);
         }
 
         parseAttachDatabaseInfo(iscDatabaseInfo(db_handle,
                 , 1024), db_handle);
     }
 
     public byte[] iscBlobInfo(IscBlobHandle handlebyte[] items,
             int buffer_lengththrows GDSException {
         isc_blob_handle_impl blob = validateHandle(handle);
         final isc_db_handle_impl db = validateHandle(blob.getDb());
 
         synchronized (db) {
             return native_isc_blob_info(blobitemsbuffer_length);
         }
     }
 
     // isc_close_blob
     // ---------------------------------------------------------------------------------------------
     public void iscCloseBlob(IscBlobHandle blob_handlethrows GDSException {
         isc_blob_handle_impl blob = validateHandle(blob_handle);
         isc_db_handle_impl db = validateHandle(blob.getDb());
         isc_tr_handle_impl tr = validateHandle(blob.getTr());
 
         synchronized (db) {
             native_isc_close_blob(blob_handle);
         }
 
         tr.removeBlob(blob);
     }
 
     // isc_commit_retaining
     // ---------------------------------------------------------------------------------------------
     public void iscCommitRetaining(IscTrHandle tr_handlethrows GDSException {
         isc_tr_handle_impl tr = validateHandle(tr_handle);
         isc_db_handle_impl db = validateHandle(tr.getDbHandle());
 
         synchronized (db) {
             if (tr.getState() != .
                     && tr.getState() != .) { throw new GDSException(
                     .); }
 
 
             native_isc_commit_retaining(tr_handle);
 
             tr.setState(.);
         }
     }
 
     // isc_commit_transaction
     // ---------------------------------------------------------------------------------------------
     public void iscCommitTransaction(IscTrHandle tr_handlethrows GDSException {
         isc_tr_handle_impl tr = validateHandle(tr_handle);
         isc_db_handle_impl db = validateHandle(tr.getDbHandle());
 
         synchronized (db) {
             if (tr.getState() != .
                     && tr.getState() != .) { throw new GDSException(
                     .); }
 
 
             native_isc_commit_transaction(tr_handle);
 
             tr.setState(.);
 
             tr.unsetDbHandle();
         }
     }
 
     // isc_create_blob2
     // ---------------------------------------------------------------------------------------------
     public void iscCreateBlob2(IscDbHandle db_handleIscTrHandle tr_handle,
             IscBlobHandle blob_handleBlobParameterBuffer blobParameterBuffer)
             throws GDSException {
         isc_db_handle_impl db = validateHandle(db_handle);
         isc_tr_handle_impl tr = validateHandle(tr_handle);
         isc_blob_handle_impl blob = validateHandle(blob_handle);
 
         final byte[] bpb = blobParameterBuffer == null ? null
                 : ((BlobParameterBufferImpblobParameterBuffer)
                         .getBytesForNativeCode();
 
         synchronized (db) {
             native_isc_create_blob2(db_handletr_handleblob_handlebpb);
 
             blob.setDb(db);
             blob.setTr(tr);
             tr.addBlob(blob);
         }
     }
 
     // isc_create_database
     // ---------------------------------------------------------------------------------------------
     public void iscCreateDatabase(String file_nameIscDbHandle db_handle,
             DatabaseParameterBuffer dpb)
             throws GDSException {
         validateHandle(db_handle);
 
         final byte[] dpbBytes;
         final String filenameCharset;
         if (dpb != null) {
             if (cleanDPB.hasArgument(.)) {
                 // For the native driver isc_dpb_connect_timeout is not a socket connect timeout
                 // It only applies to the steps for op_accept (negotiating protocol, etc)
                 if ( != null) {
                     .warn();
                 }
                 db_handle.addWarning(new GDSWarning());
             }
             if (!cleanDPB.hasArgument(.)) {
             }
 
             dpbBytes = ((DatabaseParameterBufferImpcleanDPB).getBytesForNativeCode();
             filenameCharset = dpb.getArgumentAsString(.);
         } else {
             dpbBytes = null;
             filenameCharset = null;
         }
 
         synchronized (db_handle) {
             String serverUrl  = getServerUrl(file_name);
             
             byte[] urlData;
             try {
                 if (filenameCharset != null)
                     urlData = serverUrl.getBytes(filenameCharset);
                 else
                     urlData = serverUrl.getBytes();
                 
                 byte[] nullTerminated = new byte[urlData.length + 1];
                 System.arraycopy(urlData, 0, nullTerminated, 0, urlData.length);
                 urlData = nullTerminated;
 
             } catch(UnsupportedEncodingException ex) {
                 throw new GDSException(.);
             }
             
             native_isc_create_database(urlDatadb_handle,
                     dpbBytes);
         }
     }
 
     // isc_attach_database
     // ---------------------------------------------------------------------------------------------
     public byte[] iscDatabaseInfo(IscDbHandle db_handlebyte[] items,
             int buffer_lengththrows GDSException {
         validateHandle(db_handle);
 
         synchronized (db_handle) {
             final byte[] returnValue = new byte[buffer_length];
 
             native_isc_database_info(db_handleitems.lengthitems,
                     buffer_lengthreturnValue);
 
             return returnValue;
         }
     }
 
     // isc_detach_database
     // ---------------------------------------------------------------------------------------------
     public void iscDetachDatabase(IscDbHandle db_handlethrows GDSException {
         isc_db_handle_impl db = validateHandle(db_handle);
 
         synchronized (db) {
 //            if (db.hasTransactions()) { throw new GDSException(
 //                    ISCConstants.isc_open_trans, db.getOpenTransactionCount()); }
 
             native_isc_detach_database(db_handle);
             db.invalidate();
         }
     }
 
     // isc_drop_database
     // ---------------------------------------------------------------------------------------------
     public void iscDropDatabase(IscDbHandle db_handlethrows GDSException {
         validateHandle(db_handle);
 
         synchronized (db_handle) {
             native_isc_drop_database(db_handle);
         }
     }
 
     // isc_dsql_allocate_statement
     // ---------------------------------------------------------------------------------------------
     public void iscDsqlAllocateStatement(IscDbHandle db_handle,
             IscStmtHandle stmt_handlethrows GDSException {
         isc_db_handle_impl db = validateHandle(db_handle);
         isc_stmt_handle_impl stmt = validateHandle(stmt_handle);
 
         synchronized (db) {
             native_isc_dsql_allocate_statement(db_handlestmt_handle);
 
             stmt.setRsr_rdb((isc_db_handle_impldb_handle);
             stmt.setAllRowsFetched(false);
         }
     }
 
     // isc_dsql_describe
     // ---------------------------------------------------------------------------------------------
     public XSQLDA iscDsqlDescribe(IscStmtHandle stmt_handleint da_version)
             throws GDSException {
         isc_stmt_handle_impl stmt = validateHandle(stmt_handle);
         final isc_db_handle_impl db = validateHandle(stmt.getRsr_rdb());
 
         synchronized (db) {
             // TODO setInSqlda here ??
             stmt.setInSqlda(native_isc_dsql_describe(stmt_handleda_version));
 
             return stmt_handle.getInSqlda();
         }
     }
 
     // isc_dsql_describe_bind
     // ---------------------------------------------------------------------------------------------
     public XSQLDA iscDsqlDescribeBind(IscStmtHandle stmt_handleint da_version)
             throws GDSException {
         isc_stmt_handle_impl stmt = validateHandle(stmt_handle);
         final isc_db_handle_impl db = validateHandle(stmt.getRsr_rdb());
 
         synchronized (db) {
             stmt.setInSqlda(native_isc_dsql_describe_bind(stmt_handleda_version));
 
             return stmt_handle.getInSqlda();
         }
     }
 
     public void iscDsqlExecImmed2(IscDbHandle db_handleIscTrHandle tr_handle,
             byte[] statementint dialectXSQLDA in_xsqldaXSQLDA out_xsqlda)
             throws GDSException {
         validateHandle(db_handle);
 
         synchronized (db_handle) {
             native_isc_dsql_exec_immed2(db_handletr_handle,
                     getZeroTerminatedArray(statement), dialectin_xsqlda,
                     out_xsqlda);
         }
     }
 
     public void iscDsqlExecImmed2(IscDbHandle db_handleIscTrHandle tr_handle,
             String statementint dialectXSQLDA in_xsqldaXSQLDA out_xsqlda)
             throws GDSException {
         iscDsqlExecImmed2(db_handletr_handlestatement"NONE"dialect,
                 in_xsqldaout_xsqlda);
     }
 
     public void iscDsqlExecImmed2(IscDbHandle db_handleIscTrHandle tr_handle,
             String statementString encodingint dialectXSQLDA in_xsqlda,
             XSQLDA out_xsqldathrows GDSException {
         validateHandle(db_handle);
 
         try {
             synchronized (db_handle) {
                 native_isc_dsql_exec_immed2(db_handletr_handle,
                         getByteArrayForString(statementencoding), dialect,
                         in_xsqldaout_xsqlda);
             }
         } catch (UnsupportedEncodingException e) {
             throw new GDSException("Unsupported encoding. " + e.getMessage());
         }
     }
 
     // isc_dsql_execute
     // ---------------------------------------------------------------------------------------------
     public void iscDsqlExecute(IscTrHandle tr_handle,
             IscStmtHandle stmt_handleint da_versionXSQLDA xsqlda)
             throws GDSException {
         iscDsqlExecute2(tr_handlestmt_handleda_versionxsqldanull);
     }
 
     // public synchronized abstract void native_isc_dsql_execute(isc_tr_handle
     // tr_handle, isc_stmt_handle stmt_handle, int da_version, XSQLDA xsqlda)
     // throws GDSException;
 
     // isc_dsql_execute2
     // ---------------------------------------------------------------------------------------------
     public void iscDsqlExecute2(IscTrHandle tr_handle,
             IscStmtHandle stmt_handleint da_versionXSQLDA in_xsqlda,
             XSQLDA out_xsqldathrows GDSException {
         isc_stmt_handle_impl stmt = validateHandle(stmt_handle);
         final isc_db_handle_impl db = validateHandle(stmt.getRsr_rdb());
 
         synchronized (db) {
             // TODO Fetch Statements
             native_isc_dsql_execute2(tr_handlestmt_handleda_version,
                     in_xsqldaout_xsqlda);
 
             if (stmt.getOutSqlda() != nullstmt.notifyOpenResultSet();
 
             if (out_xsqlda != null) {
                 // this would be an Execute procedure
                 stmt.ensureCapacity(1);
                 readSQLData(out_xsqldastmt);
                 stmt.setAllRowsFetched(true);
                 stmt.setSingletonResult(true);
             } else {
                 stmt.setAllRowsFetched(false);
                 stmt.setSingletonResult(false);
             }
             
             stmt.registerTransaction((AbstractIscTrHandle)tr_handle);
         }
     }
 
     public void iscDsqlExecuteImmediate(IscDbHandle db_handle,
             IscTrHandle tr_handlebyte[] statementint dialectXSQLDA xsqlda)
             throws GDSException {
 
         iscDsqlExecImmed2(db_handletr_handlestatementdialectxsqlda,
                 null);
     }
 
     // isc_dsql_execute_immediateX
     // ---------------------------------------------------------------------------------------------
     public void iscDsqlExecuteImmediate(IscDbHandle db_handle,
             IscTrHandle tr_handleString statementint dialectXSQLDA xsqlda)
             throws GDSException {
         iscDsqlExecImmed2(db_handletr_handlestatementdialectxsqlda,
                 null);
     }
 
     public void iscDsqlExecuteImmediate(IscDbHandle db_handle,
             IscTrHandle tr_handleString statementString encoding,
             int dialectXSQLDA xsqldathrows GDSException {
         iscDsqlExecImmed2(db_handletr_handlestatementencodingdialect,
                 xsqldanull);
     }
 
     // isc_dsql_fetch
     // ---------------------------------------------------------------------------------------------
     public void iscDsqlFetch(IscStmtHandle stmt_handleint da_version,
             XSQLDA xsqldaint fetchSizethrows GDSException {
         fetchSize = 1;
 
         if (xsqlda == null) { 
             throw new GDSException(.);
         }
         // TODO: Above declares fetchSize = 1, so parameter is ignored and this check is not necessary
         if (fetchSize <= 0) { 
             throw new GDSException(.);
         }
 
         isc_stmt_handle_impl stmt = validateHandle(stmt_handle);
         isc_db_handle_impl db = validateHandle(stmt.getRsr_rdb());
 
         synchronized (db) {
             // Apply fetchSize
             // Fetch next batch of rows
             stmt.ensureCapacity(fetchSize);
 
             for (int i = 0; i < fetchSizei++) {
                 try {
                     boolean isRowPresent = native_isc_dsql_fetch(stmt_handle,
                             da_versionxsqldafetchSize);
                     if (isRowPresent) {
                         readSQLData(xsqldastmt);
                     } else {
                         stmt.setAllRowsFetched(true);
                         return;
                     }
                 } finally {
                     stmt.notifyOpenResultSet();
                 }
             }
         }
     }
 
     // isc_dsql_free_statement
     // ---------------------------------------------------------------------------------------------
     public void iscDsqlFreeStatement(IscStmtHandle stmt_handleint option)
             throws GDSException {
         isc_stmt_handle_impl stmt = validateHandle(stmt_handle);
         isc_db_handle_impl db = validateHandle(stmt.getRsr_rdb());
 
         synchronized (db) {
             // Does not seem to be possible or necessary to close
             // an execute procedure statement.
             if (stmt.isSingletonResult() && option == .) { return; }
 
             if (option == .) {
                 stmt.setInSqlda(null);
                 stmt.setOutSqlda(null);
                 stmt.setRsr_rdb(null);
             }
 
             native_isc_dsql_free_statement(stmt_handleoption);
             
             // clear association with transaction
             try {
                 AbstractIscTrHandle tr = stmt.getTransaction();
                 if (tr != null)
                     tr.unregisterStatementFromTransaction(stmt);
             } finally {
                 stmt.unregisterTransaction();
             }
 
         }
     }
 
     // isc_dsql_free_statement
     // ---------------------------------------------------------------------------------------------
     public XSQLDA iscDsqlPrepare(IscTrHandle tr_handle,
             IscStmtHandle stmt_handlebyte[] statementint dialect)
             throws GDSException {
 
         validateHandle(tr_handle);
         isc_stmt_handle_impl stmt = validateHandle(stmt_handle);
         isc_db_handle_impl db = validateHandle(stmt.getRsr_rdb());
 
         synchronized (db) {
             stmt.setInSqlda(null);
             stmt.setOutSqlda(null);
 
             stmt.setOutSqlda(native_isc_dsql_prepare(tr_handlestmt_handle,
                     getZeroTerminatedArray(statement), dialect));
 
             getStatementType(stmt);
             
             return stmt_handle.getOutSqlda();
         }
     }

    
Find out the type of the specified statement.

Parameters:
stmt instance of isc_stmt_handle_impl.
Throws:
org.firebirdsql.gds.GDSException if error occured.
 
     private void getStatementType(isc_stmt_handle_impl stmtthrows GDSException {
         final byte [] REQUEST = new byte [] {
             .,
             . };
 
         int bufferSize = 1024;
         byte[] buffer;
         
         buffer = iscDsqlSqlInfo(stmtREQUESTbufferSize); 
 
         /*
         if (buffer[0] == ISCConstants.isc_info_end){
             throw new GDSException("Statement info could not be retrieved");
         }
         */
 
         int dataLength = -1; 
         for (int i = 0; i < buffer.lengthi++){
             switch(buffer[i]){
                 case .:
                     dataLength = iscVaxInteger(buffer, ++i, 2);
                     i += 2;
                     stmt.setStatementType(iscVaxInteger(bufferidataLength));
                     i += dataLength;
                     break;
                 case .:
                 case 0:
                     break;
                 default:
                     throw new GDSException("Unknown data block [" 
                             + buffer[i] + "]");
             }
         }
     }
 
     // isc_dsql_free_statement
     // ---------------------------------------------------------------------------------------------
     public XSQLDA iscDsqlPrepare(IscTrHandle tr_handle,
             IscStmtHandle stmt_handleString statementint dialect)
             throws GDSException {
         return iscDsqlPrepare(tr_handlestmt_handlestatement"NONE",
                 dialect);
     }
 
     public XSQLDA iscDsqlPrepare(IscTrHandle tr_handle,
             IscStmtHandle stmt_handleString statementString encoding,
             int dialectthrows GDSException {
         try {
             return iscDsqlPrepare(tr_handlestmt_handle,
                     getByteArrayForString(statementencoding), dialect);
         } catch (UnsupportedEncodingException e) {
             throw new GDSException("Unsupported encoding. " + e.getMessage());
         }
     }
 
     // isc_dsql_free_statement
     // ---------------------------------------------------------------------------------------------
     public void iscDsqlSetCursorName(IscStmtHandle stmt_handle,
             String cursor_nameint typethrows GDSException {
 
         isc_stmt_handle_impl stmt = validateHandle(stmt_handle);
         isc_db_handle_impl db = validateHandle(stmt.getRsr_rdb());
 
         synchronized (db) {
             native_isc_dsql_set_cursor_name(stmt_handlecursor_nametype);
         }
     }
 
     // isc_dsql_sql_info
     // ---------------------------------------------------------------------------------------------
     public byte[] iscDsqlSqlInfo(IscStmtHandle stmt_handlebyte[] items,
             int buffer_lengththrows GDSException {
         final isc_stmt_handle_impl stmt = validateHandle(stmt_handle);
         final isc_db_handle_impl db = validateHandle(stmt.getRsr_rdb());
 
         synchronized (db) {
             return native_isc_dsql_sql_info(stmt_handleitemsbuffer_length);
         }
     }
 
     // isc_expand_dpb
     // ---------------------------------------------------------------------------------------------
     public byte[] iscExpandDpb(byte[] dpbint dpb_lengthint param,
             Object[] paramsthrows GDSException {
         return dpb;
     }
 
     // isc_get_segment
     // ---------------------------------------------------------------------------------------------
     public byte[] iscGetSegment(IscBlobHandle blobint maxread)
             throws GDSException {
         final isc_blob_handle_impl blb = validateHandle(blob);
         final isc_db_handle_impl db = validateHandle(blb.getDb());
 
         synchronized (db) {
             return native_isc_get_segment(blobmaxread);
         }
     }
 
     // isc_open_blob2
     // ---------------------------------------------------------------------------------------------
     public void iscOpenBlob2(IscDbHandle db_handleIscTrHandle tr_handle,
             IscBlobHandle blob_handleBlobParameterBuffer blobParameterBuffer)
             throws GDSException {
         isc_db_handle_impl db = validateHandle(db_handle);
         isc_tr_handle_impl tr = validateHandle(tr_handle);
         isc_blob_handle_impl blob = validateHandle(blob_handle);
 
         final byte[] bpb = blobParameterBuffer == null ? null
                 : ((BlobParameterBufferImpblobParameterBuffer)
                         .getBytesForNativeCode();
 
         synchronized (db) {
             native_isc_open_blob2(db_handletr_handleblob_handlebpb);
 
             blob.setDb(db);
             blob.setTr(tr);
             tr.addBlob(blob);
         }
     }
 
     // isc_prepare_transaction
     // ---------------------------------------------------------------------------------------------
     public void iscPrepareTransaction(IscTrHandle tr_handle)
             throws GDSException {
         isc_tr_handle_impl tr = validateHandle(tr_handle);
         isc_db_handle_impl db = validateHandle(tr.getDbHandle());
 
         synchronized (db) {
             if (tr.getState() != .) {
                 throw new GDSException(.);
             }
 
             native_isc_prepare_transaction(tr_handle);
 
             tr.setState(.);
         }
     }
 
     // isc_prepare_transaction2
     // ---------------------------------------------------------------------------------------------
     public void iscPrepareTransaction2(IscTrHandle tr_handlebyte[] bytes)
             throws GDSException {
         isc_tr_handle_impl tr = validateHandle(tr_handle);
         isc_db_handle_impl db = validateHandle(tr.getDbHandle());
 
         synchronized (db) {
             if (tr.getState() != .) {
                 throw new GDSException(.);
             }
 
             native_isc_prepare_transaction2(tr_handlebytes);
 
             tr.setState(.);
         }
     }
 
     // isc_put_segment
     // ---------------------------------------------------------------------------------------------
     public void iscPutSegment(IscBlobHandle blob_handlebyte[] buffer)
             throws GDSException {
         final isc_blob_handle_impl blob = validateHandle(blob_handle);
         final isc_db_handle_impl db = validateHandle(blob.getDb());
 
         synchronized (db) {
             native_isc_put_segment(blob_handlebuffer);
         }
     }
 
     /*
      * (non-Javadoc)
      * 
      * @see org.firebirdsql.gds.GDS#isc_reconnect_transaction(org.firebirdsql.gds.isc_tr_handle,
      *      org.firebirdsql.gds.isc_db_handle, byte[])
      */
     public void iscReconnectTransaction(IscTrHandle tr_handle,
             IscDbHandle db_handlelong transactionIdthrows GDSException {
         validateHandle(db_handle);
         isc_tr_handle_impl tr = validateHandle(tr_handle);
         
         byte[] buffer = new byte[4];
         for (int i = 0; i < 4; i++){
             buffer[i] = (byte)(transactionId >>> (i * 8));
         }
 
         synchronized (db_handle) {
             tr.setState(.);
 
             native_isc_reconnect_transaction(db_handletr_handlebuffer);
             tr.setDbHandle((isc_db_handle_impldb_handle);
 
             tr.setState(.);
         }
     }
 
     // isc_rollback_retaining
     // ---------------------------------------------------------------------------------------------
     public void iscRollbackRetaining(IscTrHandle tr_handlethrows GDSException {
         isc_tr_handle_impl tr = validateHandle(tr_handle);
         isc_db_handle_impl db = validateHandle(tr.getDbHandle());
 
         synchronized (db) {
             if (tr.getState() != .
                     && tr.getState() != .) {
                 throw new GDSException(.);
             }
 
             native_isc_rollback_retaining(tr_handle);
 
             tr.setState(.);
         }
     }
 
     // isc_rollback_transaction
     // ---------------------------------------------------------------------------------------------
     public void iscRollbackTransaction(IscTrHandle tr_handle)
             throws GDSException {
         isc_tr_handle_impl tr = validateHandle(tr_handle);
         isc_db_handle_impl db = validateHandle(tr.getDbHandle());
 
         synchronized (db) {
             if (tr.getState() == .) {
                 throw new GDSException(.);
             }
 
 
             native_isc_rollback_transaction(tr_handle);
 
             tr.setState(.);
             tr.unsetDbHandle();
         }
     }
 
 
     public byte [] iscTransactionInformation(IscTrHandle trHandle
             byte [] requestBufferint bufferLenthrows GDSException {
         final IscDbHandle db = validateHandle(trHandle.getDbHandle());
 
         synchronized (db) {
             return native_isc_transaction_info(trHandlerequestBufferbufferLen);
         }
     }
 
     public void iscSeekBlob(IscBlobHandle handleint positionint mode)
             throws GDSException {
         isc_blob_handle_impl blob = validateHandle(handle);
         final isc_db_handle_impl db = validateHandle(blob.getDb());
 
         synchronized (db) {
             native_isc_seek_blob(blobpositionmode);
         }
     }
 
     // Services API
 
     public void iscServiceAttach(String serviceIscSvcHandle serviceHandle,
             ServiceParameterBuffer serviceParameterBufferthrows GDSException {
 
         if (serviceHandle == null) {
             throw new GDSException(.);
         }
         final ServiceParameterBufferImp serviceParameterBufferImp = (ServiceParameterBufferImpserviceParameterBuffer;
         final byte[] serviceParameterBufferBytes = serviceParameterBufferImp == null ? null
                 : serviceParameterBufferImp.toByteArray();
 
         synchronized (serviceHandle) {
             if (serviceHandle.isValid())
                 throw new GDSException("serviceHandle is already attached.");
 
             native_isc_service_attach(serviceserviceHandle,
                     serviceParameterBufferBytes);
         }
     }
 
     public void iscServiceDetach(IscSvcHandle serviceHandle)
             throws GDSException {
         validateHandle(serviceHandle);
         synchronized (serviceHandle) {
             native_isc_service_detach(serviceHandle);
         }
     }
 
     public void iscServiceQuery(IscSvcHandle serviceHandle,
             ServiceParameterBuffer serviceParameterBuffer,
             ServiceRequestBuffer serviceRequestBufferbyte[] resultBuffer)
             throws GDSException {
         validateHandle(serviceHandle);
         final ServiceParameterBufferImp serviceParameterBufferImp = (ServiceParameterBufferImpserviceParameterBuffer;
         final byte[] serviceParameterBufferBytes = serviceParameterBufferImp == null ? null
                 : serviceParameterBufferImp.toByteArray();
 
         final ServiceRequestBufferImp serviceRequestBufferImp = (ServiceRequestBufferImpserviceRequestBuffer;
         final byte[] serviceRequestBufferBytes = serviceRequestBufferImp == null ? null
                 : serviceRequestBufferImp.toByteArray();
 
         synchronized (serviceHandle) {
             native_isc_service_query(serviceHandle,
                     serviceParameterBufferBytesserviceRequestBufferBytes,
                     resultBuffer);
         }
     }
 
     public void iscServiceStart(IscSvcHandle serviceHandle,
             ServiceRequestBuffer serviceRequestBufferthrows GDSException {
         validateHandle(serviceHandle);
         final ServiceRequestBufferImp serviceRequestBufferImp = (ServiceRequestBufferImpserviceRequestBuffer;
         final byte[] serviceRequestBufferBytes = serviceRequestBufferImp == null ? null
                 : serviceRequestBufferImp.toByteArray();
 
         synchronized (serviceHandle) {
             native_isc_service_start(serviceHandleserviceRequestBufferBytes);
         }
     }
 
     // isc_start_transaction
     // ---------------------------------------------------------------------------------------------
     public void iscStartTransaction(IscTrHandle tr_handle,
             IscDbHandle db_handleTransactionParameterBuffer tpb)
             throws GDSException {
         isc_tr_handle_impl tr = validateHandle(tr_handle);
         isc_db_handle_impl db = validateHandle(db_handle);
 
         synchronized (db) {
             if (tr.getState() != .)
                 throw new GDSException(.);
 
             tr.setState(.);
 
             // final byte[] arg = new byte[tpb.length + 1];
             // arg[0] = 3;
             // System.arraycopy(tpb, 0, arg, 1, tpb.length);
 
             byte[] arg = tpbImpl.getBytesForNativeCode();
             native_isc_start_transaction(tr_handledb_handlearg);
 
             tr.setDbHandle((isc_db_handle_impldb_handle);
 
             tr.setState(.);
         }
     }
 
     // isc_vax_integer
     // ---------------------------------------------------------------------------------------------
     public int iscVaxInteger(byte[] bufferint posint length) {
         int value;
         int shift;
 
         value = shift = 0;
 
         int i = pos;
         while (--length >= 0) {
             value += (buffer[i++] & 0xff) << shift;
             shift += 8;
         }
         return value;
     }
     
     public int iscVaxInteger2(byte[] bufferint pos) {
         return (buffer[pos] & 0xff) | ((buffer[pos + 1] & 0xff) << 8);
     }
     
     public abstract void native_isc_attach_database(byte[] file_name,
             IscDbHandle db_handlebyte[] dpbBytes);
 
     public abstract byte[] native_isc_blob_info(isc_blob_handle_impl handle,
             byte[] itemsint buffer_lengththrows GDSException;
 
     public abstract void native_isc_close_blob(IscBlobHandle blob)
             throws GDSException;
 
     public abstract void native_isc_commit_retaining(IscTrHandle tr_handle)
             throws GDSException;
 
     public abstract void native_isc_commit_transaction(IscTrHandle tr_handle)
             throws GDSException;
 
     public abstract void native_isc_create_blob2(IscDbHandle db,
             IscTrHandle trIscBlobHandle blobbyte[] dpbBytes);
 
     public abstract void native_isc_create_database(byte[] file_name,
             IscDbHandle db_handlebyte[] dpbBytes);
 
     public abstract void native_isc_database_info(IscDbHandle db_handle,
             int item_lengthbyte[] itemsint buffer_lengthbyte[] buffer)
             throws GDSException;
 
     public abstract void native_isc_detach_database(IscDbHandle db_handle)
             throws GDSException;
 
     public abstract void native_isc_drop_database(IscDbHandle db_handle)
             throws GDSException;
 
     public abstract void native_isc_dsql_alloc_statement2(
             IscDbHandle db_handleIscStmtHandle stmt_handle)
             throws GDSException;
 
     public abstract void native_isc_dsql_allocate_statement(
             IscDbHandle db_handleIscStmtHandle stmt_handle)
             throws GDSException;
 
     public abstract XSQLDA native_isc_dsql_describe(IscStmtHandle stmt_handle,
             int da_versionthrows GDSException;
 
    public abstract XSQLDA native_isc_dsql_describe_bind(
            IscStmtHandle stmt_handleint da_versionthrows GDSException;
    public abstract void native_isc_dsql_exec_immed2(IscDbHandle db_handle,
            IscTrHandle tr_handlebyte[] statementint dialect,
            XSQLDA in_xsqldaXSQLDA out_xsqldathrows GDSException;
    public abstract void native_isc_dsql_execute2(IscTrHandle tr_handle,
            IscStmtHandle stmt_handleint da_versionXSQLDA in_xsqlda,
            XSQLDA out_xsqldathrows