db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dinesh Bajaj <dinesh.ba...@ymail.com>
Subject Re: 'ERROR 40XD0: Container has been closed.: java.io.IOException'.
Date Mon, 15 Feb 2010 16:47:58 GMT
Hi Donald,

Please find below the simplified version of my program. Basically, what I am trying to achieve
is that store an image in a blob field. Then, retrieve it via a resultset, and then store
it back.

I would like to point out that I couldn't replicate the same error as this time, as I am getting
the new error, whose stack flow I have produced below the code listing. Please let me know
if there is any fundamental flaw in my code logic.

code listing:

package learning;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;

public class TestingBlob {

    private Connection con;
    
    public TestingBlob() {

        try {
            doTest();
        } catch (Exception e) {
            System.err.println("error occurred.");
            e.printStackTrace();
        }
   }

    public static void main(String args[]) {        
        new TestingBlob();
   }

    private void doTest() throws Exception{

          if (! establishDatabaseConnection())  //database connection failed
            System.exit(0);

           String sql = "create table TABLE1 (PHOTO BLOB(512000))";
           con.createStatement().execute(sql);            
                     
            File file =
            new File("E:\\my documents\\icons\\print.jpg");
            
            sql  = "insert into TABLE1 values (?)";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setBinaryStream(1, new FileInputStream(file), file.length());
            ps.executeUpdate();
            ps.close();

           ResultSet rs = con.createStatement().executeQuery(
                    "select * from TABLE1");
           rs.next();
           InputStream is = rs.getBinaryStream(1);
         
           long length = rs.getBlob(1).length();
            rs.close();

            con.setAutoCommit(false);
            ps = con.prepareStatement("update table1 set photo = ?");
            ps.setBinaryStream(1, is, length);
            ps.executeUpdate();
            
            con.commit();
            con.close();
            System.out.println("program executed successfully");

    }
    
   
    private  boolean establishDatabaseConnection() throws Exception{

      String driver = "org.apache.derby.jdbc.EmbeddedDriver";
      String dbName="testdatabase";
      String connectionURL = "jdbc:derby:" + dbName;

     boolean connectionError = false;
 
     con = DriverManager.getConnection(connectionURL, "simple", "simple");
     return !connectionError;
    }

strack trace:


error occurred.
java.sql.SQLException: An unexpected exception was thrown
        at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown
Source)
        at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.Util.seeNextException(Unknown Source)
        at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown
Source)
        at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown
Source)
        at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
        at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
        at org.apache.derby.impl.jdbc.EmbedStatement.executeStatement(Unknown Source)
        at org.apache.derby.impl.jdbc.EmbedPreparedStatement.executeStatement(Unknown
Source)
        at org.apache.derby.impl.jdbc.EmbedPreparedStatement.executeUpdate(Unknown
Source)
        at learning.TestingBlob.doTest(TestingBlob.java:59)
        at learning.TestingBlob.<init>(TestingBlob.java:20)
        at learning.TestingBlob.main(TestingBlob.java:28)
Caused by: java.sql.SQLException: An unexpected exception was thrown
        at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown
Source)
        ... 13 more
Caused by: java.sql.SQLException: Java exception: ': java.io.IOException'.
        at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown
Source)
        at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown
Source)
        at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.Util.javaException(Unknown Source)
        at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown
Source)
        ... 10 more
Caused by: java.io.IOException
        at org.apache.derby.iapi.services.io.NewByteArrayInputStream.read(Unknown Source)
        at java.io.FilterInputStream.read(FilterInputStream.java:116)
        at org.apache.derby.iapi.services.io.LimitInputStream.read(Unknown Source)
        at org.apache.derby.iapi.types.RawToBinaryFormatStream.read(Unknown Source)
        at org.apache.derby.impl.store.raw.data.MemByteHolder.write(Unknown Source)
        at org.apache.derby.impl.store.raw.data.RememberBytesInputStream.fillBuf(Unknown
Source)
        at org.apache.derby.impl.store.raw.data.StoredPage.logColumn(Unknown Source)
        at org.apache.derby.impl.store.raw.data.StoredPage.logRow(Unknown Source)
        at org.apache.derby.impl.store.raw.data.UpdateOperation.writeOptionalDataToBuffer(Unknown
Source)
        at org.apache.derby.impl.store.raw.data.UpdateOperation.<init>(Unknown
Source)
        at org.apache.derby.impl.store.raw.data.LoggableActions.actionUpdate(Unknown
Source)
        at org.apache.derby.impl.store.raw.data.StoredPage.doUpdateAtSlot(Unknown Source)
        at org.apache.derby.impl.store.raw.data.BasePage.updateAtSlot(Unknown Source)
        at org.apache.derby.impl.store.access.conglomerate.GenericConglomerateController.replace(Unknown
Source)
        at org.apache.derby.impl.sql.execute.RowChangerImpl.updateRow(Unknown Source)
        at org.apache.derby.impl.sql.execute.UpdateResultSet.collectAffectedRows(Unknown
Source)
        at org.apache.derby.impl.sql.execute.UpdateResultSet.open(Unknown Source)
        at org.apache.derby.impl.sql.GenericPreparedStatement.executeStmt(Unknown Source)
        at org.apache.derby.impl.sql.GenericPreparedStatement.execute(Unknown Source)
        ... 6 more

--- On Mon, 15/2/10, Kristian Waagan <Kristian.Waagan@Sun.COM> wrote:

From: Kristian Waagan <Kristian.Waagan@Sun.COM>
Subject: Re: 'ERROR 40XD0: Container has been closed.: java.io.IOException'.
To: "Derby Discussion" <derby-user@db.apache.org>
Date: Monday, 15 February, 2010, 7:41 PM

On 15.02.10 14:15, Donald McLean wrote:
> When working with BLOBs and CLOBs, these sorts of errors are almost always
> caused by committed transactions. Since most people don't expect to need a
> transaction
>   for a select, more often than not autocommit is set to TRUE and the select
> commits as soon as it is executed. While this is fine for everything else,
> it doesn't work for BLOB/CLOB fields. A BLOB/CLOB is only valid inside a
> single transaction and the instant that transaction finished (commit or
> rollback) the BLOB/CLOB goes poof.
>    

Dinesh,

What Donald says is very true, but we have had some bugs in this area.
If following Donald's advice doesn't help you, please post the relevant 
part of your code (preferably in a runnable state) and the full stack trace.


Regards,
-- 
Kristian

> Donald
>
> On Mon, Feb 15, 2010 at 2:33 AM, Dinesh Bajaj<dinesh.bajaj@ymail.com>wrote:
>
>    
>> I get the above error while executing the Update statement through a java
>> application. The error occurs when the update statement involves a Blob
>> field.
>>
>> I have ensured that input stream being given to setBinaryStream method is
>> not closed and the length being given is also correct.
>>
>> Also, may I say that I can't find any source where I can readily find the
>> detailed description of a Derby error and its possible causes. The reference
>> documentation just cryptically says : "Container has been closed". Now, what
>> can be make out of this?
>>
>>
>>
>>      




      Your Mail works best with the New Yahoo Optimized IE8. Get it NOW! http://downloads.yahoo.com/in/internetexplorer/
Mime
View raw message