Cannot update Oracle view from JDBC - jdbc

Overview: Need to read row from Oracle view and create a Notes document, save document, then write Document Unique ID back to Oracle.
I am able to read connect and read data no problem. I am using a type 4 connection connecting to an Oracle 11 database. The Oracle view is setup to allow Updating. The view has nothing in it that is outline here: In Oracle, is it possible to INSERT or UPDATE a record through a view?
*With the same username and password, you are able to successfully update view by typing in SQL statement.
*Tried using conn.setAutoCommit(false); This had no effect.
*Verified that the result set was updatable (1008)
*User has been given full DBA access (temporarily)
*I have tried every possible combination of the first parameter in the createStatement method
...
Statement statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = statement.executeQuery(fetch);
...
String UNID = doc.getUniversalID(); //gets unique id from saved Notes Document
System.out.println("This is what to write to Oracle:" + UNID);
System.out.println("is updatable=1008, not updatable=1007 value is:" + rs.getConcurrency());
System.out.println("is Result Set Closed:" + rs.isClosed());
rs.updateString("NOTES_DOC_ID", UNID);
System.out.println("got past updating NOTES_DOC_ID column");
rs.updateRow(); //fails here
Here is the error from console:
This is what to write to Oracle:BF8091259610C61B87257B16005C14FB
is updatable=1008, not updatable=1007 value is:1008
is Result Set Closed:false
got past updating NOTES_DOC_ID column
java.security.AccessControlException: Access denied (java.lang.RuntimePermission exitVM.0)
Prior to asking for the user to have DBA access I would get a
java.sql.SQLSyntaxErrorException: ORA-01031: insufficient privileges
I think this a big clue. My DBA doesn't know what further access to give me.
The DBA is wanting me to start using ref cursors, which is fine, but I suspect there is some kind of security setting for JDBC access that is tripping me up, and I want to explore that first. If there is a security issue, then I don't think changing the way I read the rows is going to make a difference. Most of all the documentation on how to do this was obtained from Oracle's website, as well as this site.

I am going to answer my question and explain how I got past this roadblock. In the end, I basically did what 'a_horse_with_no_name' suggested.
Instead of using the resultSet cursor or a ref cursor to perform the update, I was able to use a plain UPDATE statement. This was possible, because I was able to convince the DBA to create a column for a unique identifier. We could never get around the exceptions caused by the updateRow() method of the resultSet. Prior to him adding the unique identifier, there was not a key in which to reliably use the UPDATE statement.
Here is the code where updateSQL is a string holding the update SQL statement:
updateResultInt = updateStatement.executeUpdate(updateSQL);
It returns a 1 if successful.
One word of caution, if you are using a tools like TOra or sql plus to check your update statements, you have to remember to manually commit them. If you don't your java agent will hang when trying to run it. Here is an good reference that helped me with that issue: SQL Update hangs Java program
Thanks to those who commented!

Related

Sequences (using as ID) issue in Oracle SQL Developer

I am using sequences to create IDs, so while executing insert stored procedure it will create unique value for ID. But after some time it is losing the definition for the sequence.
Not sure why this is happening again and again and how to solve the problem?
I am using Oracle SQL Developer and in the edit table property there is 'Identity Column' setting. See below:
Next step is setting up trigger and sequence:
It was working fine for some time until this property defaulted. Now it is not there anymore:
Still have this trigger and sequence object in the schema and able to setup again but it will break later.
How to avoid this problem in future?
I think it is just a bug/limitation in your client software, Oracle SQL Developer. The "Identity Column" tab is a handy way to create the corresponding sequence and trigger but it doesn't seem to recognise existing elements. I've just verified my own system and that's exactly what happens.
It makes sense, because adding a new sequence and trigger is a pretty straightforward task (all you need is a template) but displaying current sequence is hard given that a trigger can implement any conceivable logic. Surely it could be done but the cost-benefit ratio probably left things this way.
In short, your app is not broken so nothing needs to be fixed on your side.
This is what I received from IT support regarding the issue:
A few possibilities that might cause this:
1 - Another user with limited privileges might be editing the table using SQL Developer. In this case, if this user's privilege is not enough to obtain the sequence and/or trigger information from the database, the tool might leave the fields blank and disable it when table changes are saved.
2 - The objects are being changed or removed outside of SQL Developer, causing it to lose the information. In my tests I noticed that dropping the trigger and recreating it with the same name caused the identity property information to be lost on SQL Developer.
Even being the trigger enabled, and working for inserts it could not retrieve the information.
Then, if I run an alter trigger to enable it (even tough dba_trigger is reporting it as already enabled), SQL Developer will list the information again:
ALTER TRIGGER "AWS"."TABLE1_TRG" ENABLE;
So it looks like there are some issues with the SQL Developer, that is causing this behavior.
Next time it happen, please check if the trigger still exist on the database and is enabled with the query below:
select owner, trigger_name, TRIGGER_TYPE, TRIGGERING_EVENT, TABLE_OWNER, TABLE_NAME, STATUS
from dba_triggers
where trigger_name = 'ENTER_YOUR_TRG_NAME'; --Just change the trigger name in WHERE

OBIEE 11g OBIApps RPD Update row count, NQ_SESSION.USER_LANGUAGE_CODE "No value defination"

The repository in OBIEE 11g is a prebuilt RPD from oracle OBIApps. The connection pool is appropriate and is working.
But while updating the row count there are few variables which pop up and as they are default so i press them "OK".
But then there is an error message which is stated as
nQSError23006:The session variable, NQ_SESSION.USER_LANGUAGE_CODE, has no value
Then I tried to disable the session variable by disabling initialization block.
Then also the same error is popping up.
Please guide me with this error.
That means the variable is (re-)used as a reference in - for example - other initialization block SQLs, calculations or generally inside other repository objects and is still being queried. Or at least "attempted to be queried".
Best search across your RPD for references to that variable.
The Variable was being pointed from the Standard Views by oracle where in the the view was written with a where clause.
Where Clause was as follows:
select * from xyz where language_code = 'NQ_SESSION.USER_LANGUAGE_CODE'.

OracleDatareader seems to execute an update statement

I am using oracle client 11.2.0
Dll version 4.112.3.0
We have a page in our application where people can give a sql statement and retreive results. basically do an oracle command.executereader
Recently one of my team members gave an update statement as a test and it actually performed an update on a record!!!!
Anyone who has encountered this?
Regards
Sid.
It is a normal (albeit a bit unsettling) behavior. ExecuteReader is expected to execute the sql command provided as CommandText and build a DbDataReader that you use to loop over the results.
If the command doesn't return any row to read is not something that the reader should prevent in any case. And so it is not expected that it checks if your command is really a SELECT statement.
Think for example if you pass a stored procedure name or if you have multiple sql batch to execute. (INSERT followed by a SELECT)
I think that the biggest problem here is the fact that you allow an arbitrary sql command typed by your users to reach the database engine. A very big hole in security. You should, at least, execute some analysis on the query text before submitting the code to the database engine.
I agree with Steve. Your reader will execute any command, and might get a bit confused if it's not a select and doesn't return a result set.
To prevent people from modifying anything, create a new user, grant select only (no update, no delete, no insert) on your tables to that user (grant select on tablename to seconduser). Then, log in as seconduser, and, create synonyms for your tables (create synonym tablename for realowner.tablename). Have your application use the seconduser when connecting to the DB. This should prevent people from "hacking" your site. If you want to be of the safe side, grant no permissions but create session to the second user to prevent him from creating tables, dropping your views and similar stuff (I'd guess your executereader won't allow DDL, but test it to make sure).

Operation must be an updateable query - VB Script, Paradox Table

I'm using a Win XP box with BDE Administrator and Access 2007 installed. I'm able to open and perform select queries on existing Paradox tables without problem but have some very strange behavior when attempting INSERT/UPDATE. I can even create a new Paradox table and it has the same behavior. Here is sample code:
' create new table
conObj.Execute "CREATE TABLE test (id INT, comment VARCHAR(30))"
' first insert works fine
conObj.Execute "INSERT INTO test VALUES (1, 'something')"
' second insert fails for unknown reason
conObj.Execute "INSERT INTO test VALUES (2, 'something else')"
I've tried using Jet 4.0, MS Access Paradox driver, and native Paradox driver connection strings but all yield the same result. On the second insert statement it throws an error:
Operation must be an updateable query
I've read numerous posts in forums and pages on help sites that tell me this error is caused by a file permissions issue. The account running this script is part of the Administrator group and I've changed file permissions to allow the Everyone group Full Control of the db file but this changes nothing.
This page put out by Micrsoft Support did not fix the problem: http://support.microsoft.com/kb/175168
Additionally, I can create a new table but any time I try to create a PRIMARY KEY or UNIQUE field I get an error message that says:
"Index_[random characters] is not a valid name."
try
"CREATE TABLE test (id INT, comment VARCHAR(30), primary key(id))"
I don't know much about Paradox databases but this has indeed been a learning experience. Even though I have a table file called table.db that's not enough to store more than a single row of data. I also need several other files to insert or update a paradox database:
table.DB
table.PX
table.VAL
table.XG0
table.XG1
table.YG0
table.YG1
I was nosing around in another program that generates paradox databases and found when I copied a blank database from it along with these other files it generated I was able to insert and update without problems. I have no idea what these files are for or why they need to be present to insert or update but having them present fixed my issue.

Oracle ref cursor fetch hangs if it contains 1 single record

I have a weird problem right now that if a ref cursor returned from a stored procedure that has only 1 record in it, the fetch operation will hang and freeze. The stored procedure execution was really fast, just the fetching process hangs. If the ref cursor has more than 1 record, then everything is fine. Does anyone have similar issues before?
The Oracle server is 11g running on Linus. The client is Windows Server 2003. I'm testing this using the generic Oracle sqlplus tool on the Windows Server.
Any help and comments would be greatly appreciated. thanks.
When you say hangs, what do you mean ?
If the session is still active in the database (status in V$SESSION), then it is probably waiting on some event (eg SQL*Net from client means it is waiting for the client to do something).
It may be that the query is taking a long time to find that there aren't any more rows. Consider a table of 10,000,000 rows with no indexes. The query may full scan the table and find the first row matches the criteria. It still has to scan the next 9,999,999 rows to find that they don't. That can take a while.
Since you are saying that the process hangs, Is there a chance that your cursor does a "select for Update" instead of "Select " ? Since you are saying that the fetch of multiple records does not cause this error, that might not be the case.
Can you show us the code (or a reproducible small test/sample) for your select and the fetch.
Also, you can check the v$locked_objects using the following query and giving in your table name(s) to see if the object in question is being locked. Again, unless your current query has "for update" this fetch should not hang.
select do.*
from v$locked_objects vo,
dba_objects do
where vo.object_id = do.object_id
and vo.object_name = '<your_table_name>'

Resources