⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectpool.java

📁 Java Database connection pool
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            o = create();            locked.put(o, new Long(now));            ++count;            debug("PoolMan ObjectPool: created a new object for request");            debugMetrics();            return o;        }        // no objects available, and there is no room to create new objects        else {            if (metadata.isMaximumSoft()) {                debug("PoolMan ObjectPool: No available objects and maximum pool size soft limit reached... " + "creating an emergency object that will be removed by automatic garbage collection");                debugMetrics();                o = create();                locked.put(o, new Long(now));                ++count;                return o;            }            else {                debug("PoolMan ObjectPool: No available objects and maximum pool size hard limit reached... " + "waiting for an object to be checked in");                wait(3000);                return checkOut();            }        }    }    /** Checks an object back into the pool. */    protected synchronized void checkIn(Object o) {        locked.remove(o);        unlocked.put(o, new Long(System.currentTimeMillis()));        if (!metadata.isMaximumSoft()) {            notifyAll();        }        debug("PoolMan ObjectPool metrics after returning object:");        debugMetrics();    }    public synchronized void updateLocked(PooledObject o) {        if (locked.containsKey(o)) {            locked.put(o, new Long(System.currentTimeMillis()));        }    }    /**     * Remove timed-out objects from the pool, called by PoolSkimmerThread.     * Checks the unlocked objects for expired members, and shrinks the pool     * according to the shrinkBy value, ensuring that it keeps at least the     * minimumSize number of objects available.     */    protected synchronized void cleanUp() {        int minSize = metadata.getMinimumSize();        if (unlocked.size() <= minSize)            return;        int maxSize = metadata.getMaximumSize();        int shrinkBy = metadata.getShrinkBy();        if (shrinkBy < 1)            shrinkBy = PoolManConstants.DEFAULT_SHRINKBY;        int shrunken = 0;        debug("PoolSkimmer cleaning objects from the pool that have exceeded" +              " their allotted lifespan, has " + unlocked.size() +              " pooled objects to evaluate...");        try {            long now = System.currentTimeMillis();            // copy into temp list to avoid enum shared struct problem            ArrayList unlockedObjects = new ArrayList();            for (Enumeration enum = unlocked.keys(); enum.hasMoreElements();) {                unlockedObjects.add(enum.nextElement());            }            for (int i = 0; i < unlockedObjects.size(); i++) {                Object o = unlockedObjects.get(i);                long lasttouch = ((Long) unlocked.get(o)).longValue();                // Object has expired                if ((now - lasttouch) > (1000 * metadata.getObjectTimeout())) {                    unlocked.remove(o);                    expire(o);                    --count;                    shrunken++;                    o = null;                    debug("PoolSkimmer: Removed and destroyed a pooled object from " + getPoolname());                    debugMetrics();                }                // don't clean past min size                // if objects get old and should be invalid but aren't                // removed b/c of min size, they will still be invalidated                // upon checkOut                if (unlocked.size() <= minSize)                    return;                // don't shrink past the shrinkBy value unless the                // max size soft limit has been exceeded, which can happen                // in an emergency                if ((shrunken >= shrinkBy) && (unlocked.size() + locked.size() <= maxSize))                    return;            }        } catch (Exception e) {            log("PoolSkimmer unable to clean up available objects in the pool");        }        System.gc();    }    /**     * Determine whether locked objects have timed out and     * should be checked back in. Called by LifeGuardThread.     */    protected synchronized void checkTimeout() {        long now = System.currentTimeMillis();        try {            // copy into temp list to avoid enum shared struct problem            ArrayList lockedObjects = new ArrayList();            for (Enumeration enum = locked.keys(); enum.hasMoreElements();) {                lockedObjects.add(enum.nextElement());            }            for (int i = 0; i < lockedObjects.size(); i++) {                Object o = lockedObjects.get(i);                long lasttouch = ((Long) locked.get(o)).longValue();                if ((System.currentTimeMillis() - lasttouch) > (1000 * metadata.getUserTimeout())) {                    debug("LifeGuard returning an object to pool " + getPoolname() +                          " that had exceeded its user timeout");                    if (o instanceof PooledObject) {                        PooledObject po = (PooledObject) o;                        po.clean();                    }                    locked.remove(o);                    unlocked.put(o, new Long(System.currentTimeMillis()));                    debugMetrics();                }            }        } catch (Exception e) {            debug("LifeGuard Unable to Evaluate Objects", e);        }    }    /**     * Close all resources in the pool.     */    public synchronized void closeAllResources() {        for (Enumeration enum = this.unlocked.keys(); enum.hasMoreElements();) {            Object o = enum.nextElement();            expire(o);            o = null;        }        for (Enumeration enum = this.locked.keys(); enum.hasMoreElements();) {            Object o = enum.nextElement();            expire(o);            o = null;        }    }    public void finalize() {        closeAllResources();    }    public void log(String message) {        //String date = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL).format(new Date());        //this.logger.println(date + ": " + message);        logit.info(message);    }    public void log(String message, Exception e) {        /*log(message);        this.logger.println("EXCEPTION: " + e.getMessage() + ": " + e.toString());        if (metadata.isDebugging())            e.printStackTrace(this.logger);*/        logit.error(message, e);    }    public void debug(String message) {        if (metadata.isDebugging())            logit.debug(message);    }    public void debug(String message, Exception e) {        if (metadata.isDebugging())            logit.debug(message, e);    }    protected void debugMetrics() {        if (metadata.isDebugging()) {            logit.debug("\tPool Name: " + metadata.getName() +                        " {" + " Total Objects: " + count +                        " Objects Available: " + unlocked.size() +                        " Objects In Use: " + locked.size() + " }");        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -