Thread:
 Free temperary clob/blob with interceptor (1) 
 ingramchen   12 Sep 2004, 10:36 
 Re: Free temperary clob/blob with interceptor (... 
 ingramchen   12 Sep 2004, 10:37 
 Problem with interceptor approach. 
 rajwani   03 Mar 2005, 11:38 
 Re: Problem with interceptor approach. 
 rajwani   03 Mar 2005, 16:08 

Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
From: ingramchen (12 Sep 2004, 10:36) Replies: 3, Views: 40816
Subject: Free temperary clob/blob with interceptor (1)
We are using a specialized interceptor to free temperary clob/blob
resource, it seems work fine now:

<code>
public class LobCleanUpInterceptor implements Interceptor {
    private static final Logger logger = Logger
            .getLogger(LobCleanUpInterceptor.class);

    // leave these method unchanged
    public boolean onSave(...)
    public boolean onLoad(...)
    public boolean onFlushDirty(...)
    public void onDelete(...)
    public void preFlush(...)
    public Boolean isUnsaved(...)
    public int[] findDirty(...)
    public Object instantiate(...)

    // a thread local set to store temperary LOBs
    private static final ThreadLocal threadTempLobs 
                                     = new ThreadLocal();

    // after flush(), clean all registered LOBs
    public void postFlush(Iterator entities) 
                           throws CallbackException {
        Set tempLobs = (Set) threadTempLobs.get();
        if (tempLobs == null) {
            return;
        }
        try {
            for (Iterator iter = tempLobs.iterator(); 
                                      iter.hasNext();) {
                Object lob = iter.next();
                cleanIfBLOB(lob);
                cleanIfCLOB(lob);
            }
        } catch (SQLException e) {
            logger.fatal("clean LOB failed"+e.getMessage(), e);
            throw new RuntimeException(e);
        } finally {
            threadTempLobs.set(null);
            tempLobs.clear();
        }
    }

    // free temperary clob resource
    private static void cleanIfCLOB(Object lob) 
                                throws SQLException {
        if (lob instanceof oracle.sql.CLOB) {
            oracle.sql.CLOB clob = (oracle.sql.CLOB) lob;
            if (clob.isTemporary()) {
                oracle.sql.CLOB.freeTemporary(clob);
                logger.info("clob cleaned");
            }
        }
    }

    // free temperary blob resource
    private static void cleanIfBLOB(Object lob) 
                                 throws SQLException {
        if (lob instanceof oracle.sql.BLOB) {
            oracle.sql.BLOB blob = (oracle.sql.BLOB) lob;
            if (blob.isTemporary()) {
                oracle.sql.BLOB.freeTemporary(blob);
                logger.info("blob cleaned");
            }
        }
    }

    // register oracle temperary BLOB/CLOB into 
    // a thread-local set, this should be called at
    // the end of nullSafeSet(...) in BinaryBlobType
    // or StringClobType
    public static void registerTempLobs(Object lob) {
        getTempLobs().add(lob);
    }

    // lazy create temperary lob storage
    public static Set getTempLobs() {
        Set tempLobs = (Set) threadTempLobs.get();
        if (tempLobs == null) {
            tempLobs = new HashSet();
            threadTempLobs.set(tempLobs);
        }
        return tempLobs;
    }
}
</code>
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]