Forum Stats

  • 3,741,183 Users
  • 2,248,384 Discussions
  • 7,861,666 Comments

Discussions

Changing TNS_ADMIN to new value to point sqlnet.ora does not work from OCI Client

2880588
2880588 Member Posts: 1
edited Jul 3, 2017 1:51AM in Oracle Call Interface (OCI)

Hi

I have OCI application which is multi threaded application.

I set TNS_ADMIN to some location let say RIGHTDIR( Programmaticly to point sqlnet.ora file using wputenv ) everything works fine.

Now I set TNS_ADMIN to some another location let say WRONGDIR( Programmaticly to point new sqlnet.ora file using wputenv) , It still works fine. Ideally It should have failed.

It still takes contents from old sqlnet.ora file i.e from RIGHTDIR(sqlnet.ora file) not from WRONGDIR (sqlnet.ora) file.

My program is like below

#include <string>

#include <iostream>

#include "oci.h"

using namespace std;

struct ConnectionHandles {

ConnectionHandles()

: environmentHandle_(NULL), errorHandle_(NULL),

serviceContextHandle_(NULL), serverHandle_(NULL)

, userSessionHandle_(NULL)

{}

OCIEnv* environmentHandle_;

OCIError* errorHandle_;

OCISvcCtx* serviceContextHandle_;

OCIServer* serverHandle_;

OCISession *userSessionHandle_;

};

inline void checkError(sword status, int line , std::string function="")

{

if(status == -1)

{

std::string out;

out =  std::string("ERROR returned from function ") + function;

char buffer[4096]={0,};

sprintf (buffer," %d\n",line);

out += buffer;

throw out;

}

}

void testssl()

{

ConnectionHandles connectionHandles_;

ub4 mode = OCI_THREADED ;

sword status = OCI_ERROR;

status = OCIEnvNlsCreate((OCIEnv **)&connectionHandles_.environmentHandle_, mode,

(dvoid *)0, (dvoid * (*)(dvoid *, size_t))0,

(dvoid * (*)(dvoid *, dvoid *, size_t))0,

(void (*)(dvoid *, dvoid *))0,

(size_t)0, (dvoid **)0, OCI_UTF16ID, OCI_UTF16ID);

checkError(status,__LINE__,"OCIEnvNlsCreate");

status = OCIHandleAlloc((dvoid*)connectionHandles_.environmentHandle_, (dvoid**)&connectionHandles_.serverHandle_,

OCI_HTYPE_SERVER, (size_t)0, (dvoid**)0);

checkError(status,__LINE__,"OCIHandleAlloc");

status = OCIHandleAlloc((dvoid*)connectionHandles_.environmentHandle_, (dvoid**)&connectionHandles_.errorHandle_,

OCI_HTYPE_ERROR, (size_t)0, (dvoid**)0);

checkError(status,__LINE__,"OCIHandleAlloc");

wchar_t wDSN[4096] =L"(DESCRIPTION =(ADDRESS = (PROTOCOL = TCPS)(HOST = den00abq.us.oracle.com)(PORT = 2484))(CONNECT_DATA =(SERVICE_NAME = orcl.us.oracle.com)))";

wchar_t wUserName[4096]=L"test12";

wchar_t wPassword[4096]=L"test12";

size_t  len = wcslen(wDSN) ;

status = OCIServerAttach(connectionHandles_.serverHandle_,

connectionHandles_.errorHandle_,

(OraText *)wDSN,

static_cast<sb4>(len) *2 ,OCI_DEFAULT);

checkError(status,__LINE__,"OCIServerAttach");

std::cout << "Connected" << std::endl;

status = OCIHandleAlloc((dvoid*)connectionHandles_.environmentHandle_, (dvoid**)&connectionHandles_.serviceContextHandle_,

OCI_HTYPE_SVCCTX, (size_t)0, (dvoid**)0);

checkError(status,__LINE__,"OCIHandleAlloc");

status = OCIAttrSet (connectionHandles_.serviceContextHandle_, OCI_HTYPE_SVCCTX,

connectionHandles_.serverHandle_, (ub4) 0, OCI_ATTR_SERVER, connectionHandles_.errorHandle_);

checkError(status,__LINE__,"OCIAttrSet");

status = OCIHandleAlloc (connectionHandles_.environmentHandle_ , (dvoid **)&connectionHandles_.userSessionHandle_,

OCI_HTYPE_SESSION, 0, (dvoid **) 0);

checkError(status,__LINE__,"OCIHandleAlloc");

status = OCIAttrSet ((dvoid *)connectionHandles_.userSessionHandle_,

OCI_HTYPE_SESSION,

(OraText*) wUserName,

static_cast<ub4>(wcslen(wUserName)) * 2,

OCI_ATTR_USERNAME,

connectionHandles_.errorHandle_);

checkError(status,__LINE__,"OCIAttrSet");

status = OCIAttrSet ((dvoid *)connectionHandles_.userSessionHandle_, OCI_HTYPE_SESSION,

(OraText*)wPassword,

static_cast<ub4>(wcslen(wPassword)) * 2,

OCI_ATTR_PASSWORD,

connectionHandles_.errorHandle_);

checkError(status,__LINE__,"OCIAttrSet");

status = OCISessionBegin (connectionHandles_.serviceContextHandle_, connectionHandles_.errorHandle_, connectionHandles_.userSessionHandle_,

OCI_CRED_RDBMS, OCI_DEFAULT);

checkError(status,__LINE__,"OCISessionBegin");

status = OCIAttrSet (  connectionHandles_.serviceContextHandle_, OCI_HTYPE_SVCCTX,

connectionHandles_.userSessionHandle_, (ub4) 0, OCI_ATTR_SESSION, connectionHandles_.errorHandle_ );

checkError(status,__LINE__,"OCIAttrSet");

}

std::wstring getRightDir()

{

std::wstring env = L"TNS_ADMIN=";

std::wstring rightDir= env+getCurrentDirectory()+L"\\RIGHT";

std::wcout << "RightDir Value = "  << rightDir << std::endl;

return rightDir;

}

std::wstring getWrongDir()

{

std::wstring env = L"TNS_ADMIN=";

std::wstring wrongDir= env+getCurrentDirectory()+L"\\WRONG";

std::wcout << "Wrong Value = "  << wrongDir << std::endl;

return wrongDir;

}

int _tmain(int argc, _TCHAR* argv[])

{

cout << "OCI Test Start" << endl;

try

{

::_wputenv(getRightDir().c_str());

testssl();

::_wputenv(getWrongDir().c_str());

testssl();

}

catch(const std::string  &str)

{

std::cout << str << std::endl;

}

getchar();

return 0;

}

#include <string>#include <iostream>#include "oci.h"
using namespace std;

struct ConnectionHandles { ConnectionHandles() : environmentHandle_(NULL), errorHandle_(NULL), serviceContextHandle_(NULL), serverHandle_(NULL) , userSessionHandle_(NULL) {}
OCIEnv* environmentHandle_; OCIError* errorHandle_; OCISvcCtx* serviceContextHandle_; OCIServer* serverHandle_; OCISession *userSessionHandle_;
};




inline void checkError(sword status, int line , std::string function=""){ if(status == -1) { std::string out; out =  std::string("ERROR returned from function ") + function; char buffer[4096]={0,}; sprintf (buffer," %d\n",line); out += buffer; throw out; }}

void testssl(){
ConnectionHandles connectionHandles_; ub4 mode = OCI_THREADED ; sword status = OCI_ERROR; status = OCIEnvNlsCreate((OCIEnv **)&connectionHandles_.environmentHandle_, mode, (dvoid *)0, (dvoid * (*)(dvoid *, size_t))0, (dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *))0, (size_t)0, (dvoid **)0, OCI_UTF16ID, OCI_UTF16ID);

checkError(status,__LINE__,"OCIEnvNlsCreate");


status = OCIHandleAlloc((dvoid*)connectionHandles_.environmentHandle_, (dvoid**)&connectionHandles_.serverHandle_, OCI_HTYPE_SERVER, (size_t)0, (dvoid**)0);
checkError(status,__LINE__,"OCIHandleAlloc");
status = OCIHandleAlloc((dvoid*)connectionHandles_.environmentHandle_, (dvoid**)&connectionHandles_.errorHandle_, OCI_HTYPE_ERROR, (size_t)0, (dvoid**)0);
checkError(status,__LINE__,"OCIHandleAlloc");
wchar_t wDSN[4096] =L"(DESCRIPTION =(ADDRESS = (PROTOCOL = TCPS)(HOST = den00abq.us.oracle.com)(PORT = 2484))(CONNECT_DATA =(SERVICE_NAME = orcl.us.oracle.com)))"; wchar_t wUserName[4096]=L"test12"; wchar_t wPassword[4096]=L"test12"; size_t  len = wcslen(wDSN) ;

status = OCIServerAttach(connectionHandles_.serverHandle_, connectionHandles_.errorHandle_, (OraText *)wDSN, static_cast<sb4>(len) *2 ,OCI_DEFAULT);

checkError(status,__LINE__,"OCIServerAttach");
std::cout << "Connected" << std::endl;

status = OCIHandleAlloc((dvoid*)connectionHandles_.environmentHandle_, (dvoid**)&connectionHandles_.serviceContextHandle_, OCI_HTYPE_SVCCTX, (size_t)0, (dvoid**)0);
checkError(status,__LINE__,"OCIHandleAlloc");

status = OCIAttrSet (connectionHandles_.serviceContextHandle_, OCI_HTYPE_SVCCTX, connectionHandles_.serverHandle_, (ub4) 0, OCI_ATTR_SERVER, connectionHandles_.errorHandle_);
checkError(status,__LINE__,"OCIAttrSet");

status = OCIHandleAlloc (connectionHandles_.environmentHandle_ , (dvoid **)&connectionHandles_.userSessionHandle_, OCI_HTYPE_SESSION, 0, (dvoid **) 0);
checkError(status,__LINE__,"OCIHandleAlloc");

status = OCIAttrSet ((dvoid *)connectionHandles_.userSessionHandle_, OCI_HTYPE_SESSION, (OraText*) wUserName, static_cast<ub4>(wcslen(wUserName)) * 2, OCI_ATTR_USERNAME, connectionHandles_.errorHandle_);

checkError(status,__LINE__,"OCIAttrSet");

status = OCIAttrSet ((dvoid *)connectionHandles_.userSessionHandle_, OCI_HTYPE_SESSION, (OraText*)wPassword, static_cast<ub4>(wcslen(wPassword)) * 2, OCI_ATTR_PASSWORD, connectionHandles_.errorHandle_);
checkError(status,__LINE__,"OCIAttrSet");
status = OCISessionBegin (connectionHandles_.serviceContextHandle_, connectionHandles_.errorHandle_, connectionHandles_.userSessionHandle_, OCI_CRED_RDBMS, OCI_DEFAULT);
checkError(status,__LINE__,"OCISessionBegin");
status = OCIAttrSet (  connectionHandles_.serviceContextHandle_, OCI_HTYPE_SVCCTX, connectionHandles_.userSessionHandle_, (ub4) 0, OCI_ATTR_SESSION, connectionHandles_.errorHandle_ );
checkError(status,__LINE__,"OCIAttrSet");

// running some queries here
}
std::wstring getCurrentDirectory(){
wchar_t  Buffer[4096]={0,}; _wgetcwd(Buffer,4096);
std::wstring str(Buffer); return str;
}

std::wstring getRightDir(){ std::wstring env = L"TNS_ADMIN="; std::wstring rightDir= env+getCurrentDirectory()+L"\\RIGHT"; std::wcout << "RightDir Value = "  << rightDir << std::endl;
return rightDir;
}

std::wstring getWrongDir(){ std::wstring env = L"TNS_ADMIN="; std::wstring wrongDir= env+getCurrentDirectory()+L"\\WRONG"; std::wcout << "Wrong Value = "  << wrongDir << std::endl; return wrongDir;
}

void setCWD(){ std::wstring env(L"CWD=");
::_wputenv((env + getCurrentDirectory()).c_str() );
std::wcout <<"Setting " << env + getCurrentDirectory() << std::endl;}
int _tmain(int argc, _TCHAR* argv[]){ cout << "OCI Test Start" << endl;
    setCWD();
try { ::_wputenv(getRightDir().c_str()); testssl();
::_wputenv(getWrongDir().c_str()); testssl();
}
catch(const std::string  &str) { std::cout << str << std::endl; } getchar(); return 0;}

This discussion has been closed.