I saw AbandonedObjectPool but it is deprecated and is in the DBCP.
I was looking for an ObjectPool.

I've extended GenericObjectPool with similar features.
Here it is.
I'm using this as the ObjectPool of a PoolingDataSource and is working perfectly well.


note:
TimestampedStackTrace is not generic, it responds to my particular needs and can be taken away or replaced pretty simple.
N.



Dirk Verbeeck wrote:


Actually we already have something similar: AbandonedObjectPool.
http://jakarta.apache.org/commons/dbcp/xref/org/apache/commons/dbcp/AbandonedObjectPool.html



Many enhancements are possible, I have been playing around the java.lang.ref package to see if it can provide a safe way to recover abandoned objects.


Contributing is as simple as posting your improvements to the list.
A whole new file or a patch for existing files.
If there isn't a committer around then you can also make a bugzilla item for it. Writing comments to make the review easier helps and if you have junit tests then you can be sure your contribution won't go unnoticed.
If you're thinking about doing a lot of work then first discuss it on the list just to make sure we are on the same line.


Contributions are always welcome, bugfixes, enhancements, website updates, documentation, you name it...

Cheers
Dirk


Juan Ignacio Cidre wrote:


I was needing an ObjectPool that 'remembers' the borrowed objects so it can recollect them if not returned after a while.

Is there something like that arround?

I've extended GenericObjectPool to have that functionallity. I can give it to the project if there is not a better option than mine.
Since this would be my first contribution to the project, and I don't know the customs, I would need some help (I've read all the on-line guides, already, )
N.




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



/*
 * Created on 07-nov-2003
 * @author [EMAIL PROTECTED]
 */
package org.apache.commons.pool;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author [EMAIL PROTECTED]
 * @date 07-nov-2003
 * @this is a StackTrace and the exact time of its creation 
 */
public class TimestampedStackTrace {
        /**
        * The StackTrace is remembered as an exception to, next, ask its stackTrace()
        */
        private Exception stackTrace;
        
        /*
        * Millis of creation Time
        */
        private long lastUsed;
        
        /**
         * @param obj
         */
        public TimestampedStackTrace() {
                stackTrace = new Exception("Open StackTrace");
                lastUsed = System.currentTimeMillis();
        }

        /**
         * @return
         */
        public Exception getStackTrace() {
                return stackTrace;
        }

        /**
         * @return
         */
        public long getLastUsed() {
                return lastUsed;
        }

        public String getStackTraceAsString() {
                        StringWriter sw = new StringWriter();
                        stackTrace.printStackTrace(new PrintWriter(sw));
                        return sw.toString();
        }


}
/*
 * Created on 07-nov-2003
 * @author [EMAIL PROTECTED]
 */
package org.apache.commons.pool;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.pool.impl.GenericObjectPool;

import ar.com.eds.x71.framework.Ownable;
import ar.com.eds.x71.framework.log.SysLog;

/**
 * @author [EMAIL PROTECTED]
 * @date 07-nov-2003
 * @ObjectPool that remember borrowed objects and have politics to rescue them.
 */
public class ReapingObjectPool extends GenericObjectPool {
        /**
         * timpo que puede estar activa la conexion
         */
        private long maxActiveTime;

        /**
         * Conjunto de conexiones activas.
         */
        private Map active = new Hashtable();

        /**
         * Get a db connection from the pool.
         *
         * If removeAbandoned=true, recovers db connections which
         * have been idle > removeAbandonedTimeout.
         * 
         * @return Object jdbc Connection
         */
        public Object borrowObject() throws Exception {
                Object obj;
                try {
                        obj = super.borrowObject();
                }
                catch (Exception e) {
                        String[] activeTraces = getActiveTrace();
                        
SysLog.getLogger("ReapingObjectPool").warn("**********************************");
                        SysLog.getLogger("ReapingObjectPool").warn("PROBLEMAS TRAYENDO 
UN OBJETO DEL POOL");
                        SysLog.getLogger("ReapingObjectPool").warn("OBJETOS ACTIVOS 
DEL POOL");
                        SysLog.getLogger("ReapingObjectPool").warn("");

                        for (int i = 0; i < activeTraces.length; i++) {
                                String trace = activeTraces[i];
                                SysLog.getLogger("ReapingObjectPool").warn(trace);

                        }
                        
SysLog.getLogger("ReapingObjectPool").warn("**********************************");
                        throw e;
                }
                addActive(obj);
                return obj;
        }

        /**
         * @param obj
         */
        private void addActive(Object obj) {
                TimestampedStackTrace tst = new TimestampedStackTrace();
                //System.err.println(active);
                active.put(obj, tst);
                //System.err.println(active);
        }

        /**
         * Return a db connection to the pool.
         *
         * @param Object db Connection to return
         */
        public void returnObject(Object obj) throws Exception {
                removeActive(obj);
                super.returnObject(obj);
        }

        /**
         * @param obj
         */
        private void removeActive(Object obj) {
                //System.err.println(active);
                active.remove(obj);
                //System.err.println(active);
        }

        public void invalidateObject(Object obj) throws Exception {
                removeActive(obj);
                super.invalidateObject(obj);
        }

        /**
         * Recover abandoned db connections which have been idle
         * greater than the removeAbandonedTimeout.
         */
        private void removeAbandoned() {
                // Generate a list of abandoned connections to remove
                ArrayList remove = new ArrayList();
                synchronized (active) {
                        Iterator it = active.keySet().iterator();
                        while (it.hasNext()) {
                                Object obj = it.next();
                                TimestampedStackTrace tst = (TimestampedStackTrace) 
active.get(obj);
                                if (System.currentTimeMillis() - tst.getLastUsed() < 
getMaxActiveTime()) {
                                        continue;
                                }
                                if (getMaxActiveTime() > 0) {
                                        
SysLog.getLogger("ReapingObjectPool").error("Object Reaped : " + obj);
                                        
SysLog.getLogger("ReapingObjectPool").error("Object Reaped borrow StackTrace", 
tst.getStackTrace());
                                        remove.add(obj);
                                }
                        }
                }

                // Now remove the abandoned connections
                Iterator it = remove.iterator();
                while (it.hasNext()) {
                        Object toInvalid = it.next();
                        try {

                                invalidateObject(toInvalid);
                        }
                        catch (Exception e) {
                                SysLog.getLogger("ReapingObjectPool").error("Error al 
invalidar Object : " + toInvalid, e);
                        }
                }
        }

        /**
         * @return
         */
        public long getMaxActiveTime() {
                return maxActiveTime;
        }

        /**
         * @return
         */
        public Map getActive() {
                return active;
        }

        /**
         * @param list
         */
        public void setActive(Map list) {
                active = list;
        }

        /**
         * @param l
         */
        public void setMaxActiveTime(long l) {
                maxActiveTime = l;
        }

        /* (non-Javadoc)
         * @see org.apache.commons.pool.impl.GenericObjectPool#evict()
         */
        public synchronized void evict() throws Exception {
                super.evict();
                this.removeAbandoned();
        }

        /**
        * Devuelve información relacionada al estado de los objetos que se
        * encuentran activos, si estas estan en uso y quien las llamo, 
        * @return String[]
        */
        public String[] getActiveTrace() {
                String[] array = new String[active.size()];
                Iterator iterator = active.keySet().iterator();
                int i = 0;
                while (iterator.hasNext()) {
                        Object object = iterator.next();
                        TimestampedStackTrace value = (TimestampedStackTrace) 
active.get(object);
                        StringBuffer each = new StringBuffer();
                        each.append("object = " + object + "<br>\n");
                        if(value != null){
                                //Cuando el borrowObject falla por timeout, en el map, 
object apunta a null 
                                each.append("usedTime = " + 
(System.currentTimeMillis() - value.getLastUsed()) + "<br>\n");
                                each.append("getTrace = " + 
value.getStackTraceAsString() + "<br>\n");
                        }
                        else{
                                each.append("usedTime = NO VALUE<BR>\n");
                                each.append("getTrace = NO VALUE<BR>\n");
                        }
                        if (object instanceof Ownable) {
                                Ownable ownable = (Ownable) object;
                                each.append("Owner = " + ownable.getOwner() + 
"<br>\n");
                        }
                        else{
                                each.append("Owner = NO VALUE <BR>\n");
                        }

                        array[i++] = each.toString();
                }
                return array;
        }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to