Skip to Main Content
  • Questions
  • Oracle user unable to access network locations or permission issues?

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Dhaval.

Asked: August 31, 2017 - 5:46 am UTC

Last updated: March 11, 2022 - 3:52 pm UTC

Version: Oracle Database 12c Standard Edition Release 12.1.0.2.0 - 64bit Production

Viewed 10K+ times! This question is

You Asked

Hi TOM,

I am working on writing a package that has a requirement to create hyperlinks for photos if they exists on the server but have encountered an issue. Unsure if its the oracle user unable to access network locations (i.e. files shared on a different server other than Oracle DB Server) or permission issue. Here as an example that shows what i have done and the tests i have carried out.

I have created a directory object called PHOTOS_DIR that points to a share on my network '\\FileServer\Photos\' using the statement below:

CREATE OR REPLACE DIRECTORY PHOTOS_DIR AS '\\FileServer\Photos\';


I have also created a function FileExistsTest which accepts two parameters - Directory Object Name and File Name and checks if the specified file exists in the path specified in the directory object. Here is the code for the function:

CREATE OR REPLACE FUNCTION FileExistsTest(
  p_DirName in VARCHAR2,     -- schema object name
  p_FileName in VARCHAR2
) RETURN NUMBER
IS
 l_file_loc BFILE;
BEGIN
 l_file_loc := BFILENAME(UPPER(p_DirName), p_FileName);
 RETURN DBMS_LOB.FILEEXISTS(l_file_loc);  -- 1 exists; 0 - not exists
END;


Now, when i run the following query, i am expecting 1 if files exists or 0 if it does not exists but instead i get an error:
SELECT FileExistsTest('PHOTOS_DIR','P1234.jpg') FROM DUAL;
       *
ERROR at line 1:
ORA-22288: file or LOB operation FILEEXISTs failed
The device does not recognize the command.
ORA-06512: at "SYS.DBMS_LOG", line 786
ORA-06512: "GPROD.FILEEXISTSTEST", line 9


However, when i change the path for my directory object to C:\Temp\Photos using the SQL statement below it executes fine and i get a return value of 1.
CREATE OR REPLACE DIRECTORY PHOTOS_DIR AS 'C:\Temp\Photos';

SQL> SELECT FileExistsTest('PHOTOS_DIR','P1234.jpg') FROM DUAL;
FILEEXISTSTEST('PHOTOS_DIR ','P1234.JPG')
-----------------------------------------
                                        1


I have also gone a step ahead an mapped '\\FileServer\Photos\' on the database server on G: Drive and changed the path for my directory object to G: and then run the SQL statement which also executes fine which means it now can access the file system but cannot get its content which leads us to the second point: OracleHome cannot access those files due to permission issue?


What does you think could be the problem here and how can i resolve it. Do you think Oracle cannot access network locations. Also the OracleHome user is created as a local user on the Database Server and cannot be shared with other machines (i was thinking of giving OracleHome user full access to '\\FileServer\Photos').

I would really appreciate your insight into this and how can i over come my problem.

Kind Regards,
Dhaval Kumar

and Chris said...

MOS note 739772.1 looks like it relates to your issue:

GOAL

The purpose of this document is to provide the customers with necessary configuration to create database directory on a remote server . For exmaple:
CREATE OR REPLACE DIRECTORY 'SUBSEADATA_1' AS '<Remote_serverDriveOrNetworkUNC>;

Note: This is not a supported action as per Oracle Documentation :
The operating system directory path named in the Oracle DIRECTORY object should not contain any symbolic links in its components.
Reference: "Rules for Using Directory Objects and BFILEs"
http://docs.oracle.com/database/121/ADLOB/adlob_managing.htm#ADLOB45157
However, if it was proven that same error occurred not only in the UNC Env, but also in the Env. with using local hard disk, We Oracle will support the issue.

SOLUTION

It is recommended to use the Domain Administrator account to ensure that passwords are the same across the various machines. If there is no domain, use the local Administrator account andvensure that the passwords are the same for this account across all of the machines.

1. On the machine where you wish to write the files to, create a shared drive granting the user
'Administrator' FULL Control.
Note: For reference below, this Administrator will use the password 'test', this will be referred
to as the "Destination" machine.

2. On the machine with the DB, verify the Administrator user has the same password of the user that shared the drive on the destination machine. In the example here, the password would be 'test'.

3. Map a network drive on the DB machine to the shared drive on the destination machine. When mapping this drive, use the Administrator user with the password 'test'.

4. On the DB machine, BOTH the OracleTNSListener Service and the OracleService<SID> services must be configured to start using the Administrator/test account.
(As discussed above, Oracle uses the Local System account by default.)
a) Go to the Control Panel and then open up the Services panel.
b) Double click on the appropriate service
(TNSListener or OracleService<SID>).
c) Change the "Log on as" user from the "Local System Account" to
"This Account".
d) Specify the service to log on as the Administrator user.
e) Click on "OK".

5. Shutdown the database and stop and start the services on the DB machine. Restart the database.

6. You should now be able to create directory that references the local map or UNC location and access it.

Special Windows 2003 Update :

As Windows 2003 has a changed access behavior compared to Windows 2000, the solution is a little restrict :

Don't use local drive letters for mapping network shares. Workaround is to use UNC locations directly, e.g. backup to \\B\share Since this is in fact an absolute location this is always the
same for any node in the network. So whether accessed from node A or node B, \\B\share is always the shared location on B.

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

External Tables

John Keymer, August 31, 2017 - 11:07 am UTC

External tables might be your friend. Saves you having to do a load of logic - just use a script to "ls" your directory, then selecting from the external table gives you a list of all the files in there.
Chris Saxon
August 31, 2017 - 12:10 pm UTC

Good point. You could even make the script a preprocessor for the table. Then querying it will give you the current directory contents:

http://www.oracle.com/technetwork/issue-archive/2011/11-mar/o21nanda-312277.html

Dhaval Kumar, September 07, 2017 - 5:47 am UTC

Hi Chris,

I really like the idea from John about Pre-processing External Tables. Although there are different ways to tackle this problem, the use of external table is quick and simple. Thus, I have taken this approach and is working beautifully.

Solution:
-- Create Directory Object
CREATE OR REPLACE DIRECTORY PHOTOS_DIR AS 'C:\PhotosExternalTblDir';

-- Create External Table
CREATE TABLE POLEPHOTOS (FILENAME VARCHAR2(80))
ORGANIZATION EXTERNAL
(
TYPE ORACLE_LOADER
DEFAULT DIRECTORY PHOTOS_DIR
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ","
)
LOCATION ('ListOfImages.txt')
)
/

-- Function to check if file exists in the external table
CREATE OR REPLACE FUNCTION FileExistsTest(
p_FileName in VARCHAR2
) RETURN NUMBER
IS
v_SQL VARCHAR2(200);
v_Result NUMBER(2);
BEGIN
v_SQL := 'SELECT COUNT(*) FROM POLEPHOTOS WHERE FILENAME = :P1';
EXECUTE IMMEDIATE v_SQL INTO v_Result USING p_FileName;

IF v_Result > 0 THEN
v_Result := 1;
RETURN v_Result;
ELSE
RETURN v_Result; -- 1 exists; 0 - not exists
END IF;
END;

I updated my batch process to do create/populate 'ListOfImages.txt' file using the following command:
dir /on /b "\\FileServer\Photos\*.jpg" > "C:\PhotosExternalTblDir\ListOfImages.txt"

Now when i run the same SQL Statement i get a return value but not error:
SQL> SELECT FileExistsTest('PHOTOS_DIR','P1234.jpg') FROM DUAL;
FILEEXISTSTEST('PHOTOS_DIR ','P1234.JPG')
-----------------------------------------
1

I hope the above helps someone someday.

Thanks to you and Chris for your help.

Kind Regards,
Dhaval
Connor McDonald
September 09, 2017 - 12:47 am UTC

nice work

UNC vs SymLink Discrepancy

Damian Indigo, March 10, 2022 - 3:03 pm UTC

UNC paths are not symbolic links; why is Oracle flagging a standard UNC paths. As Chris noted in his last paragraph, UNCs are absolute references and should be allowed/supported.
Chris Saxon
March 11, 2022 - 3:52 pm UTC

I'm not sure what you're referring to

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library