Database Lock while updating table in oracle - java

My aplication is performing a very simple update on a table :
UPDATE TABLE SET COLUMN = 'XYZ' WHERE PK = 123
The problem is, when Hibernate tries to update the table like this, the table get locked with
ORA event : SQL*Net more data from client.
I tried to replicate the error on my local database but i couldn't.
Does Anybody know what is happening?
Database's version where the error is happening: Oracle Database 10g Release 10.2.0.3.0 - 64bit Production
My local database's version: Oracle Database 10g Release 10.2.0.5.0 - 64bit Production
PS: The column being updated is a CLOB type and the OJDBC driver version is 1.4

Do you know what exactly is being sent to oracle db? I've seen something similar when Hibernate was sending very long sql command and it failed on JDBC driver.
I would suggest starting from getting latest JDBC driver version.

Prepared statement should be used. We should not directly assign the values.
For example:
String updateSQL = UPDATE TABLE SET COLUMN = ? WHERE PK = ? .
PreparedStatement pstmt = null;
pstmt=dbresourceAgent.getPreparedStatement(updateSQL);
pstmt.setString(1,"XYZ");
pstmt.setString(2,"123");
pstmt.executeUpdate();

Related

Avoid ORA-00904 - invalid identifier error while doing sql query in java as the column may or may not be preset in the database

I am trying to write java code to migrate data from oracle database to other database.
My use case is that different client have different version of code and so the database columns may vary. Clients with later version have additional column.
For eg : Client with new version as COL99 in the table SAMPLE_TABLE.
While writing the migration code, if I try to select the COL99 from SAMPLE_TABLE, it will work fine for the new client. But for clients on old version, the code fails with
ORA-00904 Invalid Identifier error.
Is there a way to handle in sql query or java code such that, if the column doesn't exist in the database table, simply ignore and do not return the value instead of throwing the exception.
You should first check, whether COL99 exists for your current database connection.
For Oracle you can use a query like this:
SELECT
COL.COLUMN_ID,
COL.OWNER AS SCHEMA_NAME,
COL.TABLE_NAME,
COL.COLUMN_NAME
FROM
SYS.ALL_TAB_COLUMNS COL
INNER JOIN
SYS.ALL_TABLES T
ON COL.OWNER = T.OWNER
AND
COL.TABLE_NAME = T.TABLE_NAME
WHERE
COL.OWNER = 'SCHEMA'
AND
COL.TABLE_NAME = 'SAMPLE_TABLE'
AND
COL.COLUMN_NAME = 'COL99'
Then you create your query with or without COL99.

Create table with longvarchar consistently in Hsqldb and Oracle

In my project, we use hsqldb for running unit test cases and oracle in production. Liquibase is used to run queries on environments. I have an issue with creating table with datatype LONGVARCHAR. I am already using this statement to use oracle syntax in hsqldb.
SET DATABASE SQL SYNTAX ORA TRUE
When I try to create table in hsqldb, this query seem to work.
CREATE TABLE A (DATA LONGVARCHAR);
And when I try to create table in oracle, the following works.
CREATE TABLE A (DATA LONG VARCHAR);
How can I write a homogeneous query which can work for both database servers.
Use a CLOB
CREATE TABLE A (DATA CLOB);

After Oracle upgraded from 10g to 11g , "Oracle-ORA-01722 invalid number" appeared

A java web Project
1.OS:Linux
2.Tomcat upgraded from 1.5 to 1.7
3.Java upgraded from 1.5 to 1.7
4.Oracle upgraded from 10g to 11g
There is a talbe named TBL in oracle and a column named COL defined by NUMBER(38,15).
Before upgrading, there was no problem to put a number(for example:0.123456789012345) into the column or retrieve it by "SELECT TO_CHAR(COL) FROM TBL".
But now,when I use "SELECT TO_CHAR(COL) FROM TBL" , OR "SELECT TO_NUMBER(COL) FROM TBL", the error appears("Oracle-ORA-01722 invalid number").
I confirm the value by “SELECT COL FROM TBL” via Oracle SQL Developer,and it looks like very noraml,just a number like 0.123456789012345.But if I use SQL Plus to confirm the value ,it is blank! This is very strange!
Moreover,when I use the value plus one ,no error encountered but the result is not correct (0.123456789012345 + 1 = 1.123456789012305).
Not every value in that column has the same problem but all value looks like a normal number when I select it via Oracle SQL Developer without TO_CHAR() or TO_NUMBER().
Anyone who has the simliar expierence will help a lot.thank you.

DB2 and ResultSetMetaData - Unable to get column name

I am developping an application on WAS 8.0.0.5 that iteracts with a DB2 database.
I am getting the column name using java.sql.ResultSetMetaData call getColumnName() class. On my development WAS everything works great.
ResultSetMetaData rsmd = rs.getMetaData();
String columnName = rsmd.getColumnName(i + 1);
When I try and install on a WAS 8.0.0.6 instead of getting the column name, I get the column index!!!
The driver set for the connection string is com.ibm.db2.jcc.DB2Driver
As I side note, I've confirmed and WAS 8.0.0.5 uses DB2 driver 3.62 (works) and 8.0.0.6 uses 4.12 (doesn't work).
What is wrong?
The behaviour of getColumnName() and getColumnLabel() has changed in the IBM Data Server Driver for JDBC version 4. I believe it now conforms to the JDBC specification. You can use the connection property useJDBC4ColumnNameAndLabelSemantics to modify this behaviour, as explained here: http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.apdv.java.doc/src/tpc/imjcc_c0052593.html.
Thanks for the response.
Unfortunately it wasn't the solution. The behavior was that the column index was being returned, instead of the query's label or column name itself.
The problem was that the db2jcc.jar version, configured on the WAS JDBC resources, was too old (version 3.59) I replaced it for 4.12 and now it works.

How to check, that we are using oracle 8i database in jdbc?

In jdbc, how to check, that we are using oracle 8i database?
Connection connection = DriverManager.getConnection(url);
DatabaseMetaData meta = connection.getMetaData();
String product = meta.getDatabaseProductName();
String major = meta.getDatabaseMajorVersion();
String minor = meta.getDatabaseMinorVersion();
You might have to use the Database metadata class.
Run:
select * from v$version
It should produce something like:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
...
Then it's just a simple matter of parsing that 1st result row.

Categories

Resources