Ciaran Fisher
Ciaran Fisher

Reputation: 1006

MongoDB GridFS Error

I'm using gridFS and it appears after I write a file, if I immediately do a read to try to find it mongoDB throws a runtime exception but it seems intermittent.

The java code that causes it is:

List<GridFSDBFile> files = PlayJongo.gridfs().find(filename);

The relevant code in the driver in the GridFS class is below:

/**
 * finds a list of files matching the given query
 * @param query
 * @param sort
 * @return
 * @throws MongoException 
 */
public List<GridFSDBFile> find( DBObject query , DBObject sort){
    List<GridFSDBFile> files = new ArrayList<GridFSDBFile>();

    DBCursor c = null;
    try {
        c = _filesCollection.find( query );
        if (sort != null) {
            c.sort(sort);
        }
        while ( c.hasNext() ){
            files.add( _fix( c.next() ) );
        }
    } finally {
         if (c != null){
             c.close();
         }
    }
    return files;
}

protected GridFSDBFile _fix( Object o ){
    if ( o == null )
        return null;

    if ( ! ( o instanceof GridFSDBFile ) )
        throw new RuntimeException( "somehow didn't get a GridFSDBFile" );

    GridFSDBFile f = (GridFSDBFile)o;
    f._fs = this;
    return f;
}

I'm using play! 2.1 with the Jongo wrapper but they shouldn't affect it as I'm accessing gridFS directly using mongodb's java driver.

Whats odd is if i debug than the data I actually want is in the object but its not the object that the driver is expecting:

enter image description here

Has anyone ever encountered this?

Upvotes: 1

Views: 1080

Answers (2)

Long_Le
Long_Le

Reputation: 96

More details on this bug fix:

  1. Save the state of the 'fs.files' collection before fs.files.find() by

    Class save = fs.files.getObjectClass();

  2. Restore the state afterward

    fs.files.setObjectClass(save)

Upvotes: 0

Ciaran Fisher
Ciaran Fisher

Reputation: 1006

Found the answer. If you access the fs.files collection using the jongo api it breaks further access to the gridFS file system, even if you use the mongoDB driver directly.

A patch has been issue but is not yet integrated into a release

Patch: https://github.com/bguerout/jongo/commit/0d27ba3ebfd065e7bf4ad323ee21059b762b512e

Upvotes: 1

Related Questions