jsedano
jsedano

Reputation: 4216

Store a Blob from Java to BD: Data size bigger than max size for this type when is clearly not

Ok, here we go...

I am trying to call a Stored Procedure (PL/SQL), one of the parameters is a blob, but when I execute the OracleCallableStatement I get this error:

java.sql.SQLException: Data size bigger than max size for this type

That is oh so very frustrating..

I have tried to do the following and fails...

oracleCallableStatement.setBinaryStream(3, new ByteArrayInputStream(someByteArray), someByteArray.length);

I get the same error with:

oracleCallableStatement.setBytes(3, someByteArray);

I also changed the oracle driver, since I read web pages like this:

http://www.coderanch.com/t/457770/JDBC/databases/java-sql-sqlexception-data-size

Says that there's a bug, I updated it to ojdbc5.jar From: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html

But the problem is persistent

The DB is an Oracle 11g JavaEE is 1.5 running in Glassfish 2.1

Well, I guess that's it, my someByteArray is not bigger than 4Gb! it has a length of 38678

EDIT: The exception is firing before the execute.. it fires when I set an attribute for the OracleCallableStatement

Portion of the stack trace:

at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.ttc7.TTCItem.setArrayData(TTCItem.java:103)
at oracle.jdbc.dbaccess.DBDataSetImpl.setBytesBindItem(DBDataSetImpl.java:2484)
at oracle.jdbc.driver.OraclePreparedStatement.setItem(OraclePreparedStatement.java:1224)
at oracle.jdbc.driver.OraclePreparedStatement.setBinaryStream(OraclePreparedStatement.java:2710)

EDIT

So, hello again, going to put this here... tell me if I´m doing wrong and I will delete it...

someByteArray was an array that was get from a someString.toByteArray()

That String was holding a representation of a (EDIT: used to say jpeg) PNG image encoded in Base64...

So, I used a method in sun.misc.BASE64Decoder to get the someByteArray from the someString... then I used

oracleCallableStatement.setBinaryStream(3, new ByteArrayInputStream(someByteArray), someByteArray.length);

And it worked like a charm..

BUUUT... Usage of the classes in sun.misc is not recommended, read this for further information..

work sun.misc.BASE64Encoder/Decoder for getting byte[]

BUT!!!

That doesn't solve the mystery to why I was unable to put the first byte array into the parameters and the mystery of the cryptic exception "java.sql.SQLException: Data size bigger than max size for this type" ...

EDIT:

This error appeared again... this time everything was alright, I was decoding the base64 correctly, but "java.sql.SQLException: Data size bigger than max size for this type" kept returning...

EDIT:

It was the driver, I change it on the server and the app, and I redeployed and everything worked fine...

Upvotes: 1

Views: 4687

Answers (2)

jsedano
jsedano

Reputation: 4216

Turns out it was the driver, I changed to ojdbc5.jar as I stated in my question, and I had to make sure that the web server picked the correct one and not the older one, as pointed by Alex Poole, and problem solved.

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109547

Not the answer

You cannot keep the bytes of an image in a String (correctly). You could first encode the byte[] using Base64 into a String. Maybe though I misunderstand, and you did it correctly. Check that you did log every exception.

There are some other Base64 conversions besides the deprecated sun's version (which is not available in every JDK). In JavaEE:

import javax.xml.bind.DatatypeConverter;
String data = "...";
byte[] bytes = DatatypeConverter.parseBase64Binary(data);
String data = DatatypeConverter.parseBase64Binary(bytes);

Can't you store the data as binary blob, without base64 encoding? It'd at least save space.

An other error could be growing reused ByteArray's or something other trivial bug.

Upvotes: 1

Related Questions