Is GenericObjectPools borrowObject Method thread safe?

我是研究僧i 提交于 2021-01-27 13:55:40

问题


In this question Is GenericObjectPool<T> from commons.apache.org thread safe? It is mentioned that its thread safe .

Edited:But im having a situation in my multithreaded application that two threads are getting the same object from the pool at the same time.-This statement was wrong.

I moved the borrowObject to synchronize block and it solved my issue.

Has anyone faced this issue earlier?

Here is my code:

public static GenericObjectPool<IDocBuilderPool> documentBuilderPool = new GenericObjectPool(new DocumentPool());

static {
    documentBuilderPool.setMaxActive(1000);
    documentBuilderPool.setMaxWait(30000);
    documentBuilderPool.setMaxIdle(-1);

}
//method that returns document pool called by multiple threads .

public static IDocBuilderPool getDocumentPool() {

    return documentBuilderPool.borrowObject();
}

//The pool factory class
public class DocumentPool extends BasePoolableObjectFactory<ICollabrrDocument> {

    public DomDocumentPool() {
    }

    @Override
    public DomDocument makeObject() throws Exception {
        // TODO Auto-generated method stub
        return new DomDocument();
    }

    @Override
    public void activateObject(IDocBuilderPool obj) throws Exception {
        // TODO Auto-generated method stub
        super.activateObject(obj);
    }

    @Override
    public void destroyObject(IDocBuilderPool obj) throws Exception {
        // TODO Auto-generated method stub
        super.destroyObject(obj);

    }

    @Override
    public void passivateObject(IDocBuilderPool obj) throws Exception {
        // TODO Auto-generated method stub
        obj.release();
        super.passivateObject(obj);
    }

    @Override
    public boolean validateObject(IDocBuilderPool obj) {
        // TODO Auto-generated method stub
        return super.validateObject(obj);
    }
}




public class DomDocument implements IDocBuilderPool  {

private Document domDocument;
private DocumentBuilder documentBuilder;
private DocumentBuilderFactory documentBuilderFactory;

public HashMap<org.w3c.dom.Node, DOMElement> elementMap = new HashMap<org.w3c.dom.Node, DOMElement>();

public long threadID;

public DomDocument()  {


    setDomDocument();
    this.threadID = Thread.currentThread().getId();

}

public void setDomDocument() throws 
    this.documentBuilderFactory = DocumentBuilderFactory.newInstance();


        this.documentBuilderFactory.setNamespaceAware(true);
        this.documentBuilder = this.documentBuilderFactory.newDocumentBuilder();
        this.domDocument = this.documentBuilder.parse(new ByteArrayInputStream("<Root/>".getBytes()));


}

}


回答1:


The documentation of PoolableObjectFactory states:

PoolableObjectFactory must be thread-safe.

Looking at your code, the only thing that could be thread unsafe is the call to obj.release();. This is possibly where your problem is.

Apart from that all looks ok...



来源:https://stackoverflow.com/questions/18127170/is-genericobjectpools-borrowobject-method-thread-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!