Friday, March 19, 2010

SQLException: Protocol violation - A possible solution

Sometime ago at work I encountered an exception while inserting a row in a table.
We kept seeing following exception
when we were trying to insert a new row in a table using jdbc (using ojdbc14.jar and oracle 9 as database). DBA tried creating the table
again, she could run the query well in sqlplus, golden.

java.sql.SQLException: Protocol violation at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) at
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java: 146) at
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java: 208) at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:764) at
oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216)
oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:955)
oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1168)
oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3285)
oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3390)
com.myCompany.myPackage.MyDao.insert(MyDAO.java:122)

The table in which we were trying to do an insert looked as followed

FIELD | TYPE | CONSTRAINT | ADDITIONAL CONSTRAINT |
PERSON_ID | NUMBER| PRIMARY KEY |SEQUENCE GENERATED ID |
CREATED_ON | DATE | NOT NULL |
STATUS| VARCHAR (1)| NOT NULL | THE VALUES ARE EITHER 'A' OR 'I'|
LAST_NAME| VARCHAR (100)| NOT NULL | |

The query was something like following

INSERT INTO PERSON (LAST_NAME,STATUS) VALUES (?,'A');

When we replaced query following query, it worked smooth.

INSERT INTO PERSON (NAME,STATUS) VALUES (?,?);

Apparently we cannot insert into table when there is an static value in
the statement using jdbc. This solution was not found in any google search at that time,
may be this blog post will help anybody who encounters it :)

Note: The queries and table in this blog are changed so as to protect the intellectual property.

No comments:

Post a Comment