使用OCI_SUCCESS_WITH_INFO在OCILogon之后获取OCI_INVALID_HANDLE

时间:2012-02-08 11:21:35

标签: c++ oracle10g oci ora-28002

我创建了以下个人资料。

create profile EXPTESTPROF limit failed_login_attempts unlimited password_life_time 1/24/60/60 password_reuse_time unlimited password_reuse_max unlimited password_verify_function null password_lock_time 1  password_grace_time 31;

alter user exptest profile EXPTESTPROF;

当我尝试从命令提示符登录时,它会成功登录并显示以下错误消息。

ERROR:
ORA-28002: the password will expire within 30 days

直到我没事。

但是我写了一个小程序用于OCI登录和一些操作。

当我使用上述用户调用OCILogon时,它返回1加上OCI_SUCCESS_WITH_INFO

之后,我尝试使用OCI_HTYPE_SVCCTX抓取OCIAttrGet,然后返回-2,其中添加OCI_INVALID_HANDLE

为什么句柄被破坏了?有没有办法克服这个问题并一直工作到宽限期结束?

请在下面找到示例OCI客户端。

#include "stdio.h"
#include "oci.h"
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#pragma comment(lib, "%ORACLE_HOME%\\oci\\lib\\MSVC\\oci.lib")

void main()
{
  int    errcode   = 0;
  int    rc        = 0;
  sb4    ociErrVal = 0;
  char   *ErrTxt   = NULL;
  ub4    BufLen    = 1024;
  size_t DBNameLen = 0;
  size_t PasswdLen = 0;
  size_t UserLen   = 0;

  OCISvcCtx        *p_svc = NULL;
  OCIEnv           *p_env = NULL;
  OCIError         *p_err = NULL;
  OCIStmt          *p_sql = NULL;
  OCIDefine        *p_dfn = (OCIDefine *) 0;
  OCIBind          *p_bnd = (OCIBind *) 0;

  const unsigned char *UserID   = (const unsigned char*)"exptest";
  const unsigned char *Password = (const unsigned char*)"oracle";
  const unsigned char *DBName   = (const unsigned char*)"ORCL10G";

  if (UserID != NULL)
  {
      UserLen = strlen ((char *)UserID);
  }

  if (Password != NULL)
  {
      PasswdLen  = strlen((char *)Password);
  }

  if (DBName   != NULL)
  {
      DBNameLen = strlen((char *)DBName);
  }

  if (ErrTxt != NULL) {
      BufLen = 0;
      ErrTxt[0] = '\0';          
      ErrTxt[1] = '\0';          
      ErrTxt[2] = '\0';
      ErrTxt[3] = '\0';
   }

  rc = OCIInitialize((ub4) OCI_DEFAULT, (dvoid *)0,  /* Initialize OCI */
          (dvoid * (*)(dvoid *, size_t)) 0,
          (dvoid * (*)(dvoid *, dvoid *, size_t))0,
          (void (*)(dvoid *, dvoid *)) 0 );

  /* Initialize evironment */
  rc = OCIEnvInit( (OCIEnv **) &p_env, OCI_DEFAULT, (size_t) 0, (dvoid **) 0 );

  /* Initialize handles */
  rc = OCIHandleAlloc( (dvoid *) p_env, (dvoid **) &p_err, OCI_HTYPE_ERROR,
          (size_t) 0, (dvoid **) 0);
  rc = OCIHandleAlloc( (dvoid *) p_env, (dvoid **) &p_svc, OCI_HTYPE_SVCCTX,
          (size_t) 0, (dvoid **) 0);


  /* Connect to database server */
  rc = OCILogon(p_env, 
                p_err,
                &p_svc,
                UserID,
                (ub4) UserLen,
                Password,
                (ub4) PasswdLen,
                DBName,
                (ub4) DBNameLen);

  if (rc != 0) {
     OCIErrorGet((dvoid *)p_err,
                 (ub4) 1,
                 (text *) NULL,
                 &errcode,
                 (unsigned char *) ErrTxt,
                 BufLen,
                 OCI_HTYPE_ERROR);
   }

    rc = (long) OCIAttrGet(p_svc,
                         (ub4) OCI_HTYPE_SVCCTX,
                         (dvoid *) &p_svc,
                         (ub4) NULL,
                         (ub4) OCI_ATTR_SERVER,
                         p_err);

  rc = OCILogoff(p_svc, p_err);                           /* Disconnect */
  rc = OCIHandleFree((dvoid *) p_sql, OCI_HTYPE_STMT);    /* Free handles */
  rc = OCIHandleFree((dvoid *) p_svc, OCI_HTYPE_SVCCTX);
  rc = OCIHandleFree((dvoid *) p_err, OCI_HTYPE_ERROR);
}

1 个答案:

答案 0 :(得分:2)

问题出在OCI Api中,并在oracle11g R2中修复 -